Advertisement
Nimbi

commands/tickets.ts

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