Files
src
.gitignore
package.json
README.md
tsconfig.json
Code
13 1,134
src/bot.ts
import {
Client,
GatewayIntentBits,
Events,
Interaction,
REST,
Routes,
ButtonInteraction,
ChatInputCommandInteraction,
Collection,
PermissionFlagsBits
} from 'discord.js';
import * as dotenv from 'dotenv';
import { TicketManager } from './managers/ticketManager';
import { TicketCategory, TicketConfig } from './types/ticket';
import { EmbedFactory } from './utils/embeds';
import { ButtonFactory } from './utils/buttons';
import * as setupCommand from './commands/setup';
import * as closeCommand from './commands/close';
import * as addCommand from './commands/add';
import * as removeCommand from './commands/remove';
dotenv.config();
const client = new Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.MessageContent,
GatewayIntentBits.GuildMembers
]
});
const commands = new Collection<string, any>();
commands.set('setup', setupCommand);
commands.set('close', closeCommand);
commands.set('add', addCommand);
commands.set('remove', removeCommand);
let ticketManager: TicketManager;
client.once(Events.ClientReady, async (c) => {
console.log(`✅ Bot is ready! Logged in as ${c.user.tag}`);
const config: TicketConfig = {
guildId: process.env.GUILD_ID || '',
staffRoleId: process.env.STAFF_ROLE_ID || '',
ticketCategoryId: process.env.TICKET_CATEGORY_ID || '',
logChannelId: process.env.LOG_CHANNEL_ID || '',
priorityRoleId: process.env.PRIORITY_ROLE_ID
};
ticketManager = new TicketManager(config);
const guild = await client.guilds.fetch(config.guildId);
if (guild) {
ticketManager.setGuildReference(guild);
}
await registerCommands();
});
client.on(Events.InteractionCreate, async (interaction: Interaction) => {
if (interaction.isChatInputCommand()) {
await handleCommandInteraction(interaction);
} else if (interaction.isButton()) {
await handleButtonInteraction(interaction);
}
});
async function handleCommandInteraction(interaction: ChatInputCommandInteraction): Promise<void> {
const command = commands.get(interaction.commandName);
if (!command) return;
try {
if (interaction.commandName === 'setup') {
await command.execute(interaction);
} else {
await command.execute(interaction, ticketManager);
}
} catch (error) {
console.error(`Error executing command ${interaction.commandName}:`, error);
const errorEmbed = EmbedFactory.createErrorEmbed('An error occurred while executing the command');
if (interaction.deferred || interaction.replied) {
await interaction.editReply({ embeds: [errorEmbed] });
} else {
await interaction.reply({ embeds: [errorEmbed], ephemeral: true });
}
}
}
async function handleButtonInteraction(interaction: ButtonInteraction): Promise<void> {
const customId = interaction.customId;
try {
if (customId.startsWith('ticket_create_')) {
await handleTicketCreate(interaction);
} else if (customId === 'ticket_claim') {
await handleTicketClaim(interaction);
} else if (customId === 'ticket_close') {
await handleTicketClose(interaction);
} else if (customId === 'ticket_close_confirm') {
await handleTicketCloseConfirm(interaction);
} else if (customId === 'ticket_close_cancel') {
await handleTicketCloseCancel(interaction);
}
} catch (error) {
console.error('Error handling button interaction:', error);
await interaction.reply({
embeds: [EmbedFactory.createErrorEmbed('An error occurred while processing your request')],
ephemeral: true
});
}
}
async function handleTicketCreate(interaction: ButtonInteraction): Promise<void> {
await interaction.deferReply({ ephemeral: true });
const category = interaction.customId.replace('ticket_create_', '') as TicketCategory;
if (!interaction.guild) {
await interaction.editReply({
embeds: [EmbedFactory.createErrorEmbed('This command can only be used in a server')]
});
return;
}
const existingTicket = ticketManager.getUserTicket(interaction.user.id);
if (existingTicket) {
await interaction.editReply({
embeds: [EmbedFactory.createErrorEmbed(`You already have an open ticket: <#${existingTicket.channelId}>`)]
});
return;
}
const member = await interaction.guild.members.fetch(interaction.user.id);
const priorityRoleId = process.env.PRIORITY_ROLE_ID;
const isPriority = priorityRoleId ? member.roles.cache.has(priorityRoleId) : false;
const channel = await ticketManager.createTicket(
interaction.guild,
interaction.user,
category,
isPriority
);
if (channel) {
await interaction.editReply({
embeds: [EmbedFactory.createSuccessEmbed(`Your ticket has been created: ${channel}`)]
});
} else {
await interaction.editReply({
embeds: [EmbedFactory.createErrorEmbed('Failed to create ticket. Please contact an administrator.')]
});
}
}
async function handleTicketClaim(interaction: ButtonInteraction): Promise<void> {
await interaction.deferReply({ ephemeral: false });
if (!interaction.guild) {
await interaction.editReply({
embeds: [EmbedFactory.createErrorEmbed('This command can only be used in a server')]
});
return;
}
const member = await interaction.guild.members.fetch(interaction.user.id);
const staffRoleId = process.env.STAFF_ROLE_ID || '';
if (!member.roles.cache.has(staffRoleId)) {
await interaction.editReply({
embeds: [EmbedFactory.createErrorEmbed('Only staff members can claim tickets')]
});
return;
}
const success = await ticketManager.claimTicket(interaction.channelId, interaction.user);
if (success) {
await interaction.editReply({
embeds: [EmbedFactory.createSuccessEmbed(`Ticket claimed by ${interaction.user}`)]
});
const ticket = ticketManager.getTicket(interaction.channelId);
if (ticket) {
const updatedButtons = ButtonFactory.createTicketControlButtons(true);
const messages = await interaction.channel?.messages.fetch({ limit: 10 });
const botMessage = messages?.find(msg =>
msg.author.id === client.user?.id && msg.components.length > 0
);
if (botMessage) {
await botMessage.edit({ components: [updatedButtons] });
}
}
} else {
await interaction.editReply({