From 2b02553a4c7b28051a9159492cac8869b69114de Mon Sep 17 00:00:00 2001 From: Raphael Date: Sun, 27 Jul 2025 18:28:19 +0200 Subject: [PATCH] feat(cmd/deploy): adding the deploy command --- README.md | 15 ++++ package.json | 24 +++++ src/events/client/ready.ts | 9 ++ src/events/interaction/interaction.ts | 30 +++++++ src/index.ts | 121 ++++++++++++++++++++++++++ src/internal/deploy-command.ts | 51 +++++++++++ 6 files changed, 250 insertions(+) create mode 100644 README.md create mode 100644 package.json create mode 100644 src/events/client/ready.ts create mode 100644 src/events/interaction/interaction.ts create mode 100644 src/index.ts create mode 100644 src/internal/deploy-command.ts diff --git a/README.md b/README.md new file mode 100644 index 0000000..74c0e67 --- /dev/null +++ b/README.md @@ -0,0 +1,15 @@ +# dsc_bot + +To install dependencies: + +```bash +bun install +``` + +To run: + +```bash +bun run index.ts +``` + +This project was created using `bun init` in bun v1.2.18. [Bun](https://bun.sh) is a fast all-in-one JavaScript runtime. diff --git a/package.json b/package.json new file mode 100644 index 0000000..4175245 --- /dev/null +++ b/package.json @@ -0,0 +1,24 @@ +{ + "name": "dsc_bot", + "module": "./src/index.ts", + "type": "module", + "scripts": { + "dev": "bun ./src/internal/deploy-command.ts && bun src/index.ts", + "build": "bun build src/index.ts --outdir dist --target bun" + }, + "private": true, + "devDependencies": { + "@types/bun": "latest", + "eslint": "^9.31.0", + "prisma": "^6.12.0" + }, + "peerDependencies": { + "typescript": "^5" + }, + "dependencies": { + "@prisma/client": "^6.12.0", + "discord.js": "^14.21.0", + "dotenv": "^17.2.0", + "mariadb": "^3.4.5" + } +} diff --git a/src/events/client/ready.ts b/src/events/client/ready.ts new file mode 100644 index 0000000..302dac8 --- /dev/null +++ b/src/events/client/ready.ts @@ -0,0 +1,9 @@ +import { Events } from 'discord.js' + +export default { + name: Events.ClientReady, + once: true, + execute(client) { + console.log(`✅ | ${client.user.username} is now running under TTS bot`); + }, +}; diff --git a/src/events/interaction/interaction.ts b/src/events/interaction/interaction.ts new file mode 100644 index 0000000..206a329 --- /dev/null +++ b/src/events/interaction/interaction.ts @@ -0,0 +1,30 @@ +import { Events, MessageFlags } from 'discord.js' + +export default { + name: Events.InteractionCreate, + async execute(interaction) { + if (!interaction.isChatInputCommand()) + return; + const command = interaction.client.commands.get(interaction.commandName); + if (!command) { + console.error(`⚠️ | Can't execute ${interaction.commandName}`); + return; + } + try { + await command.execute(interaction); + } catch (error) { + console.error(`⚠️ | Error when occured this command ${interaction.commandName}\n\t${error}`); + if (interaction.replied || interaction.deferred) { + await interaction.followUp({ + content: ` | ${interaction.commandName} seems have a problem, thanks report that to the support (After Print)`, + flags: MessageFlags.Ephemeral + }); + } else { + await interaction.reply({ + content: ` | ${interaction.commandName} seems have a problem, thanks report that to the support (Before Print)`, + flags: MessageFlags.Ephemeral + }); + } + } + }, +}; diff --git a/src/index.ts b/src/index.ts new file mode 100644 index 0000000..efa278e --- /dev/null +++ b/src/index.ts @@ -0,0 +1,121 @@ +import fs from 'node:fs' +import path from 'node:path' +import 'dotenv/config'; +import { Client, Collection, GatewayIntentBits } from 'discord.js'; +import { PrismaClient } from '@prisma/client'; +import { deployCommands } from './internal/deploy-commands.ts'; + +const prisma = new PrismaClient(); + +const client = new Client({ + intents: [ + GatewayIntentBits.Guilds, + GatewayIntentBits.GuildMessages, + GatewayIntentBits.MessageContent, + GatewayIntentBits.GuildMembers, + ], +}); + +client.login(process.env.DSC_TOKEN); + +client.commands = new Collection(); + +const commandFolderPath = path.join(__dirname, 'commands'); +const commandFolders = fs.readdirSync(commandFolderPath); + +console.log(`\n🔍 | Commands search:`) +for (const folder of commandFolders) { + const commandsPath = path.join(commandFolderPath, folder); + const commandFiles = fs + .readdirSync(commandsPath) + .filter(file => file.endsWith('.js') || file.endsWith('.ts')); + for (const file of commandFiles) { + const fullCommandPath = path.join(commandsPath, file); + try { + const commandModule = await import(fullCommandPath); + const command = commandModule.default || commandModule; + if ('data' in command && 'execute' in command) { + client.commands.set(command.data.name, command); + console.log(`\t✅ | ${command.data.name}`); + } + } catch (err) { + console.error(`\t⚠️ | Command at ${file}\n\t\t(${err}).`); + } + } +} +console.log('\n\n'); + +const eventFolderPath = path.join(__dirname, 'events'); +const eventFolders = fs.readdirSync(eventFolderPath); + +console.log(`\n🔍 | Events search:`) +for (const folder of eventFolders) { + const eventsPath = path.join(eventFolderPath, folder); + const eventFiles = fs + .readdirSync(eventsPath) + .filter(file => file.endsWith('.js') || file.endsWith('.ts')); + for (const file of eventFiles) { + const fullEventPath = path.join(eventsPath, file); + try { + const eventModule = await import(fullEventPath); + const event = eventModule.default || eventModule; + if ('name' in event && 'execute' in event) { + if (event.once) { + client.once(event.name, (...args) => event.execute(...args)); + } else { + client.on(event.name, (...args) => event.execute(...args)); + } + console.log(`\t✅ | ${event.name}`); + } + } catch (err) { + console.error(`\t⚠️ | event at ${file}\n\t\t(${err}).`); + } + } +} +console.log('\n\n'); + +client.once('ready', async () => { + console.log(`🤖 | Connecté en tant que ${client.user?.tag}`); + + for (const [guildId, guild] of client.guilds.cache) { + await prisma.guild.upsert({ + where: { + id: guildId + }, + update: {}, + create: { + id: guildId + } + }); + + const members = await guild.members.fetch(); + + for (const [memberId, member] of members) { + await prisma.user.upsert({ + where: { + id: memberId + }, + update: {}, + create: { + id: memberId + } + }); + + await prisma.guildUser.upsert({ + where: { + userId_guildId: { + userId: memberId, + guildId: guildId + } + }, + update: {}, + create: { + userId: memberId, + guildId: guildId + } + }); + } + + console.log(`✅ | Guild ${guild.name} synchronisée avec ${members.size} membres.`); + } +}); diff --git a/src/internal/deploy-command.ts b/src/internal/deploy-command.ts new file mode 100644 index 0000000..f036b50 --- /dev/null +++ b/src/internal/deploy-command.ts @@ -0,0 +1,51 @@ +import { REST, Routes } from 'discord.js' +import { pathToFileURL } from 'node:url'; +import { Client, Collection, Events, GatewayIntentBits, MessageFlags } from 'discord.js'; +import 'dotenv/config'; +import fs from 'node:fs' +import path from 'node:path' + +const client = new Client({ + intents: [ + GatewayIntentBits.Guilds + ] +}); + +client.commands = new Collection(); + +const commands = []; + +const foldersPath = path.join(__dirname, '../commands'); +const commandFolders = fs.readdirSync(foldersPath); + +for (const folder of commandFolders) { + const commandsPath = path.join(foldersPath, folder); + const commandFiles = fs.readdirSync(commandsPath).filter(file => file.endsWith('.ts') || file.endsWith('.js')); + for (const file of commandFiles) { + const filesPath = path.join(commandsPath, file); + const commandModule = await import(filesPath); + const command = commandModule.default || commandModule; + if ('data' in command && 'execute' in command) { + commands.push(command.data.toJSON()); + } else { + console.log(`⚠️ | A Command is missing a required "data" or "execute" property.`); + } + } +} + +const rest = new REST().setToken(process.env.DSC_TOKEN); + +(async () => { + try { + console.log(`🔍 | ${commands.length} commands found.`); + const data = await rest.put( + Routes.applicationCommands(process.env.CLIENT_ID), + { + body: commands + }, + ); + console.log(`✅ | ${data.length} is now reloaded`); + } catch (error) { + console.error(error); + } +})();