2021-06-30 18:43:42 +02:00
|
|
|
import { Snowflake, VoiceChannel } from "discord.js";
|
2021-07-21 22:14:09 +02:00
|
|
|
import {
|
|
|
|
channelToConfigAccessibleChannel,
|
|
|
|
memberToConfigAccessibleMember,
|
|
|
|
userToConfigAccessibleUser,
|
2021-07-25 14:32:08 +02:00
|
|
|
} from "../../../utils/configAccessibleObjects";
|
2020-07-06 01:51:48 +03:00
|
|
|
import { commandTypeHelpers as ct } from "../../../commandTypes";
|
2021-06-06 23:51:32 +02:00
|
|
|
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";
|
2021-06-06 23:51:32 +02:00
|
|
|
import { utilityCmd } from "../types";
|
2021-06-01 02:05:55 +02:00
|
|
|
|
2020-07-06 01:51:48 +03:00
|
|
|
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);
|
2020-07-06 01:51:48 +03:00
|
|
|
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
|
2020-11-09 20:03:57 +02:00
|
|
|
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);
|
2020-07-06 01:51:48 +03:00
|
|
|
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
|
2021-06-02 04:07:50 +02:00
|
|
|
const voiceChannels = pluginData.guild.channels.cache.array().filter(theChannel => {
|
2020-07-06 01:51:48 +03:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2021-07-29 01:43:50 +01:00
|
|
|
if (!args.member.voice?.channelId) {
|
2020-07-06 01:51:48 +03:00
|
|
|
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) {
|
2020-07-06 01:51:48 +03:00
|
|
|
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);
|
2020-07-06 01:51:48 +03:00
|
|
|
|
|
|
|
try {
|
|
|
|
await args.member.edit({
|
2021-06-02 04:07:50 +02:00
|
|
|
channel: channel.id,
|
2020-07-06 01:51:48 +03:00
|
|
|
});
|
2021-05-06 19:23:47 +01:00
|
|
|
} catch {
|
2021-01-28 00:28:26 +01:00
|
|
|
sendErrorMessage(pluginData, msg.channel, "Failed to move member");
|
2020-07-06 01:51:48 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
pluginData.state.logs.log(LogType.VOICE_CHANNEL_FORCE_MOVE, {
|
2021-07-21 22:14:09 +02:00
|
|
|
mod: userToConfigAccessibleUser(msg.author),
|
|
|
|
member: memberToConfigAccessibleMember(args.member),
|
|
|
|
oldChannel: channelToConfigAccessibleChannel(oldVoiceChannel!),
|
|
|
|
newChannel: channelToConfigAccessibleChannel(channel),
|
2020-07-06 01:51:48 +03:00
|
|
|
});
|
|
|
|
|
2021-07-29 00:37:19 +01:00
|
|
|
sendSuccessMessage(pluginData, msg.channel, `**${args.member.user.tag}** moved to **${channel.name}**`);
|
2020-07-06 01:51:48 +03:00
|
|
|
},
|
|
|
|
});
|
2020-12-12 21:15:15 +01:00
|
|
|
|
|
|
|
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);
|
2020-12-12 21:15:15 +01:00
|
|
|
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);
|
2020-12-12 21:15:15 +01:00
|
|
|
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
|
2021-06-02 04:07:50 +02:00
|
|
|
const voiceChannels = pluginData.guild.channels.cache.array().filter(theChannel => {
|
2020-12-12 21:15:15 +01:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2021-06-02 04:07:50 +02:00
|
|
|
if (args.oldChannel.members.size === 0) {
|
2020-12-12 21:15:15 +01:00
|
|
|
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;
|
2021-06-02 04:07:50 +02:00
|
|
|
const moveAmt = args.oldChannel.members.size;
|
2020-12-20 17:31:28 +01:00
|
|
|
let errAmt = 0;
|
2021-06-02 04:07:50 +02:00
|
|
|
for (const memberWithId of args.oldChannel.members) {
|
2020-12-20 17:31:28 +01:00
|
|
|
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`,
|
2020-12-20 17:31:28 +01:00
|
|
|
);
|
|
|
|
errAmt++;
|
|
|
|
continue;
|
|
|
|
}
|
2020-12-12 21:15:15 +01:00
|
|
|
|
2020-12-20 17:31:28 +01:00
|
|
|
try {
|
2020-12-12 21:15:15 +01:00
|
|
|
currMember.edit({
|
2021-06-02 04:07:50 +02:00
|
|
|
channel: channel.id,
|
2020-12-12 21:15:15 +01:00
|
|
|
});
|
2021-05-06 19:23:47 +01:00
|
|
|
} catch {
|
2020-12-20 17:31:28 +01:00
|
|
|
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})`);
|
2020-12-20 17:31:28 +01:00
|
|
|
errAmt++;
|
|
|
|
continue;
|
2020-12-12 21:15:15 +01:00
|
|
|
}
|
2020-12-20 17:31:28 +01:00
|
|
|
|
|
|
|
pluginData.state.logs.log(LogType.VOICE_CHANNEL_FORCE_MOVE, {
|
2021-07-21 22:14:09 +02:00
|
|
|
mod: userToConfigAccessibleUser(msg.author),
|
|
|
|
member: memberToConfigAccessibleMember(currMember),
|
|
|
|
oldChannel: channelToConfigAccessibleChannel(args.oldChannel),
|
|
|
|
newChannel: channelToConfigAccessibleChannel(channel),
|
2020-12-20 17:31:28 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (moveAmt !== errAmt) {
|
|
|
|
sendSuccessMessage(
|
2020-12-12 21:15:15 +01:00
|
|
|
pluginData,
|
|
|
|
msg.channel,
|
2020-12-20 17:31:28 +01:00
|
|
|
`${moveAmt - errAmt} members from **${args.oldChannel.name}** moved to **${channel.name}**`,
|
2020-12-12 21:15:15 +01:00
|
|
|
);
|
2020-12-20 17:31:28 +01:00
|
|
|
} else {
|
|
|
|
sendErrorMessage(pluginData, msg.channel, `Failed to move any members.`);
|
2020-12-12 21:15:15 +01:00
|
|
|
}
|
|
|
|
},
|
|
|
|
});
|