PepperBot/src/events/messageCreate.js

71 lines
1.7 KiB
JavaScript

import * as dotenv from "dotenv";
dotenv.config();
import { default as log } from "../util/log.js";
import fs from "fs";
import discord from "discord.js";
let commands = new discord.Collection();
const commandFiles = fs
.readdirSync("src/commands/")
.filter((file) => file.endsWith(".js"));
for (const file of commandFiles) {
(async () => {
const command = await import(`../commands/${file}`);
commands.set(command.default.name, command.default);
})();
}
function parseargs(string) {
const args = string.split(" ");
for (let i = 0; i < args.length; i++) {
if (args[i] === "") {
args.splice(i, 1);
}
}
return args;
}
const prefix = process.env.PREFIX;
export default function (message, client) {
if (message.channel.type === 1) {
log(
"directmessages.log",
message,
import.meta.url,
`direct message from ${message.author.username} (${message.author}) with: "${message.content}"`
);
}
if (!message.content.startsWith(prefix)) return;
const command = message.content
.slice(prefix.length)
.split(/ +/)
.shift()
.toLowerCase();
const args = parseargs(message.content.slice(prefix.length + command.length));
if (!commands.has(command)) {
message.reply("idiot thats not a command");
log(
"failed.log",
message,
import.meta.url,
`attempt to use invalid command: ${command} by: ${message.author.username} (${message.author})"`
);
}
if (commands.get(command)) {
log(
"commands.log",
message,
import.meta.url,
`use of command: ${command} by: ${message.author.username} (\`${message.author}\`)"`,
true
);
(async () => {
commands.get(command).execute(message, args, client);
})();
}
}