3
0
Fork 0
mirror of https://github.com/ZeppelinBot/Zeppelin.git synced 2025-05-12 04:55:01 +00:00

Migrate LocateUser to new Plugin structure

This commit is contained in:
Dark 2020-07-08 02:53:44 +02:00
parent 4a8a63e8b8
commit 63efaf84ee
16 changed files with 380 additions and 0 deletions

View file

@ -0,0 +1,11 @@
import { VoiceChannel } from "eris";
export async function createOrReuseInvite(vc: VoiceChannel) {
const existingInvites = await vc.getInvites();
if (existingInvites.length !== 0) {
return existingInvites[0];
} else {
return vc.createInvite(undefined);
}
}

View file

@ -0,0 +1,9 @@
export async function fillActiveAlertsList(pluginData) {
const allAlerts = await pluginData.state.alerts.getAllGuildAlerts();
allAlerts.forEach(alert => {
if (!pluginData.state.usersWithAlerts.includes(alert.user_id)) {
pluginData.state.usersWithAlerts.push(alert.user_id);
}
});
}

View file

@ -0,0 +1,25 @@
import { Member, TextableChannel } from "eris";
import { PluginData } from "knub";
import { LocateUserPluginType } from "../types";
import { sendErrorMessage } from "src/pluginUtils";
export async function moveMember(
pluginData: PluginData<LocateUserPluginType>,
toMoveID: string,
target: Member,
errorChannel: TextableChannel,
) {
const modMember: Member = await this.bot.getRESTGuildMember(pluginData.guild.id, toMoveID);
if (modMember.voiceState.channelID != null) {
try {
await modMember.edit({
channelID: target.voiceState.channelID,
});
} catch (e) {
sendErrorMessage(pluginData, errorChannel, "Failed to move you. Are you in a voice channel?");
return;
}
} else {
sendErrorMessage(pluginData, errorChannel, "Failed to move you. Are you in a voice channel?");
}
}

View file

@ -0,0 +1,17 @@
import { SECONDS } from "src/utils";
import { removeUserIdFromActiveAlerts } from "./removeUserIdFromActiveAlerts";
const ALERT_LOOP_TIME = 30 * SECONDS;
export async function outdatedAlertsLoop(pluginData) {
const outdatedAlerts = await pluginData.state.alerts.getOutdatedAlerts();
for (const alert of outdatedAlerts) {
await pluginData.state.alerts.delete(alert.id);
await removeUserIdFromActiveAlerts(pluginData, alert.user_id);
}
if (!pluginData.state.unloaded) {
pluginData.state.outdatedAlertsTimeout = setTimeout(() => this.outdatedAlertsLoop(pluginData), ALERT_LOOP_TIME);
}
}

View file

@ -0,0 +1,6 @@
export async function removeUserIdFromActiveAlerts(pluginData, userId: string) {
const index = pluginData.state.usersWithAlerts.indexOf(userId);
if (index > -1) {
pluginData.state.usersWithAlerts.splice(index, 1);
}
}

View file

@ -0,0 +1,20 @@
import { PluginData } from "knub";
import { LocateUserPluginType } from "../types";
import { resolveMember } from "src/utils";
import { sendWhere } from "./sendWhere";
import { TextableChannel } from "eris";
import { moveMember } from "./moveMember";
export async function sendAlerts(pluginData: PluginData<LocateUserPluginType>, userId: string) {
const triggeredAlerts = await pluginData.state.alerts.getAlertsByUserId(userId);
const member = await resolveMember(pluginData.client, pluginData.guild, userId);
triggeredAlerts.forEach(alert => {
const prepend = `<@!${alert.requestor_id}>, an alert requested by you has triggered!\nReminder: \`${alert.body}\`\n`;
const txtChannel = pluginData.client.getChannel(alert.channel_id) as TextableChannel;
sendWhere.call(this, pluginData.guild, member, txtChannel, prepend);
if (alert.active) {
moveMember(pluginData, alert.requestor_id, member, txtChannel);
}
});
}

View file

@ -0,0 +1,22 @@
import { Guild, Member, TextableChannel, VoiceChannel } from "eris";
import { getInviteLink } from "knub/dist/helpers";
import { createOrReuseInvite } from "./createOrReuseInvite";
export async function sendWhere(guild: Guild, member: Member, channel: TextableChannel, prepend: string) {
const voice = guild.channels.get(member.voiceState.channelID) as VoiceChannel;
if (voice == null) {
channel.createMessage(prepend + "That user is not in a channel");
} else {
let invite = null;
try {
invite = await createOrReuseInvite(voice);
} catch (e) {
this.sendErrorMessage(channel, "Cannot create an invite to that channel!");
return;
}
channel.createMessage(
prepend + `${member.mention} is in the following channel: \`${voice.name}\` ${getInviteLink(invite)}`,
);
}
}