basic command handler (messageCreate fixme)

This commit is contained in:
2021-08-23 15:05:17 +02:00
parent 4f82c6efdd
commit 2b5d3fa0b6
7 changed files with 519 additions and 507 deletions

View File

@@ -1,38 +0,0 @@
const { Client, Intents } = require('discord.js');
// const cfg = require('./cfg.json');
// const fs = require('fs');
require('dotenv').config();
const cl = new Client({ intents: [Intents.FLAGS.GUILDS] });
// ======== ======== ======== ======== COMMAND HANDLER
// const cmds = [];
// const cmdsFls = fs
// .readdirSync(`./commands`)
// .filter((file) => file.endsWith(`.js`));
//
// for (const Fl of cmdsFls) {
// const cmd = require(`./commands/${Fl}`);
// cmds.push(cmd.data.toJSON());
// }
// ======== ======== ======== ======== INTERACTIONS (don't work - require slash command registration)
// cl.on('interactionCreate', async (interaction) => {
// if (!interaction.isCommand()) return;
//
// const { commandName } = interaction;
//
// if (commandName === 'ping') {
// await interaction.reply('Pong!');
// } else if (commandName === 'beep') {
// await interaction.reply('Boop!');
// } else if (commandName === 'server') {
// await interaction.reply(`This server's name is: ${interaction.guild.name}`);
// }
// });
cl.once('ready', () => {
console.log(`bot ready`);
});
cl.login(process.env.token);

View File

@@ -2,7 +2,7 @@
"name": "discord-bot",
"version": "1.0.0",
"description": "gractwo-bot is a Discord bot written in JavaScript, using node.js and discord.js.\r It has been created for and only serves the Gractwo Discord server. It is hosted using Heroku.",
"main": "index.js",
"main": "src/index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
@@ -10,7 +10,7 @@
"type": "git",
"url": "git+https://github.com/Gractwo/discord-bot.git"
},
"author": "",
"author": "github.com/jakubmanczak",
"license": "UNLICENSED",
"bugs": {
"url": "https://github.com/Gractwo/discord-bot/issues"

6
src/cmds/ping.js Normal file
View File

@@ -0,0 +1,6 @@
module.exports = {
name: 'ping',
execute(msg) {
msg.channel.send(`pong`);
},
};

View File

Before

Width:  |  Height:  |  Size: 16 KiB

After

Width:  |  Height:  |  Size: 16 KiB

44
src/index.js Normal file
View File

@@ -0,0 +1,44 @@
const { Client, Intents, Collection } = require('discord.js');
const cfg = require('./cfg.json');
const fs = require('fs');
require('dotenv').config();
const cl = new Client({ intents: [Intents.FLAGS.GUILDS] });
cl.cmds = new Collection();
const cmdsFls = fs
.readdirSync(`./src/cmds`)
.filter((file) => file.endsWith(`.js`));
for (const Fl of cmdsFls) {
const cmd = require(`./cmds/${Fl}`);
console.log(`command: ` + cmd.name);
cl.cmds.set(cmd.name, cmd);
}
// FIXME: messageCreate is never invoked for some reason, disabling
// literally all of the bot's functionality
cl.on('messageCreate', (msg) => {
console.log('a message has been received');
if (!msg.content.startsWith(cfg.prefix) || msg.author.bot) return;
const args = msg.content.slice(cfg.prefix.length).trim().split(/ +/);
const cmdName = args.shift().toLowerCase();
if (!cl.cmds.has(cmdName)) return;
const cmd = cl.cmds.get(cmdName);
try {
cmd.execute(msg);
} catch (error) {
console.error(error);
message.reply(`the above error occured while trying to execute ${cmd}`);
}
});
cl.once('ready', () => {
console.clear();
console.log(`bot ready; logged in as ${cl.user.tag}\n--`);
cl.user.setActivity('.ping', { type: 'LISTENING' });
});
cl.login(process.env.token);