Skip to content
This repository was archived by the owner on May 15, 2025. It is now read-only.

Commit ac1d6fc

Browse files
committed
INTREACTION CREATE LISTENER RAHH
1 parent 5627ce1 commit ac1d6fc

File tree

4 files changed

+130
-31
lines changed

4 files changed

+130
-31
lines changed

src/commands/misc/ping.ts

Lines changed: 24 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
/* import { SlashCommandBuilder, EmbedBuilder, ChatInputCommandInteraction } from "discord.js";
2-
import { KOGBot } from "index.ts";
3-
import { SlashCommand } from "main.d.ts";
1+
import { SlashCommandBuilder, EmbedBuilder, ChatInputCommandInteraction, Colors } from "discord.js";
2+
import { KOGBot } from "index.js";
43

54
class PingCommand implements SlashCommand {
65
name = 'ping';
@@ -15,31 +14,28 @@ class PingCommand implements SlashCommand {
1514
}
1615

1716
async execute(interaction: ChatInputCommandInteraction): Promise<void> {
18-
try {
19-
const start = Date.now();
20-
await interaction.reply('Pinging...');
21-
const latency = Date.now() - start;
22-
23-
const embed = new EmbedBuilder()
24-
.setColor("#9033FF")
25-
.setTitle('Pong!')
26-
.setDescription(`Returned Successfully\nBot Latency: ${latency}ms\nAPI Latency: ${interaction.client.ws.ping}ms`);
27-
28-
await interaction.followUp({ embeds: [embed] });
29-
} catch (error) {
30-
console.error("Error in executing ping command:", error);
31-
32-
const errorEmbed = new EmbedBuilder()
33-
.setColor("#E73A3A")
34-
.setTitle("Error")
35-
.setDescription("An error occurred while executing the ping command.")
36-
.setTimestamp();
37-
38-
await interaction.reply({ embeds: [errorEmbed], ephemeral: true });
39-
}
17+
const ping = Math.floor(interaction.client.ws.ping);
18+
19+
const message = await interaction.reply({
20+
embeds: [
21+
new EmbedBuilder()
22+
.setTitle("Pong!")
23+
.setDescription(`🏓**Client latency** \`${ping}\` ms\n🌐 **Round-trip** \`Calculating...\``)
24+
.setColor(Colors.Blue)
25+
]
26+
});
27+
28+
const round = Math.floor(message.interaction.createdTimestamp - interaction.createdTimestamp);
29+
30+
await interaction.editReply({
31+
embeds: [
32+
new EmbedBuilder()
33+
.setTitle("Pong!")
34+
.setDescription(`🏓**Client latency**: \`${ping}\` ms\n🌐 **Round-trip**: \`${round}\` ms`)
35+
.setColor(Colors.Green)
36+
]
37+
});
4038
}
4139
}
4240

43-
export default PingCommand;
44-
45-
*/
41+
export default PingCommand;

src/events/interactionCreate.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import { Client, Interaction, Colors, EmbedBuilder } from "discord.js";
2+
import { KOGBot } from "index.js";
3+
4+
export default class ReadyEvent implements GatewayEvent {
5+
name: string = 'interactionCreate';
6+
once: boolean = false;
7+
8+
async code (kogBot: KOGBot, interaction: Interaction) {
9+
if (interaction.isChatInputCommand()) {
10+
const receivedCommand = interaction.commandName;
11+
const params = interaction.options;
12+
13+
const commandObj = kogBot.discord_client.commands.list.find(cmd => cmd.instance.name === receivedCommand);
14+
15+
if (!commandObj) {
16+
interaction.reply("Command not found bro");
17+
return;
18+
}
19+
20+
try {
21+
await commandObj.instance.execute(interaction);
22+
} catch (err) {
23+
const replied = interaction.replied || interaction.deferred;
24+
const replyData = {
25+
embeds: [
26+
new EmbedBuilder()
27+
.setTitle("Command Execution Error")
28+
.setDescription(`${err}`)
29+
.setColor(Colors.Red)
30+
.setFooter({
31+
text: `${interaction.guild?.name}`,
32+
iconURL: interaction.guild?.iconURL() as string
33+
})
34+
.setTimestamp()
35+
]
36+
};
37+
}
38+
}
39+
}
40+
}

src/events/ready.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ export default class ReadyEvent implements GatewayEvent {
1414
name: debug ? 'debug mode ⚠️' : 'with Knex.',
1515
type: debug ? ActivityType.Competing : ActivityType.Playing
1616
}]
17-
})
17+
});
18+
19+
console.log(`Logged in as ${client.user?.tag}!`);
1820
}
1921
}

src/index.ts

Lines changed: 63 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Client, IntentsBitField, Message, REST } from "discord.js";
1+
import { Client, IntentsBitField, REST, SlashCommandBuilder, Routes, SlashCommandSubcommandBuilder } from "discord.js";
22
import fs from 'fs';
33
import knex, { Knex } from "knex";
44
import toml from 'toml';
@@ -27,8 +27,11 @@ class DBot extends Client {
2727
this.REST.setToken(this.kogBot.environment.discord.client_token);
2828

2929
if (!kogBot.ci_workflow) {
30+
if (process.argv.includes('--deploy')) {
31+
this.commands.deploy().then(() => console.log("Commands deployed."));
32+
}
3033
this.gatewayEvents.listen().then(() => console.log("Listening to events."));
31-
this.login(this.kogBot.environment.discord.client_token);
34+
this.login(this.kogBot.environment.discord.client_token).then(() => console.log("Logged in, welcome!"));
3235
}
3336
}
3437

@@ -73,6 +76,64 @@ class DBot extends Client {
7376
}
7477
}
7578
}
79+
80+
commands = {
81+
list: [] as any[],
82+
83+
parse: async () => {
84+
const folderPath = join(__dirname, 'commands');
85+
const commandCategories = fs.readdirSync(folderPath);
86+
const commands = [];
87+
88+
for (const category of commandCategories) {
89+
const commandsPath = join(folderPath, category);
90+
const commandFiles = readdirSync(commandsPath).filter(file => file.endsWith('.js'));
91+
92+
for (const file of commandFiles) {
93+
const filePath = join(commandsPath, file);
94+
const commandClass = (await import(`file://${filePath}`)).default;
95+
const command: SlashCommand = new commandClass(this.kogBot);
96+
const slashCommand = new SlashCommandBuilder();
97+
98+
slashCommand.setName(command.name);
99+
slashCommand.setDescription(command.description);
100+
101+
if (command.subcommands.length > 0) {
102+
for (const subcommand of command.subcommands) {
103+
const sub = new SlashCommandSubcommandBuilder()
104+
.setName(subcommand.name)
105+
.setDescription(subcommand.description);
106+
for (const parameter of command.parameters) {
107+
sub.options.push(parameter);
108+
}
109+
slashCommand.addSubcommand(sub);
110+
111+
}
112+
113+
}
114+
115+
commands.push({
116+
builder: slashCommand,
117+
instance: command
118+
});
119+
}
120+
}
121+
return commands;
122+
},
123+
124+
deploy: async () => {
125+
this.commands.list = await this.commands.parse();
126+
if (process.argv.includes('--ci')) {
127+
console.log("Not deploying commands because of CI environment. Commands successfully parsed!")
128+
process.exit(0)
129+
}
130+
console.log(`Deploying ${this.commands.list.length} commands...`);
131+
await this.REST.put(
132+
Routes.applicationCommands(this.kogBot.environment.discord.client_id),
133+
{ body: this.commands.list.map(cmd => cmd.builder) }
134+
);
135+
}
136+
}
76137
}
77138

78139
class KOGBot_Client {

0 commit comments

Comments
 (0)