Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const commandGroupOptions: discord.command.ICommandGroupOptions = {
- defaultPrefix: '?'
- };
- const responses = {
- yes: ['yes', 'y'],
- no: ['no', 'n']
- };
- const descriptions = {
- ban: 'Bans a mentioned user **MODS ONLY**',
- kick: 'Kicks a mentioned user **MODS ONLY**',
- unban: 'Unbans a mentioned user **MODS ONLY**',
- softban:
- 'Bans and unbans a user (deletes messages sent in the past 7 days) **MODS ONLY**',
- slowmode: 'Sets the slowmode for a channel **MODS ONLY**'
- };
- const cmdgroup = new discord.command.CommandGroup(commandGroupOptions);
- const enum ModAction {
- BAN = 'ban',
- KICK = 'kick',
- SOFTBAN = 'softban',
- UNBAN = 'unban'
- }
- interface ModEntry {
- userId: string;
- messageId: string;
- targetId: string;
- action: ModAction;
- triggeredAt: number;
- requiresGuildMemberObject: boolean; // commands like ban require a guild member object, while unban does not
- }
- // Used in our slowmode command
- const timeSuffix: {
- [index: string]: number;
- } = {
- s: 1,
- m: 60,
- h: 3600
- };
- // How long to wait for confirmation
- const ttl = 60000;
- const modKv = new pylon.KVNamespace('modResponses');
- async function handleModCommand(
- message: discord.GuildMemberMessage,
- userId: string,
- action: ModAction,
- requiresGuildMemberObject: boolean
- ) {
- const user = await discord.getUser(userId);
- if (!user) return message.reply('Could not find user');
- const confirmMsg = await message.reply(
- `Do you really want to ${action} __${user.getTag()}__? Reply with __y__es or __n__o within the next ${ttl /
- 1000} seconds.`
- );
- const entry: ModEntry = {
- action,
- messageId: confirmMsg.id,
- userId: message.author.id,
- targetId: userId,
- triggeredAt: Date.now(),
- requiresGuildMemberObject
- };
- await modKv.put(message.author.id, <any>entry, { ttl });
- }
- // Commands
- cmdgroup.on(
- {
- name: 'ban',
- filters: discord.command.filters.canBanMembers(),
- description: descriptions.ban
- },
- (ctx) => ({
- member: ctx.guildMember()
- }),
- async (message, { member }) =>
- await handleModCommand(message, member.user.id, ModAction.BAN, true)
- );
- cmdgroup.on(
- {
- name: 'kick',
- filters: discord.command.filters.canKickMembers(),
- description: descriptions.kick
- },
- (ctx) => ({
- member: ctx.guildMember()
- }),
- async (message, { member }) =>
- await handleModCommand(message, member.user.id, ModAction.KICK, true)
- );
- cmdgroup.on(
- {
- name: 'softban',
- filters: discord.command.filters.canBanMembers(),
- description: descriptions.softban
- },
- (ctx) => ({
- member: ctx.guildMember()
- }),
- async (message, { member }) =>
- await handleModCommand(message, member.user.id, ModAction.SOFTBAN, true)
- );
- cmdgroup.on(
- {
- name: 'unban',
- filters: discord.command.filters.canBanMembers(),
- description: descriptions.unban
- },
- (ctx) => ({ userId: ctx.string() }),
- async (message, { userId }) =>
- await handleModCommand(message, userId, ModAction.UNBAN, false)
- );
- cmdgroup.on(
- {
- name: 'slowmode',
- filters: discord.command.filters.canManageChannels(),
- description: descriptions.slowmode
- },
- (ctx) => ({ time: ctx.string() }),
- async (message, { time }) => {
- const [, num, format] = time.match(/(\d+)([smh])/) ?? [];
- if (!num || !format)
- return message.reply(
- `Invalid format! Example: ${commandGroupOptions.defaultPrefix}slowmode 10s`
- );
- const channel = await message.getChannel();
- await channel.edit({
- rateLimitPerUser: parseInt(num, 10) * timeSuffix[format]
- });
- await message.reply(`Updated slowmode for channel ${channel.toMention()}`);
- }
- );
- // Handlers
- discord.on(discord.Event.MESSAGE_CREATE, async (message) => {
- if (
- !message.author ||
- message.author.bot ||
- !(message instanceof discord.GuildMemberMessage)
- )
- return;
- const entry = <ModEntry>(<unknown>await modKv.get(message.author.id));
- if (!entry) return;
- const guild = await message.getGuild();
- const member = await guild.getMember(entry.targetId);
- const user = <discord.User>(
- (member?.user || (await discord.getUser(entry.targetId)))
- );
- if (responses.yes.some((v) => message.content.toLowerCase() === v)) {
- const reason = `Responsible moderator: ${message.author.getTag()}`;
- const processingMsg = await message.reply(
- `Performing action ${entry.action} on ${user.getTag()}`
- );
- try {
- switch (entry.action) {
- case ModAction.BAN:
- await member?.ban({ reason });
- break;
- case ModAction.KICK:
- await member?.kick();
- break;
- case ModAction.SOFTBAN:
- await member?.ban({
- reason,
- deleteMessageDays: 7
- });
- await guild.deleteBan(entry.targetId);
- break;
- case ModAction.UNBAN:
- guild.deleteBan(entry.targetId);
- break;
- }
- await processingMsg.edit(
- `OK! Executed action ${entry.action} on user ${(
- member?.user ?? user
- ).getTag()}`
- );
- } catch (e) {
- await processingMsg.edit(`Action failed: ${e.message}`);
- }
- await modKv.delete(message.author.id);
- } else if (responses.no.some((v) => message.content.toLowerCase() === v)) {
- await message.reply(`Cancelled ${entry.action} for ${user.getTag()}`);
- await modKv.delete(message.author.id);
- }
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement