Skip to content

Commit

Permalink
Add event handler and prefix command handler
Browse files Browse the repository at this point in the history
  • Loading branch information
LerssiUkko committed Sep 6, 2021
1 parent 4949d59 commit 047ab5c
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 0 deletions.
14 changes: 14 additions & 0 deletions events/messageCreate.js
Original file line number Diff line number Diff line change
@@ -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);
};
30 changes: 30 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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] });

Expand All @@ -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;

Expand All @@ -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}`)
})
Expand Down
9 changes: 9 additions & 0 deletions komennot/test.js
Original file line number Diff line number Diff line change
@@ -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",
};

0 comments on commit 047ab5c

Please sign in to comment.