From 047ab5c65a6240e6190427c26c411a0d2c196d93 Mon Sep 17 00:00:00 2001 From: LerssiUkko Date: Mon, 6 Sep 2021 08:38:13 +0300 Subject: [PATCH] Add event handler and prefix command handler --- events/messageCreate.js | 14 ++++++++++++++ index.js | 30 ++++++++++++++++++++++++++++++ komennot/test.js | 9 +++++++++ 3 files changed, 53 insertions(+) create mode 100644 events/messageCreate.js create mode 100644 komennot/test.js diff --git a/events/messageCreate.js b/events/messageCreate.js new file mode 100644 index 0000000..326e26c --- /dev/null +++ b/events/messageCreate.js @@ -0,0 +1,14 @@ +module.exports = (client, message) => { + if (message.author.bot) return; + + if (message.content.indexOf(client.config.prefix) !== 0) return; + + const args = message.content.slice(client.config.prefix.length).trim().split(/ +/g); + const command = args.shift().toLowerCase(); + + const cmd = client.komennot.get(command); + + if (!cmd) return; + + cmd.run(client, message, args); + }; \ No newline at end of file diff --git a/index.js b/index.js index 122fe21..f9c9b3d 100644 --- a/index.js +++ b/index.js @@ -7,6 +7,7 @@ var XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest; const { token, prefix } = require("./config.json"); const meme = require('./commands/meme'); const userinfo = require('./commands/userinfo'); +const config = require("./config.json"); const client = new Client({ intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES, Intents.FLAGS.DIRECT_MESSAGES] }); @@ -24,6 +25,34 @@ client.on("ready", () => { }); +//event handling and prefix command handling +client.config = config; + +fs.readdir("./events/", (err, files) => { + if (err) return console.error(err); + files.forEach(file => { + const event = require(`./events/${file}`); + let eventName = file.split(".")[0]; + client.on(eventName, event.bind(null, client)); + }); +}); + + +client.komennot = new Collection(); + +fs.readdir("./komennot/", (err, files) => { + if (err) return console.error(err); + files.forEach(file => { + if (!file.endsWith(".js")) return; + let props = require(`./komennot/${file}`); + let commandName = file.split(".")[0]; + console.log(`Attempting to load command ${commandName}`); + client.komennot.set(commandName, props); + }); + }); + + +//slash command handling client.on('interactionCreate', async interaction => { if (!interaction.isCommand()) return; @@ -39,6 +68,7 @@ client.on('interactionCreate', async interaction => { } }); +//some random shit client.on("messageCreate", async message => { console.log(`${message.author.username}: ${message.content} in: ${message.guild}`) }) diff --git a/komennot/test.js b/komennot/test.js new file mode 100644 index 0000000..8900f51 --- /dev/null +++ b/komennot/test.js @@ -0,0 +1,9 @@ +const Discord = require("discord.js"); + +exports.run = (client, message, args) => { + message.channel.send("pong!").catch(console.error); +} + module.exports.help = { + name: "command name", + usage: "command usage", + }; \ No newline at end of file