Intégration MongoDB + Fix export et amp

This commit is contained in:
Angels-dev
2024-02-29 00:24:21 +01:00
parent 397a45e371
commit 94c7fc1c98
63 changed files with 1608 additions and 1161 deletions

View File

@@ -1,21 +1,22 @@
// PACKAGES
import { Client, Collection, GatewayIntentBits, REST, Routes, ChatInputCommandInteraction, AutocompleteInteraction, ButtonInteraction } from 'discord.js'
import { Player } from 'discord-player'
import { connection } from 'mongoose'
import path from 'path'
import fs from 'fs'
import 'dotenv/config'
export interface Command {
name: string,
description: string,
data: any,
autocompleteRun: (interaction: AutocompleteInteraction) => any,
name: string
description: string
data: any
autocompleteRun: (interaction: AutocompleteInteraction) => any
execute: (interaction: ChatInputCommandInteraction) => any
}
export interface Button {
name: string,
description: string,
id: string,
name: string
description: string
id: string
execute: (interaction: ButtonInteraction) => any
}
@@ -23,6 +24,8 @@ declare module 'discord.js' {
export interface Client {
commands: Collection<unknown, Command>
buttons: Collection<unknown, Button>
disco: { interval: NodeJS.Timeout }
rss: { interval: NodeJS.Timeout }
}
}
@@ -30,59 +33,78 @@ declare module 'discord.js' {
// 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 commands = [] as Command[]
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); commands.push(commandData) }
} 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: 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)
// COMMANDS HANDLING
let commands = [] as Command[]
let commandsParsed = 0
let commandsTotal = 0
let 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'))
commandsTotal += commandFiles.length
commandFiles.forEach(async file => {
let command = await import(path.join(folderPath, file))
command = command.default
if ('data' in command && 'execute' in command) {
let commandData = command.data.toJSON()
if (commandData) { client.commands.set(commandData.name, command); commands.push(commandData) }
} else console.log(`[WARNING] The command at ${`${folderPath}/${file}`} is missing a required "data" or "execute" property.`)
commandsParsed++
if (commandsParsed === commandsTotal) {
// COMMANDS REGISTRATION
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: commands }) }
catch (error) { console.error(error) }
})()
}
})
})
// BUTTONS HANDLING
let buttonFiles = fs.readdirSync(path.join(__dirname, './buttons')).filter(file => file.endsWith('.ts'))
buttonFiles.forEach(async file => {
let button = await import(path.join(__dirname, './buttons', file))
button = button.default
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.`)
})
// EVENTS HANDLING
let eventClientFiles = fs.readdirSync(path.join(__dirname, './events/client')).filter(file => file.endsWith('.ts'))
eventClientFiles.forEach(async file => {
let event = await import(path.join(__dirname, './events/client', file))
event = event.default
if (event.once) client.once(event.name, (...args) => { event.execute(...args) })
else client.on(event.name, (...args) => { event.execute(...args) })
})
// 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))
let eventsPlayer = fs.readdirSync(path.join(__dirname, './events/player')).filter(file => file.endsWith('.ts'))
eventsPlayer.forEach(async file => {
let event = await import(path.join(__dirname, './events/player', file))
event = event.default
if (event.name === 'debug') return
player.events.on(event.name, (...args: any[]) => event.execute(...args))
})
// MONGO EVENTS HANDLING
let eventsMongo = fs.readdirSync(path.join(__dirname, './events/mongo')).filter(file => file.endsWith('.ts'))
eventsMongo.forEach(async file => {
let event = await import(path.join(__dirname, './events/mongo', file))
event = event.default
if (event.once) connection.once(event.name, (...args) => { event.execute(...args, client) })
else connection.on(event.name, (...args) => { event.execute(...args, client) })
})