3
0
Fork 0
mirror of https://github.com/ZeppelinBot/Zeppelin.git synced 2025-03-19 07:20:00 +00:00
zeppelin/backend/src/plugins/Utility/commands/VcmoveCmd.ts

200 lines
7.1 KiB
TypeScript
Raw Normal View History

2021-06-30 18:43:42 +02:00
import { Snowflake, VoiceChannel } from "discord.js";
import {
channelToConfigAccessibleChannel,
memberToConfigAccessibleMember,
userToConfigAccessibleUser,
2021-07-25 14:32:08 +02:00
} from "../../../utils/configAccessibleObjects";
import { commandTypeHelpers as ct } from "../../../commandTypes";
import { LogType } from "../../../data/LogType";
import { canActOn, sendErrorMessage, sendSuccessMessage } from "../../../pluginUtils";
2021-07-25 14:32:08 +02:00
import { channelMentionRegex, isSnowflake, simpleClosestStringMatch } from "../../../utils";
import { utilityCmd } from "../types";
export const VcmoveCmd = utilityCmd({
trigger: "vcmove",
description: "Move a member to another voice channel",
usage: "!vcmove @Dragory 473223047822704651",
permission: "can_vcmove",
signature: {
member: ct.resolvedMember(),
channel: ct.string({ catchAll: true }),
},
async run({ message: msg, args, pluginData }) {
let channel: VoiceChannel;
if (isSnowflake(args.channel)) {
// Snowflake -> resolve channel directly
2021-06-30 18:43:42 +02:00
const potentialChannel = pluginData.guild.channels.cache.get(args.channel as Snowflake);
if (!potentialChannel || !(potentialChannel instanceof VoiceChannel)) {
sendErrorMessage(pluginData, msg.channel, "Unknown or non-voice channel");
return;
}
channel = potentialChannel;
} else if (channelMentionRegex.test(args.channel)) {
// Channel mention -> parse channel id and resolve channel from that
const channelId = args.channel.match(channelMentionRegex)![1];
2021-06-30 18:43:42 +02:00
const potentialChannel = pluginData.guild.channels.cache.get(channelId as Snowflake);
if (!potentialChannel || !(potentialChannel instanceof VoiceChannel)) {
sendErrorMessage(pluginData, msg.channel, "Unknown or non-voice channel");
return;
}
channel = potentialChannel;
} else {
// Search string -> find closest matching voice channel name
const voiceChannels = pluginData.guild.channels.cache.array().filter(theChannel => {
return theChannel instanceof VoiceChannel;
}) as VoiceChannel[];
const closestMatch = simpleClosestStringMatch(args.channel, voiceChannels, ch => ch.name);
if (!closestMatch) {
sendErrorMessage(pluginData, msg.channel, "No matching voice channels");
return;
}
channel = closestMatch;
}
if (!args.member.voice?.channelId) {
sendErrorMessage(pluginData, msg.channel, "Member is not in a voice channel");
return;
}
2021-07-04 23:14:12 +02:00
if (args.member.voice.channelId === channel.id) {
sendErrorMessage(pluginData, msg.channel, "Member is already on that channel!");
return;
}
2021-07-04 23:14:12 +02:00
const oldVoiceChannel = pluginData.guild.channels.cache.get(args.member.voice.channelId);
try {
await args.member.edit({
channel: channel.id,
});
} catch {
2021-01-28 00:28:26 +01:00
sendErrorMessage(pluginData, msg.channel, "Failed to move member");
return;
}
pluginData.state.logs.log(LogType.VOICE_CHANNEL_FORCE_MOVE, {
mod: userToConfigAccessibleUser(msg.author),
member: memberToConfigAccessibleMember(args.member),
oldChannel: channelToConfigAccessibleChannel(oldVoiceChannel!),
newChannel: channelToConfigAccessibleChannel(channel),
});
2021-07-29 00:37:19 +01:00
sendSuccessMessage(pluginData, msg.channel, `**${args.member.user.tag}** moved to **${channel.name}**`);
},
});
export const VcmoveAllCmd = utilityCmd({
trigger: "vcmoveall",
description: "Move all members of a voice channel to another voice channel",
usage: "!vcmoveall 551767166395875334 767497573560352798",
permission: "can_vcmove",
signature: {
oldChannel: ct.voiceChannel(),
channel: ct.string({ catchAll: true }),
},
async run({ message: msg, args, pluginData }) {
let channel: VoiceChannel;
if (isSnowflake(args.channel)) {
// Snowflake -> resolve channel directly
2021-06-30 18:43:42 +02:00
const potentialChannel = pluginData.guild.channels.cache.get(args.channel as Snowflake);
if (!potentialChannel || !(potentialChannel instanceof VoiceChannel)) {
sendErrorMessage(pluginData, msg.channel, "Unknown or non-voice channel");
return;
}
channel = potentialChannel;
} else if (channelMentionRegex.test(args.channel)) {
// Channel mention -> parse channel id and resolve channel from that
const channelId = args.channel.match(channelMentionRegex)![1];
2021-06-30 18:43:42 +02:00
const potentialChannel = pluginData.guild.channels.cache.get(channelId as Snowflake);
if (!potentialChannel || !(potentialChannel instanceof VoiceChannel)) {
sendErrorMessage(pluginData, msg.channel, "Unknown or non-voice channel");
return;
}
channel = potentialChannel;
} else {
// Search string -> find closest matching voice channel name
const voiceChannels = pluginData.guild.channels.cache.array().filter(theChannel => {
return theChannel instanceof VoiceChannel;
}) as VoiceChannel[];
const closestMatch = simpleClosestStringMatch(args.channel, voiceChannels, ch => ch.name);
if (!closestMatch) {
sendErrorMessage(pluginData, msg.channel, "No matching voice channels");
return;
}
channel = closestMatch;
}
if (args.oldChannel.members.size === 0) {
sendErrorMessage(pluginData, msg.channel, "Voice channel is empty");
return;
}
if (args.oldChannel.id === channel.id) {
sendErrorMessage(pluginData, msg.channel, "Cant move from and to the same channel!");
return;
}
// Cant leave null, otherwise we get an assignment error in the catch
let currMember = msg.member;
const moveAmt = args.oldChannel.members.size;
let errAmt = 0;
for (const memberWithId of args.oldChannel.members) {
currMember = memberWithId[1];
// Check for permissions but allow self-moves
if (currMember.id !== msg.member.id && !canActOn(pluginData, msg.member, currMember)) {
sendErrorMessage(
pluginData,
msg.channel,
2021-07-29 00:37:19 +01:00
`Failed to move ${currMember.user.tag} (${currMember.id}): You cannot act on this member`,
);
errAmt++;
continue;
}
try {
currMember.edit({
channel: channel.id,
});
} catch {
if (msg.member.id === currMember.id) {
sendErrorMessage(pluginData, msg.channel, "Unknown error when trying to move members");
return;
}
2021-07-29 00:37:19 +01:00
sendErrorMessage(pluginData, msg.channel, `Failed to move ${currMember.user.tag} (${currMember.id})`);
errAmt++;
continue;
}
pluginData.state.logs.log(LogType.VOICE_CHANNEL_FORCE_MOVE, {
mod: userToConfigAccessibleUser(msg.author),
member: memberToConfigAccessibleMember(currMember),
oldChannel: channelToConfigAccessibleChannel(args.oldChannel),
newChannel: channelToConfigAccessibleChannel(channel),
});
}
if (moveAmt !== errAmt) {
sendSuccessMessage(
pluginData,
msg.channel,
`${moveAmt - errAmt} members from **${args.oldChannel.name}** moved to **${channel.name}**`,
);
} else {
sendErrorMessage(pluginData, msg.channel, `Failed to move any members.`);
}
},
});