Réécriture complète en Typescript
This commit is contained in:
89
src/index.ts
Executable file
89
src/index.ts
Executable file
@@ -0,0 +1,89 @@
|
||||
// PACKAGES
|
||||
import { Client, Collection, GatewayIntentBits, REST, Routes, ChatInputCommandInteraction, AutocompleteInteraction, ButtonInteraction } from 'discord.js'
|
||||
import { Player } from 'discord-player'
|
||||
import path from 'path'
|
||||
import fs from 'fs'
|
||||
import 'dotenv/config'
|
||||
|
||||
export interface Command {
|
||||
name: string,
|
||||
description: string,
|
||||
data: any,
|
||||
autocompleteRun: (interaction: AutocompleteInteraction) => any,
|
||||
execute: (interaction: ChatInputCommandInteraction) => any
|
||||
}
|
||||
export interface Button {
|
||||
name: string,
|
||||
description: string,
|
||||
id: string,
|
||||
execute: (interaction: ButtonInteraction) => any
|
||||
}
|
||||
|
||||
declare module 'discord.js' {
|
||||
export interface Client {
|
||||
commands: Collection<unknown, Command>
|
||||
buttons: Collection<unknown, Button>
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// CLIENT INITIALIZATION
|
||||
let intents = [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMembers, GatewayIntentBits.GuildVoiceStates]
|
||||
const client = new Client({ intents })
|
||||
|
||||
// EVENTS HANDLING
|
||||
const eventFiles = fs.readdirSync(path.join(__dirname, './events')).filter(file => file.endsWith('.ts'))
|
||||
eventFiles.forEach(file => {
|
||||
let event = require(path.join(__dirname, './events', file))
|
||||
if (event.once) client.once(event.name, (...args) => { event.execute(...args) })
|
||||
else client.on(event.name, (...args) => { event.execute(...args) })
|
||||
})
|
||||
|
||||
// COMMANDS HANDLING
|
||||
client.commands = new Collection()
|
||||
|
||||
const commandFolders = fs.readdirSync(path.join(__dirname, './commands'))
|
||||
commandFolders.forEach(folder => {
|
||||
let folderPath = path.join(__dirname, './commands', folder)
|
||||
let commandFiles = fs.readdirSync(folderPath).filter(file => file.endsWith('.ts'))
|
||||
commandFiles.forEach(file => {
|
||||
let command = require(path.join(folderPath, file))
|
||||
if ('data' in command && 'execute' in command) {
|
||||
const commandData = command.data.toJSON()
|
||||
if (commandData) client.commands.set(commandData.name, command)
|
||||
} else console.log(`[WARNING] The command at ${`${folderPath}/${file}`} is missing a required "data" or "execute" property.`)
|
||||
})
|
||||
})
|
||||
|
||||
// COMMANDS REGISTERING
|
||||
const rest = new REST({ version: '10' }).setToken(process.env.DISCORD_TOKEN as string)
|
||||
async () => {
|
||||
try { await rest.put(Routes.applicationCommands(process.env.DISCORD_APP_ID as string), { body: client.commands }) }
|
||||
catch (error) { console.error(error) }
|
||||
}
|
||||
|
||||
// BUTTONS HANDLING
|
||||
client.buttons = new Collection()
|
||||
|
||||
const buttonFiles = fs.readdirSync(path.join(__dirname, './buttons')).filter(file => file.endsWith('.ts'))
|
||||
buttonFiles.forEach(file => {
|
||||
let button = require(path.join(__dirname, './buttons', file))
|
||||
if ('id' in button && 'execute' in button) client.buttons.set(button.id, button)
|
||||
else console.log(`[WARNING] The button ${file} is missing a required "id" or "execute" property.`)
|
||||
})
|
||||
|
||||
|
||||
// PLAYER INITIALIZATION
|
||||
const player = new Player(client)
|
||||
|
||||
// PLAYER EVENTS HANDLING
|
||||
const eventPlayerFiles = fs.readdirSync(path.join(__dirname, './eventsPlayer')).filter(file => file.endsWith('.ts'))
|
||||
eventPlayerFiles.forEach(async file => {
|
||||
let event = await import(path.join(__dirname, './eventsPlayer', file))
|
||||
if (event.default.name === 'debug') return
|
||||
player.events.on(event.default.name, (...args: any[]) => event.default.execute(...args))
|
||||
})
|
||||
|
||||
|
||||
// LAUNCH
|
||||
client.login()
|
||||
Reference in New Issue
Block a user