Skip to content

Instantly share code, notes, and snippets.

@PhantomNimbi
Last active July 5, 2022 07:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save PhantomNimbi/d001ee9859187b23474316199cf84045 to your computer and use it in GitHub Desktop.
Save PhantomNimbi/d001ee9859187b23474316199cf84045 to your computer and use it in GitHub Desktop.
Pylon.Bot Ticket System
import * as app from '../config';
import * as DB from '../database/betterKV';
let config = {
slashCommands: discord.interactions.commands,
roles: {
ticket_managers: ['721103743579324467', '850580487714111568'],
},
tickets: {
channel_id: '993781851388919828',
category_id: '993781107730427944',
},
logger: {
channel_id: '927217111342936074',
embed_color: discord.decor.RoleColors.DARK_RED,
},
embeds: {
color: discord.decor.RoleColors.BLUE,
},
};
let slashCommands = config.slashCommands;
function intersection(arr1: Array<string>, arr2: Array<string>): number {
return arr1.filter((value) => arr2.includes(value)).length;
}
discord.on(discord.Event.MESSAGE_CREATE, async (message) => {
if (!(message.channelId == config.tickets.channel_id)) return;
if (message.content.length > 100) {
await message.delete();
let msg = await message.reply(
'Your ticket name cannot contain more than 100 characters!'
);
await setTimeout(() => msg.delete(), 10000);
}
let guild = await discord.getGuild();
let permissions = [
{
type: discord.Channel.PermissionOverwriteType.MEMBER,
id: message.author.id,
allow: 0x00000400,
},
{
type: discord.Channel.PermissionOverwriteType.ROLE,
id: guild.id,
deny: 0x00000400,
},
];
for (let role of config.roles.ticket_managers)
permissions.push({
type: discord.Channel.PermissionOverwriteType.ROLE,
id: role,
allow: 0x00000400,
});
let channel = (await guild.createChannel({
name: message.content,
type: discord.Channel.Type.GUILD_TEXT,
// @ts-ignore
permissionOverwrites: permissions,
parentId: config.tickets.category_id,
})) as discord.GuildTextChannel;
await channel.sendMessage(
`${message.member?.toMention()} your ticket has been opened. You may provide further details as a staff member will be with you shortly.`
);
await message.delete();
await DB.save(channel.id, [`Log of ticket with topic: ${message.content}`]);
});
discord.on(discord.Event.MESSAGE_CREATE, async (message) => {
if (!(await DB.exist(message.channelId))) return;
DB.transact(message.channelId, (val) =>
(val as Array<string>).concat(
`${message.author.username}: ${message.content}`
)
);
});
slashCommands.register(
{
name: 'ticket',
description: 'Pylon Ticket System',
options: (args) => ({
command: args.string({
name: 'command',
description: 'Choose a command',
required: true,
choices: ['close', 'setup'],
}),
channel: args.guildChannel({
name: 'channel',
description: 'Specify a channel',
required: false,
}),
}),
},
async (interaction, { command, channel }) => {
let self = await discord.getBotUser();
try {
if (command == 'close') {
// @ts-ignore
let channel_id = channel.id ?? interaction.channelId;
if (
intersection(
interaction.member.roles,
config.roles.ticket_managers
) == 0
)
return await interaction.respondEphemeral(
'You lack the permissions to do this'
);
if (!(await DB.exist(channel_id)))
return await interaction.respondEphemeral(
'This channel id is not a ticket!'
);
let logChannel: discord.GuildTextChannel | null =
await discord.getGuildTextChannel(config.logger.channel_id);
let ticketChannel: discord.GuildTextChannel | null =
await discord.getGuildTextChannel(channel_id);
let _logs: Array<string> = (await DB.get(channel_id)) as Array<string>;
let logs: string = _logs.join('\n');
await ticketChannel?.delete();
await DB.del(channel_id);
await logChannel?.sendMessage({
attachments: [
{
name: 'log.txt',
data: new TextEncoder().encode(logs).buffer,
},
],
});
await interaction.respondEphemeral(
`\\${discord.decor.Emojis.BALLOT_BOX_WITH_CHECK}`
);
} else {
if (command == 'setup') {
let guild = await discord.getGuild();
let channel = await discord.getGuildTextChannel(
config.tickets.channel_id
);
const embed = new discord.Embed();
embed.setTitle('Pylon Ticket System');
embed.setDescription(
[
'Welcome to the Pylon Ticket System.',
'',
'To open a ticket simply type a breif description of your problem here and a private ticket channel will be opened for you.',
'',
].join('\n')
);
embed.setColor(config.embeds.color);
embed.setThumbnail({ url: self.getAvatarUrl() });
embed.setFooter({
text: 'Powered by ' + self.username,
iconUrl: self.getAvatarUrl(),
});
await channel?.sendMessage({ embed: embed });
await interaction.respondEphemeral(
`\\${discord.decor.Emojis.BALLOT_BOX_WITH_CHECK}`
);
}
}
} catch (_) {
console.log(
// @ts-ignore
`>> | (${_.code}) ${_.name}: ${_.message} \n------------------------------------------------------------\n${_.stack}`
);
let channel = await discord.getTextChannel(config.logger.channel_id);
let embed = new discord.Embed();
embed.setTitle(':warning: Error Triggered :warning:');
embed.setFields([
{
name: 'Error Code',
// @ts-ignore
value: '```ts\n' + _.code + '\n```',
inline: true,
},
{
name: 'Error Name',
// @ts-ignore
value: '```ts\n' + _.name + '\n```',
inline: true,
},
// @ts-ignore
{ name: 'Error Message', value: _.message, inline: false },
{
name: 'Error Stack',
// @ts-ignore
value: ['```ts', `${_.stack}`, '```'].join('\n'),
inline: false,
},
]);
embed.setColor(discord.decor.RoleColors.DARK_RED);
embed.setTimestamp(new Date().toISOString());
embed.setFooter({
text: 'Powered by ' + self.username,
iconUrl: self.getAvatarUrl(),
});
await channel?.sendMessage(embed);
}
}
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment