Advertisement
Nimbi

tickets.ts

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