68 lines
2.4 KiB
TypeScript
68 lines
2.4 KiB
TypeScript
import 'dotenv/config.js';
|
|
import fs from 'node:fs';
|
|
import path from 'node:path';
|
|
import { ApplicationCommand, Client, Collection, GatewayIntentBits, REST, Routes } from 'discord.js';
|
|
import { createGasPriceChecker } from './gasPriceChecker';
|
|
|
|
const clientId = process.env.DISCORD_CLIENT || "";
|
|
const token = process.env.DISCORD_BOT_TOKEN || "";
|
|
|
|
export class DiscordClient extends Client {
|
|
public commands: Collection<string, any> = new Collection();
|
|
}
|
|
|
|
// Create a new Discord client
|
|
const client = new DiscordClient({ intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMessages] });
|
|
|
|
const commands: ApplicationCommand[] = [];
|
|
const commandsPath = path.join(__dirname, 'commands');
|
|
const commandFiles = fs.readdirSync(commandsPath).filter(file => file.endsWith('.js'));
|
|
|
|
for (const file of commandFiles) {
|
|
const filePath = path.join(commandsPath, file);
|
|
const command = require(filePath);
|
|
// Set a new item in the Collection with the key as the command name and the value as the exported module
|
|
if ('data' in command && 'execute' in command) {
|
|
commands.push(command.data.toJSON())
|
|
client.commands.set(command.data.name, command);
|
|
} else {
|
|
console.log(`[WARNING] The command at ${filePath} is missing a required "data" or "execute" property.`);
|
|
}
|
|
}
|
|
|
|
const eventsPath = path.join(__dirname, 'events');
|
|
const eventFiles = fs.readdirSync(eventsPath).filter(file => file.endsWith('.js'));
|
|
|
|
for (const file of eventFiles) {
|
|
const filePath = path.join(eventsPath, file);
|
|
const event = require(filePath);
|
|
if (event.once) {
|
|
client.once(event.name, (...args) => event.execute(...args));
|
|
} else {
|
|
client.on(event.name, (...args) => event.execute(...args));
|
|
}
|
|
}
|
|
|
|
console.log('Booting up discord bot')
|
|
|
|
// Log in to Discord
|
|
client.login(process.env.DISCORD_BOT_TOKEN)
|
|
.then(async () => {
|
|
console.log(`Started refreshing ${commands.length} global application (/) commands.`);
|
|
|
|
|
|
// Construct and prepare an instance of the REST module
|
|
const rest = new REST({ version: '10' }).setToken(token);
|
|
|
|
// The put method is used to fully refresh all commands in the guild with the current set
|
|
const data = await rest.put(
|
|
Routes.applicationCommands(clientId),
|
|
{ body: commands },
|
|
) as ApplicationCommand[];
|
|
|
|
console.log(`Successfully reloaded ${data.length} global application (/) commands.`);
|
|
})
|
|
.then(() => {
|
|
// Start the gas price checker
|
|
createGasPriceChecker(client);
|
|
}); |