Skip to content

Commit

Permalink
optimised
Browse files Browse the repository at this point in the history
  • Loading branch information
LucasB25 committed May 19, 2024
1 parent 80d6ce1 commit b0e869b
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 37 deletions.
Binary file modified prisma/aikouticket.db
Binary file not shown.
5 changes: 0 additions & 5 deletions prisma/example.mongodb.schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,4 @@ datasource db {
model tickets {
guildId String @id @map("_id")
selectMenuOptions String
}

model users {
userId String @id @map("_id")
activeTickets Int
}
5 changes: 0 additions & 5 deletions prisma/example.postgresql.schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,4 @@ datasource db {
model tickets {
guildId String @id
selectMenuOptions String
}

model users {
userId String @id
activeTickets Int
}
4 changes: 0 additions & 4 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,4 @@ datasource db {
model tickets {
guildId String @id
selectMenuOptions String
}

model users {
userId String @id
}
71 changes: 48 additions & 23 deletions src/commands/tickets/panel.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {
ActionRowBuilder,
CommandInteraction,
EmbedBuilder,
StringSelectMenuBuilder,
StringSelectMenuOptionBuilder,
} from 'discord.js';
Expand Down Expand Up @@ -38,10 +39,42 @@ export default class PanelCommand extends Command {
async run(client: Bot, interaction: CommandInteraction): Promise<void> {
await interaction.deferReply({ ephemeral: true });

const configFile = fs.readFileSync('./config.yml', 'utf8');
const config = YAML.parse(configFile);
let config;
try {
const configFile = fs.readFileSync('./config.yml', 'utf8');
config = YAML.parse(configFile);
} catch (error) {
console.error('Error reading or parsing config file:', error);
await interaction.editReply({
content:
'There was an error loading the configuration. Please contact the administrator.',
});
return;
}

const panelEmbed = this.createPanelEmbed(client);
const selectMenu = this.createSelectMenu(config.ticketCategories, config.menuPlaceholder);

const actionRowsMenus = new ActionRowBuilder<StringSelectMenuBuilder>().addComponents(
selectMenu
);

try {
await interaction.editReply({ content: 'Sending the panel in this channel...' });

await interaction.channel.send({ embeds: [panelEmbed], components: [actionRowsMenus] });

await this.saveTicketData(interaction.guild.id, selectMenu.options);
} catch (error) {
console.error('Error sending the panel or saving ticket data:', error);
await interaction.editReply({
content: 'There was an error sending the panel. Please contact the administrator.',
});
}
}

const panelEmbed = client
createPanelEmbed(client: Bot): EmbedBuilder {
return client
.embed()
.setColor(this.client.color)
.setTitle('AikouTicket')
Expand All @@ -52,9 +85,9 @@ export default class PanelCommand extends Command {
'https://cdn.discordapp.com/attachments/1109764526552915988/1136666715078533303/image.png?ex=6647695f&is=664617df&hm=ec6a3e7de621daf0813e7a70c6ec7b2c9741bad8450172d356f85f28273610b2&'
)
.setTimestamp();
}

const ticketCategories = config.ticketCategories;

createSelectMenu(ticketCategories: any, placeholder: string): StringSelectMenuBuilder {
const options = Object.keys(ticketCategories).map(customId => {
const category = ticketCategories[customId];
const option = new StringSelectMenuOptionBuilder()
Expand All @@ -69,28 +102,20 @@ export default class PanelCommand extends Command {
return option;
});

const selectMenu = new StringSelectMenuBuilder()
return new StringSelectMenuBuilder()
.setCustomId('categoryMenu')
.setPlaceholder(config.menuPlaceholder)
.setPlaceholder(placeholder)
.setMinValues(1)
.setMaxValues(1)
.addOptions(options);
}

const actionRowsMenus = new ActionRowBuilder<StringSelectMenuBuilder>().addComponents(
selectMenu
);

await interaction.editReply({ content: 'Sending the panel in this channel...' });

await interaction.channel
.send({ embeds: [panelEmbed], components: [actionRowsMenus] })
.then(async () => {
await this.client.prisma.tickets.create({
data: {
guildId: interaction.guild.id,
selectMenuOptions: JSON.stringify(options),
},
});
});
async saveTicketData(guildId: string, options: any): Promise<void> {
await this.client.prisma.tickets.create({
data: {
guildId,
selectMenuOptions: JSON.stringify(options),
},
});
}
}

0 comments on commit b0e869b

Please sign in to comment.