Advertisement
ii_kinqm1ke

ban.slowmode.kick.softban

Jun 7th, 2020
267
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const commandGroupOptions: discord.command.ICommandGroupOptions = {
  2.   defaultPrefix: '?'
  3. };
  4.  
  5. const responses = {
  6.   yes: ['yes', 'y'],
  7.   no: ['no', 'n']
  8. };
  9.  
  10. const descriptions = {
  11.   ban: 'Bans a mentioned user **MODS ONLY**',
  12.   kick: 'Kicks a mentioned user **MODS ONLY**',
  13.   unban: 'Unbans a mentioned user **MODS ONLY**',
  14.   softban:
  15.     'Bans and unbans a user (deletes messages sent in the past 7 days) **MODS ONLY**',
  16.   slowmode: 'Sets the slowmode for a channel **MODS ONLY**'
  17. };
  18.  
  19. const cmdgroup = new discord.command.CommandGroup(commandGroupOptions);
  20.  
  21. const enum ModAction {
  22.   BAN = 'ban',
  23.   KICK = 'kick',
  24.   SOFTBAN = 'softban',
  25.   UNBAN = 'unban'
  26. }
  27.  
  28. interface ModEntry {
  29.   userId: string;
  30.   messageId: string;
  31.   targetId: string;
  32.   action: ModAction;
  33.   triggeredAt: number;
  34.   requiresGuildMemberObject: boolean; // commands like ban require a guild member object, while unban does not
  35. }
  36.  
  37. // Used in our slowmode command
  38. const timeSuffix: {
  39.   [index: string]: number;
  40. } = {
  41.   s: 1,
  42.   m: 60,
  43.   h: 3600
  44. };
  45.  
  46. // How long to wait for confirmation
  47. const ttl = 60000;
  48.  
  49. const modKv = new pylon.KVNamespace('modResponses');
  50.  
  51. async function handleModCommand(
  52.   message: discord.GuildMemberMessage,
  53.   userId: string,
  54.   action: ModAction,
  55.   requiresGuildMemberObject: boolean
  56. ) {
  57.   const user = await discord.getUser(userId);
  58.   if (!user) return message.reply('Could not find user');
  59.  
  60.   const confirmMsg = await message.reply(
  61.     `Do you really want to ${action} __${user.getTag()}__? Reply with __y__es or __n__o within the next ${ttl /
  62.       1000} seconds.`
  63.   );
  64.  
  65.   const entry: ModEntry = {
  66.     action,
  67.     messageId: confirmMsg.id,
  68.     userId: message.author.id,
  69.     targetId: userId,
  70.     triggeredAt: Date.now(),
  71.     requiresGuildMemberObject
  72.   };
  73.  
  74.   await modKv.put(message.author.id, <any>entry, { ttl });
  75. }
  76.  
  77. // Commands
  78. cmdgroup.on(
  79.   {
  80.     name: 'ban',
  81.     filters: discord.command.filters.canBanMembers(),
  82.     description: descriptions.ban
  83.   },
  84.   (ctx) => ({
  85.     member: ctx.guildMember()
  86.   }),
  87.   async (message, { member }) =>
  88.     await handleModCommand(message, member.user.id, ModAction.BAN, true)
  89. );
  90.  
  91. cmdgroup.on(
  92.   {
  93.     name: 'kick',
  94.     filters: discord.command.filters.canKickMembers(),
  95.     description: descriptions.kick
  96.   },
  97.   (ctx) => ({
  98.     member: ctx.guildMember()
  99.   }),
  100.   async (message, { member }) =>
  101.     await handleModCommand(message, member.user.id, ModAction.KICK, true)
  102. );
  103.  
  104. cmdgroup.on(
  105.   {
  106.     name: 'softban',
  107.     filters: discord.command.filters.canBanMembers(),
  108.     description: descriptions.softban
  109.   },
  110.   (ctx) => ({
  111.     member: ctx.guildMember()
  112.   }),
  113.   async (message, { member }) =>
  114.     await handleModCommand(message, member.user.id, ModAction.SOFTBAN, true)
  115. );
  116.  
  117. cmdgroup.on(
  118.   {
  119.     name: 'unban',
  120.     filters: discord.command.filters.canBanMembers(),
  121.     description: descriptions.unban
  122.   },
  123.   (ctx) => ({ userId: ctx.string() }),
  124.   async (message, { userId }) =>
  125.     await handleModCommand(message, userId, ModAction.UNBAN, false)
  126. );
  127.  
  128. cmdgroup.on(
  129.   {
  130.     name: 'slowmode',
  131.     filters: discord.command.filters.canManageChannels(),
  132.     description: descriptions.slowmode
  133.   },
  134.   (ctx) => ({ time: ctx.string() }),
  135.   async (message, { time }) => {
  136.     const [, num, format] = time.match(/(\d+)([smh])/) ?? [];
  137.     if (!num || !format)
  138.       return message.reply(
  139.         `Invalid format! Example: ${commandGroupOptions.defaultPrefix}slowmode 10s`
  140.       );
  141.  
  142.     const channel = await message.getChannel();
  143.     await channel.edit({
  144.       rateLimitPerUser: parseInt(num, 10) * timeSuffix[format]
  145.     });
  146.  
  147.     await message.reply(`Updated slowmode for channel ${channel.toMention()}`);
  148.   }
  149. );
  150.  
  151. // Handlers
  152. discord.on(discord.Event.MESSAGE_CREATE, async (message) => {
  153.   if (
  154.     !message.author ||
  155.     message.author.bot ||
  156.     !(message instanceof discord.GuildMemberMessage)
  157.   )
  158.     return;
  159.  
  160.   const entry = <ModEntry>(<unknown>await modKv.get(message.author.id));
  161.   if (!entry) return;
  162.  
  163.   const guild = await message.getGuild();
  164.   const member = await guild.getMember(entry.targetId);
  165.   const user = <discord.User>(
  166.     (member?.user || (await discord.getUser(entry.targetId)))
  167.   );
  168.  
  169.   if (responses.yes.some((v) => message.content.toLowerCase() === v)) {
  170.     const reason = `Responsible moderator: ${message.author.getTag()}`;
  171.  
  172.     const processingMsg = await message.reply(
  173.       `Performing action ${entry.action} on ${user.getTag()}`
  174.     );
  175.     try {
  176.       switch (entry.action) {
  177.         case ModAction.BAN:
  178.           await member?.ban({ reason });
  179.           break;
  180.         case ModAction.KICK:
  181.           await member?.kick();
  182.           break;
  183.         case ModAction.SOFTBAN:
  184.           await member?.ban({
  185.             reason,
  186.             deleteMessageDays: 7
  187.           });
  188.           await guild.deleteBan(entry.targetId);
  189.           break;
  190.         case ModAction.UNBAN:
  191.           guild.deleteBan(entry.targetId);
  192.           break;
  193.       }
  194.  
  195.       await processingMsg.edit(
  196.         `OK! Executed action ${entry.action} on user ${(
  197.           member?.user ?? user
  198.         ).getTag()}`
  199.       );
  200.     } catch (e) {
  201.       await processingMsg.edit(`Action failed: ${e.message}`);
  202.     }
  203.  
  204.     await modKv.delete(message.author.id);
  205.   } else if (responses.no.some((v) => message.content.toLowerCase() === v)) {
  206.     await message.reply(`Cancelled ${entry.action} for ${user.getTag()}`);
  207.     await modKv.delete(message.author.id);
  208.   }
  209. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement