2019-06-26 01:04:11 +02:00
|
|
|
import { decorators as d, IPluginOptions, getInviteLink, logger } from "knub";
|
2019-08-22 02:58:32 +03:00
|
|
|
import { trimPluginDescription, ZeppelinPlugin } from "./ZeppelinPlugin";
|
2019-06-26 01:04:11 +02:00
|
|
|
import humanizeDuration from "humanize-duration";
|
2019-06-28 23:53:10 +02:00
|
|
|
import { Message, Member, Guild, TextableChannel, VoiceChannel, Channel, User } from "eris";
|
2019-06-26 01:04:11 +02:00
|
|
|
import { GuildVCAlerts } from "../data/GuildVCAlerts";
|
2019-08-04 13:16:46 +03:00
|
|
|
import moment from "moment-timezone";
|
|
|
|
import { resolveMember, sorter, createChunkedMessage, errorMessage, successMessage, MINUTES } from "../utils";
|
2019-07-21 21:15:52 +03:00
|
|
|
import * as t from "io-ts";
|
2019-06-26 01:04:11 +02:00
|
|
|
|
2019-07-21 21:15:52 +03:00
|
|
|
const ConfigSchema = t.type({
|
|
|
|
can_where: t.boolean,
|
|
|
|
can_alert: t.boolean,
|
|
|
|
});
|
|
|
|
type TConfigSchema = t.TypeOf<typeof ConfigSchema>;
|
2019-06-26 01:04:11 +02:00
|
|
|
|
2019-07-21 21:15:52 +03:00
|
|
|
const ALERT_LOOP_TIME = 30 * 1000;
|
2019-06-26 01:04:11 +02:00
|
|
|
|
2019-07-21 21:15:52 +03:00
|
|
|
export class LocatePlugin extends ZeppelinPlugin<TConfigSchema> {
|
2019-06-26 01:04:11 +02:00
|
|
|
public static pluginName = "locate_user";
|
2019-08-22 02:58:32 +03:00
|
|
|
public static configSchema = ConfigSchema;
|
|
|
|
|
|
|
|
public static pluginInfo = {
|
|
|
|
prettyName: "Locate user",
|
|
|
|
description: trimPluginDescription(`
|
|
|
|
This plugin allows users with access to the commands the following:
|
|
|
|
* Instantly receive an invite to the voice channel of a user
|
|
|
|
* Be notified as soon as a user switches or joins a voice channel
|
|
|
|
`),
|
|
|
|
};
|
2019-06-26 01:04:11 +02:00
|
|
|
|
|
|
|
private alerts: GuildVCAlerts;
|
|
|
|
private outdatedAlertsTimeout;
|
|
|
|
private usersWithAlerts: string[] = [];
|
|
|
|
|
2019-08-22 01:22:26 +03:00
|
|
|
public static getStaticDefaultOptions(): IPluginOptions<TConfigSchema> {
|
2019-06-26 01:04:11 +02:00
|
|
|
return {
|
|
|
|
config: {
|
|
|
|
can_where: false,
|
|
|
|
can_alert: false,
|
|
|
|
},
|
|
|
|
overrides: [
|
|
|
|
{
|
|
|
|
level: ">=50",
|
|
|
|
config: {
|
|
|
|
can_where: true,
|
|
|
|
can_alert: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
],
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
onLoad() {
|
|
|
|
this.alerts = GuildVCAlerts.getGuildInstance(this.guildId);
|
|
|
|
this.outdatedAlertsLoop();
|
|
|
|
this.fillActiveAlertsList();
|
|
|
|
}
|
|
|
|
|
|
|
|
async outdatedAlertsLoop() {
|
|
|
|
const outdatedAlerts = await this.alerts.getOutdatedAlerts();
|
|
|
|
|
|
|
|
for (const alert of outdatedAlerts) {
|
|
|
|
await this.alerts.delete(alert.id);
|
2019-08-10 01:47:45 +03:00
|
|
|
await this.removeUserIdFromActiveAlerts(alert.user_id);
|
2019-06-26 01:04:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
this.outdatedAlertsTimeout = setTimeout(() => this.outdatedAlertsLoop(), ALERT_LOOP_TIME);
|
|
|
|
}
|
|
|
|
|
|
|
|
async fillActiveAlertsList() {
|
|
|
|
const allAlerts = await this.alerts.getAllGuildAlerts();
|
|
|
|
|
|
|
|
allAlerts.forEach(alert => {
|
|
|
|
if (!this.usersWithAlerts.includes(alert.user_id)) {
|
|
|
|
this.usersWithAlerts.push(alert.user_id);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-08-22 02:58:32 +03:00
|
|
|
@d.command("where", "<member:resolvedMember>", {
|
2019-09-22 17:06:22 +03:00
|
|
|
extra: {
|
|
|
|
info: {
|
|
|
|
description: "Posts an instant invite to the voice channel that `<member>` is in",
|
|
|
|
},
|
2019-08-22 02:58:32 +03:00
|
|
|
},
|
|
|
|
})
|
2019-06-26 01:04:11 +02:00
|
|
|
@d.permission("can_where")
|
|
|
|
async whereCmd(msg: Message, args: { member: Member; time?: number; reminder?: string }) {
|
2019-08-04 13:16:46 +03:00
|
|
|
const member = await resolveMember(this.bot, this.guild, args.member.id);
|
2019-06-26 01:04:11 +02:00
|
|
|
sendWhere(this.guild, member, msg.channel, `${msg.member.mention} |`);
|
|
|
|
}
|
|
|
|
|
2019-06-28 23:26:24 +02:00
|
|
|
@d.command("vcalert", "<member:resolvedMember> [duration:delay] [reminder:string$]", {
|
|
|
|
aliases: ["vca"],
|
2019-09-22 17:06:22 +03:00
|
|
|
extra: {
|
|
|
|
info: {
|
|
|
|
description: "Sets up an alert that notifies you any time `<member>` switches or joins voice channels",
|
|
|
|
},
|
2019-08-22 02:58:32 +03:00
|
|
|
},
|
2019-06-28 23:26:24 +02:00
|
|
|
})
|
2019-06-26 01:04:11 +02:00
|
|
|
@d.permission("can_alert")
|
2019-06-28 23:26:24 +02:00
|
|
|
async vcalertCmd(msg: Message, args: { member: Member; duration?: number; reminder?: string }) {
|
2019-08-04 13:16:46 +03:00
|
|
|
const time = args.duration || 10 * MINUTES;
|
|
|
|
const alertTime = moment().add(time, "millisecond");
|
|
|
|
const body = args.reminder || "None";
|
2019-06-26 01:04:11 +02:00
|
|
|
|
|
|
|
this.alerts.add(msg.author.id, args.member.id, msg.channel.id, alertTime.format("YYYY-MM-DD HH:mm:ss"), body);
|
|
|
|
if (!this.usersWithAlerts.includes(args.member.id)) {
|
|
|
|
this.usersWithAlerts.push(args.member.id);
|
|
|
|
}
|
|
|
|
|
|
|
|
msg.channel.createMessage(
|
|
|
|
`If ${args.member.mention} joins or switches VC in the next ${humanizeDuration(time)} i will notify you`,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-06-28 23:26:24 +02:00
|
|
|
@d.command("vcalerts")
|
|
|
|
@d.permission("can_alert")
|
|
|
|
async listVcalertCmd(msg: Message) {
|
|
|
|
const alerts = await this.alerts.getAlertsByRequestorId(msg.member.id);
|
|
|
|
if (alerts.length === 0) {
|
|
|
|
this.sendErrorMessage(msg.channel, "You have no active alerts!");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
alerts.sort(sorter("expires_at"));
|
|
|
|
const longestNum = (alerts.length + 1).toString().length;
|
|
|
|
const lines = Array.from(alerts.entries()).map(([i, alert]) => {
|
|
|
|
const num = i + 1;
|
|
|
|
const paddedNum = num.toString().padStart(longestNum, " ");
|
|
|
|
return `\`${paddedNum}.\` \`${alert.expires_at}\` Member: <@!${alert.user_id}> Reminder: \`${alert.body}\``;
|
|
|
|
});
|
|
|
|
createChunkedMessage(msg.channel, lines.join("\n"));
|
|
|
|
}
|
|
|
|
|
|
|
|
@d.command("vcalerts delete", "<num:number>", {
|
|
|
|
aliases: ["vcalerts d"],
|
|
|
|
})
|
|
|
|
@d.permission("can_alert")
|
|
|
|
async deleteVcalertCmd(msg: Message, args: { num: number }) {
|
|
|
|
const alerts = await this.alerts.getAlertsByRequestorId(msg.member.id);
|
|
|
|
alerts.sort(sorter("expires_at"));
|
|
|
|
const lastNum = alerts.length + 1;
|
|
|
|
|
|
|
|
if (args.num > lastNum || args.num < 0) {
|
|
|
|
msg.channel.createMessage(errorMessage("Unknown alert"));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const toDelete = alerts[args.num - 1];
|
|
|
|
await this.alerts.delete(toDelete.id);
|
|
|
|
|
|
|
|
msg.channel.createMessage(successMessage("Alert deleted"));
|
|
|
|
}
|
|
|
|
|
2019-06-26 01:04:11 +02:00
|
|
|
@d.event("voiceChannelJoin")
|
|
|
|
async userJoinedVC(member: Member, channel: Channel) {
|
|
|
|
if (this.usersWithAlerts.includes(member.id)) {
|
|
|
|
this.sendAlerts(member.id);
|
2019-08-10 01:47:45 +03:00
|
|
|
await this.removeUserIdFromActiveAlerts(member.id);
|
2019-06-26 01:04:11 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@d.event("voiceChannelSwitch")
|
|
|
|
async userSwitchedVC(member: Member, newChannel: Channel, oldChannel: Channel) {
|
|
|
|
if (this.usersWithAlerts.includes(member.id)) {
|
|
|
|
this.sendAlerts(member.id);
|
2019-08-10 01:47:45 +03:00
|
|
|
await this.removeUserIdFromActiveAlerts(member.id);
|
2019-06-26 01:04:11 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-28 23:53:10 +02:00
|
|
|
@d.event("guildBanAdd")
|
|
|
|
async onGuildBanAdd(_, user: User) {
|
|
|
|
const alerts = await this.alerts.getAlertsByUserId(user.id);
|
|
|
|
alerts.forEach(alert => {
|
|
|
|
this.alerts.delete(alert.id);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-08-10 01:47:45 +03:00
|
|
|
async sendAlerts(userId: string) {
|
|
|
|
const triggeredAlerts = await this.alerts.getAlertsByUserId(userId);
|
|
|
|
const member = await resolveMember(this.bot, this.guild, userId);
|
2019-06-26 01:04:11 +02:00
|
|
|
|
|
|
|
triggeredAlerts.forEach(alert => {
|
2019-11-02 22:11:26 +02:00
|
|
|
const prepend = `<@!${alert.requestor_id}>, an alert requested by you has triggered!\nReminder: \`${alert.body}\`\n`;
|
2019-08-04 13:16:46 +03:00
|
|
|
sendWhere(this.guild, member, this.bot.getChannel(alert.channel_id) as TextableChannel, prepend);
|
2019-06-26 01:04:11 +02:00
|
|
|
this.alerts.delete(alert.id);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-08-10 01:47:45 +03:00
|
|
|
async removeUserIdFromActiveAlerts(userId: string) {
|
|
|
|
const index = this.usersWithAlerts.indexOf(userId);
|
2019-06-26 01:04:11 +02:00
|
|
|
if (index > -1) {
|
|
|
|
this.usersWithAlerts.splice(index, 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function sendWhere(guild: Guild, member: Member, channel: TextableChannel, prepend: string) {
|
2019-08-04 13:16:46 +03:00
|
|
|
const voice = guild.channels.get(member.voiceState.channelID) as VoiceChannel;
|
2019-06-26 01:04:11 +02:00
|
|
|
|
|
|
|
if (voice == null) {
|
|
|
|
channel.createMessage(prepend + "That user is not in a channel");
|
|
|
|
} else {
|
2019-08-04 13:16:46 +03:00
|
|
|
const invite = await createInvite(voice);
|
2019-06-26 01:04:11 +02:00
|
|
|
channel.createMessage(
|
|
|
|
prepend + ` ${member.mention} is in the following channel: ${voice.name} https://${getInviteLink(invite)}`,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function createInvite(vc: VoiceChannel) {
|
2019-08-04 13:16:46 +03:00
|
|
|
const existingInvites = await vc.getInvites();
|
2019-06-26 01:04:11 +02:00
|
|
|
|
|
|
|
if (existingInvites.length !== 0) {
|
|
|
|
return existingInvites[0];
|
|
|
|
} else {
|
|
|
|
return vc.createInvite(undefined);
|
|
|
|
}
|
|
|
|
}
|