Nimbi

tickets.ts

May 19th, 2022 (edited)
673
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import { config } from '../modules/config/cfg';
  2.  
  3. import * as db from '../database/betterKV';
  4.  
  5. const role = config.modules.roles;
  6. const chan = config.modules.channels;
  7.  
  8. const group = config.slashCommands;
  9. const logChannel: string = chan.logging_channel.id;
  10. const ticketChannel: string = chan.tickets_channel.id;
  11. const ticketChannelCategory: string = chan.tickets_category.id;
  12. const adminRoles: Array<string> = [role.ticket_manager.id];
  13.  
  14. function intersection(arr1: Array<string>, arr2: Array<string>): number {
  15.   return arr1.filter((value) => arr2.includes(value)).length;
  16. }
  17.  
  18. discord.on(discord.Event.MESSAGE_CREATE, async (message) => {
  19.   if (!(message.channelId == ticketChannel)) return;
  20.   if (message.content.length > 100) {
  21.     await message.delete();
  22.     let msg = await message.reply(
  23.       'Your ticket name cannot contain more than 100 characters!'
  24.     );
  25.     await setTimeout(() => msg.delete(), 10000);
  26.   }
  27.  
  28.   let guild = await discord.getGuild();
  29.  
  30.   let permissions = [
  31.     {
  32.       type: discord.Channel.PermissionOverwriteType.MEMBER,
  33.       id: message.author.id,
  34.       allow: 0x00000400,
  35.     },
  36.  
  37.     {
  38.       type: discord.Channel.PermissionOverwriteType.ROLE,
  39.       id: guild.id,
  40.       deny: 0x00000400,
  41.     },
  42.   ];
  43.  
  44.   for (let role of adminRoles)
  45.     permissions.push({
  46.       type: discord.Channel.PermissionOverwriteType.ROLE,
  47.       id: role,
  48.       allow: 0x00000400,
  49.     });
  50.  
  51.   let channel = (await guild.createChannel({
  52.     name: message.content,
  53.     type: discord.Channel.Type.GUILD_TEXT,
  54.     permissionOverwrites: permissions,
  55.     parentId: ticketChannelCategory,
  56.   })) as discord.GuildTextChannel;
  57.  
  58.   await channel.sendMessage(
  59.     `${message.member?.toMention()} your ticket has been opened. You may provide further details as a staff member will be with you shortly.`
  60.   );
  61.  
  62.   await message.delete();
  63.   await db.save(channel.id, [`Log of ticket with topic: ${message.content}`]);
  64. });
  65.  
  66. discord.on(discord.Event.MESSAGE_CREATE, async (message) => {
  67.   if (!(await db.exist(message.channelId))) return;
  68.  
  69.   db.transact(message.channelId, (val) =>
  70.     (val as Array<string>).concat(
  71.       `${message.author.username}: ${message.content}`
  72.     )
  73.   );
  74. });
  75.  
  76. group.register(
  77.   {
  78.     name: 'close',
  79.     description: 'Closes and archives the ticket, channel, and its history.',
  80.     options: (args) => ({
  81.       channel_id: args.string({
  82.         name: 'channelid',
  83.         description: 'The channel you want to close',
  84.         required: false,
  85.       }),
  86.     }),
  87.   },
  88.   async (message, { channel_id }) => {
  89.     channel_id = channel_id ?? message.channelId;
  90.  
  91.     if (intersection(message.member.roles, adminRoles) == 0)
  92.       return await message.respondEphemeral(
  93.         'You lack the permissions to do this'
  94.       );
  95.  
  96.     if (!(await db.exist(channel_id)))
  97.       return await message.respondEphemeral('This channel id is not a ticket!');
  98.  
  99.     let logChannelObject: discord.GuildTextChannel | null =
  100.       await discord.getGuildTextChannel(logChannel);
  101.     let ticketChannelObject: discord.GuildTextChannel | null =
  102.       await discord.getGuildTextChannel(channel_id);
  103.  
  104.     let _logs: Array<string> = (await db.get(channel_id)) as Array<string>;
  105.     let logs: string = _logs.join('\n');
  106.     await ticketChannelObject?.delete();
  107.     await db.del(channel_id);
  108.  
  109.     logChannelObject?.sendMessage({
  110.       attachments: [
  111.         {
  112.           name: 'log.txt',
  113.           data: new TextEncoder().encode(logs).buffer,
  114.         },
  115.       ],
  116.     });
  117.   }
  118. );
  119.  
Add Comment
Please, Sign In to add comment