Skip to content

Commit

Permalink
revert everything from ESM to CJS + added registerCommands to refresh…
Browse files Browse the repository at this point in the history
… bot commands
  • Loading branch information
BodomBeach committed May 23, 2024
1 parent 02362a6 commit e44e204
Show file tree
Hide file tree
Showing 11 changed files with 252 additions and 186 deletions.
339 changes: 191 additions & 148 deletions package-lock.json

Large diffs are not rendered by default.

6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@
"author": "Nicolas Bertin",
"license": "MIT",
"dependencies": {
"discord.js": "^14.11.0",
"@discordjs/builders": "^1.8.1",
"@discordjs/rest": "^2.3.0",
"discord-api-types": "^0.37.84",
"discord.js": "^14.15.2",
"dotenv": "^16.3.1",
"path": "^0.12.7",
"puppeteer": "^15.5.0"
Expand All @@ -20,7 +23,6 @@
"dev": "nodemon src/app.js",
"start": "node src/app.js"
},
"type": "module",
"devDependencies": {
"nodemon": "^3.1.0"
}
Expand Down
8 changes: 4 additions & 4 deletions src/app.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { } from 'dotenv/config';
import fs from 'fs';
import { Client, GatewayIntentBits } from 'discord.js';
require('dotenv').config();
const fs = require('fs');
const { Client, GatewayIntentBits } = require('discord.js');

// Create a new Client with the Guilds intent
const client = new Client({ intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMembers] });
Expand All @@ -12,7 +12,7 @@ const events = fs

for (let event of events) {

const eventFile = await import(`./events/${event}`);
const eventFile = require(`./events/${event}`);

if (eventFile.once)
client.once(eventFile.name, (...args) => {
Expand Down
6 changes: 3 additions & 3 deletions src/events/commands/archive.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// TODO -> Archive by transforming channel into a thread in the 'archives' channel

import { SlashCommandBuilder } from 'discord.js';
import {archive}from '../../utils/archive.js';
const { SlashCommandBuilder } = require('discord.js');
const {archive} = require('../../utils/archive.js');

// Creates an Object in JSON with the data required by Discord's API to create a SlashCommand
const create = () => {
Expand All @@ -27,4 +27,4 @@ const create = () => {
};
};

export { create, invoke };
module.exports = { create, invoke };
6 changes: 3 additions & 3 deletions src/events/commands/balise.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { SlashCommandBuilder, AttachmentBuilder, EmbedBuilder } from 'discord.js';
import puppeteer from 'puppeteer';
const { SlashCommandBuilder, AttachmentBuilder, EmbedBuilder } = require('discord.js');
const puppeteer = require('puppeteer');

const url = 'http://www.murblanc.org/sthil';

Expand Down Expand Up @@ -65,4 +65,4 @@ const getScreenhot = async (selector) => {
return screenshot;
}

export { create, invoke };
module.exports = { create, invoke };
6 changes: 4 additions & 2 deletions src/events/interactionCreate.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ const name = 'interactionCreate';
async function invoke(interaction) {
// Check if the interaction is a command and call the invoke method in the corresponding file
if (interaction.isChatInputCommand())
(await import(`./commands/${interaction.commandName}.js`)).invoke(interaction);
(await require(`./commands/${interaction.commandName}.js`)).invoke(interaction);
}

export { once, name, invoke };
module.exports = { once, name, invoke };


29 changes: 13 additions & 16 deletions src/events/ready.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import fs from 'fs';
import {serverStats} from '../jobs/serverStats.js';
import {channelCleanup} from '../jobs/channelCleanup.js';
const fs = require('fs');
const { serverStats } = require('../jobs/serverStats.js');
const { channelCleanup } = require('../jobs/channelCleanup.js');
const registerCommands = require('../utils/registerCommands.js');

const once = true;
const name = 'ready';
Expand All @@ -11,21 +12,17 @@ async function invoke(client) {
setInterval(() => {channelCleanup(client)}, process.env.CHANNEL_CLEANUP_INTERVAL || 3600000); // every hour
setInterval(() => {serverStats(client)}, process.env.STATS_INTERVAL || 600000); // every 10 min

const commands = fs
.readdirSync('src/events/commands')
.filter((file) => file.endsWith('.js'))
.map((file) => file.slice(0, -3));
const commands = []
const commandFiles = fs.readdirSync('./src/events/commands').filter(file => file.endsWith('.js'));

const commandsArray = [];

for (let command of commands) {
const commandFile = await import(`./commands/${command}.js`);
commandsArray.push(commandFile.create());
}

client.application.commands.set(commandsArray);
for (const file of commandFiles) {
const command = require(`./commands/${file}`);
commands.push(command.create());
}
registerCommands(client, commands)

console.log(`Successfully logged to Discord as ${client.user.tag}!`);

}

export { once, name, invoke };
module.exports = { once, name, invoke };
8 changes: 5 additions & 3 deletions src/jobs/channelCleanup.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {archive} from '../utils/archive.js';
const {archive} = require('../utils/archive.js');

export function channelCleanup(client) {
function channelCleanup(client) {


console.log('===== starting scheduled channel cleanup =====');
Expand Down Expand Up @@ -37,4 +37,6 @@ export function channelCleanup(client) {
}
});
});
}
}

module.exports = { channelCleanup };
6 changes: 4 additions & 2 deletions src/jobs/serverStats.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { EmbedBuilder } from 'discord.js';
const { EmbedBuilder } = require('discord.js');

export async function serverStats(client) {
async function serverStats(client) {
console.log('===== starting scheduled server stats update =====');
const guild = client.guilds.cache.get(process.env.GUILD_ID);

Expand Down Expand Up @@ -61,3 +61,5 @@ export async function serverStats(client) {
} ;
console.log('Server stats updated');
}

module.exports = { serverStats };
7 changes: 4 additions & 3 deletions src/utils/archive.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
// TODO -> Archive by transforming channel into a thread in the 'archives' channel

export async function archive(channel) {
async function archive(channel) {
const archiveCategories = channel.guild.channels.cache.filter(channel => channel.type === 4 && channel.name.slice(0,11).toLowerCase() === '📁archives_');
let available_archive = archiveCategories.find(cat => cat.children.cache.size < 50)

Expand All @@ -16,4 +15,6 @@ export async function archive(channel) {
console.log('Created new archive category');
channel.setParent(available_archive)
}
}
}

module.exports = { archive };
17 changes: 17 additions & 0 deletions src/utils/registerCommands.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
const { REST } = require('@discordjs/rest');
const { Routes } = require('discord-api-types/v9');

module.exports = async (client, commands) => {
if(!commands || commands.length == 0) return console.log("No commands to register")
const rest = new REST({ version: '9' }).setToken(process.env.BOT_TOKEN);
try {
await rest.put(
Routes.applicationGuildCommands(client.user.id, process.env.GUILD_ID),
{ body: commands },
);

console.log(`Successfully reloaded application (/) ${commands.length} commands.`);
} catch (error) {
console.error(error);
}
}

0 comments on commit e44e204

Please sign in to comment.