MetalRuller

pylong temp mutes

Sep 21st, 2020
979
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.     Pylon Instructional tempmutes codebase
  3.     This is more-or-less for learning purposes and not for actual use on your server, because
  4.      for actual use you'll need a lot more checks & safety than this.
  5.  
  6. */
  7.    
  8. const muteRole = 'YOUR MUTE ROLE ID';
  9.  
  10. const muteKv = new pylon.KVNamespace('mutes');
  11. const commandsAdmin = new discord.command.CommandGroup({
  12.   defaultPrefix: '!',
  13.   filters: discord.command.filters.isAdministrator()
  14. });
  15.  
  16. async function TempMute(member: discord.GuildMember, duration: number) {
  17.   // add the actual mute role to the user
  18.   if (!member.roles.includes(muteRole)) await member.addRole(muteRole);
  19.   // store our expiration time for the mute
  20.   await muteKv.put(member.user.id, Date.now() + duration, {
  21.     ifNotExists: true
  22.   });
  23. }
  24.  
  25. async function UnMute(member: discord.GuildMember) {
  26.   // remove the mute role from the user if they have it
  27.   if (member.roles.includes(muteRole)) await member.removeRole(muteRole);
  28.   // delete our stored kv
  29.   await muteKv.delete(member.user.id);
  30. }
  31.  
  32. // let's check our task every 5 minutes
  33. pylon.tasks.cron('Every_5_Min', '0 0/5 * * * * *', async () => {
  34.   const now = Date.now();
  35.   // get all our items of potentially muted users
  36.   const items = await muteKv.items();
  37.   // get our guild object
  38.   const guild = await discord.getGuild();
  39.   let toRemove: string[] = [];
  40.   // loop thru every item to see if it expired
  41.   await Promise.all(
  42.     items.map(async (val) => {
  43.       const member = await guild.getMember(val.key);
  44.       // if the member is no longer on your server, or no longer has the mute role, let's remove it
  45.       if (member === null || !member.roles.includes(muteRole)) {
  46.         toRemove.push(val.key);
  47.         return;
  48.       }
  49.       if (typeof val.value !== 'number') return; // type safety check
  50.       const diff = now - val.value;
  51.       if (diff > 0) {
  52.         // The mute has expired!
  53.         await member.removeRole(muteRole);
  54.         toRemove.push(val.key);
  55.       }
  56.     })
  57.   );
  58.   // clean our KVs for expired mutes
  59.   if (toRemove.length > 0) {
  60.     // @ts-ignore
  61.     await muteKv.transactMulti(toRemove, () => undefined);
  62.   }
  63. });
  64.  
  65. commandsAdmin.on(
  66.   'mute',
  67.   (ctx) => ({
  68.     member: ctx.guildMember(),
  69.     duration: ctx.integer({ minValue: 1, maxValue: 1000000 })
  70.   }),
  71.   async (msg, { member, duration }) => {
  72.     await msg.reply(async () => {
  73.       if (member.roles.includes(muteRole))
  74.         return 'The target is already muted!';
  75.       await TempMute(member, duration * 1000 * 60);
  76.       return `${
  77.         discord.decor.Emojis.WHITE_CHECK_MARK
  78.       } ${member.toMention()} was muted for ${duration} minutes!`;
  79.     });
  80.   }
  81. );
  82.  
  83. commandsAdmin.on(
  84.   'unmute',
  85.   (ctx) => ({
  86.     member: ctx.guildMember()
  87.   }),
  88.   async (msg, { member }) => {
  89.     await msg.reply(async () => {
  90.       if (!member.roles.includes(muteRole)) return 'The target is not muted!';
  91.       await UnMute(member);
  92.       return `${
  93.         discord.decor.Emojis.WHITE_CHECK_MARK
  94.       } ${member.toMention()} was un-muted!`;
  95.     });
  96.   }
  97. );
  98.  
RAW Paste Data