feat(cmd/deploy): adding the deploy command
This commit is contained in:
parent
bca3bd0c2a
commit
2b02553a4c
6 changed files with 250 additions and 0 deletions
15
README.md
Normal file
15
README.md
Normal file
|
|
@ -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.
|
||||||
24
package.json
Normal file
24
package.json
Normal file
|
|
@ -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"
|
||||||
|
}
|
||||||
|
}
|
||||||
9
src/events/client/ready.ts
Normal file
9
src/events/client/ready.ts
Normal file
|
|
@ -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`);
|
||||||
|
},
|
||||||
|
};
|
||||||
30
src/events/interaction/interaction.ts
Normal file
30
src/events/interaction/interaction.ts
Normal file
|
|
@ -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: `<a:error:1398985025688436797> | ${interaction.commandName} seems have a problem, thanks report that to the support (After Print)`,
|
||||||
|
flags: MessageFlags.Ephemeral
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
await interaction.reply({
|
||||||
|
content: `<a:error:1398985025688436797> | ${interaction.commandName} seems have a problem, thanks report that to the support (Before Print)`,
|
||||||
|
flags: MessageFlags.Ephemeral
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
};
|
||||||
121
src/index.ts
Normal file
121
src/index.ts
Normal file
|
|
@ -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.`);
|
||||||
|
}
|
||||||
|
});
|
||||||
51
src/internal/deploy-command.ts
Normal file
51
src/internal/deploy-command.ts
Normal file
|
|
@ -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);
|
||||||
|
}
|
||||||
|
})();
|
||||||
Loading…
Add table
Add a link
Reference in a new issue