min-robot/src/index.ts

67 lines
1.4 KiB
TypeScript

import { Client } from "oceanic.js";
import "dotenv/config";
import commands from "./commands";
import { PREFIX } from "./constants";
function token() {
return (
process.env.DISCORD_TOKEN ??
(() => {
console.error("No `DISCORD_TOKEN` was specified!!");
process.exit(1);
})()
);
}
const client = new Client({
auth: `Bot ${token()}`,
gateway: {
intents: ["ALL"],
},
});
// --
client.on("ready", async () => {
console.log("Ready as", client.user.tag);
});
client.on("messageCreate", async (msg) => {
if (msg.author.bot) return;
const content = msg.content.toLowerCase().trim();
if (!content.startsWith(PREFIX)) return;
const cmdName = content.split(/\s/)[0]?.slice(PREFIX.length) ?? "";
const cmd = commands.find((cmd) => cmd.names.includes(cmdName));
if (cmd === undefined) return;
if (await cmd.checkPerm(msg)) {
const reply = async (text: string) => {
await msg.channel?.createMessage({
content: text,
allowedMentions: {
repliedUser: false,
},
messageReference: {
messageID: msg.id,
},
});
};
const say = async (text: string) => {
await msg.channel?.createMessage({
content: text,
});
};
await cmd?.handler(msg, { reply, say });
} else await msg.createReaction("❌");
});
client.on("error", (err) => {
console.error("Error!!", err);
});
client.connect();