bot stuff idk
This commit is contained in:
+27
-3
@@ -1,14 +1,19 @@
|
||||
import { Message } from "discord.js";
|
||||
import { Client } from "fosscord-gopnik/build/lib"; // huh? oh well. some bugs in my lib Ig
|
||||
|
||||
import { Command, getCommands } from "./commands/index.js";
|
||||
|
||||
export default class Bot {
|
||||
client: Client;
|
||||
commands: { [key: string]: Command; } = {};
|
||||
|
||||
constructor(client: Client) {
|
||||
this.client = client;
|
||||
}
|
||||
|
||||
onReady = () => {
|
||||
onReady = async () => {
|
||||
this.commands = await getCommands();
|
||||
|
||||
console.log(`Logged in as ${this.client.user!.tag}`);
|
||||
|
||||
this.client.user!.setPresence({
|
||||
@@ -16,10 +21,29 @@ export default class Bot {
|
||||
name: "EVERYTHING",
|
||||
type: "WATCHING",
|
||||
}]
|
||||
})
|
||||
});
|
||||
|
||||
};
|
||||
|
||||
onMessageCreate = (msg: Message) => {
|
||||
|
||||
const prefix = process.env.PREFIX as string;
|
||||
if (msg.content.indexOf(prefix) === -1) return;
|
||||
if (msg.author.bot) return;
|
||||
|
||||
const content = msg.content.slice(prefix.length).split(" ");
|
||||
const cmd = content.shift();
|
||||
if (!cmd) return;
|
||||
const args = content;
|
||||
|
||||
const command = this.commands[cmd];
|
||||
if (!command) return;
|
||||
|
||||
command.exec({
|
||||
user: msg.author,
|
||||
member: msg.member,
|
||||
guild: msg.guild,
|
||||
message: msg,
|
||||
args: args,
|
||||
});
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
import { Message, GuildMember, Guild, User } from "discord.js";
|
||||
import fs from "fs";
|
||||
|
||||
export type CommandContext = {
|
||||
user: User,
|
||||
guild: Guild | null,
|
||||
member: GuildMember | null,
|
||||
message: Message,
|
||||
args: string[],
|
||||
};
|
||||
|
||||
export type Command = {
|
||||
name: string;
|
||||
exec: (ctx: CommandContext) => any;
|
||||
};
|
||||
|
||||
const walk = async (path: string): Promise<Command[]> => {
|
||||
const files = fs.readdirSync(path);
|
||||
const out: Command[] = [];
|
||||
for (var file of files) {
|
||||
if (file.indexOf("index") !== -1) continue;
|
||||
|
||||
var imported = await import(`${path}/${file}`);
|
||||
}
|
||||
|
||||
return out;
|
||||
};
|
||||
|
||||
export const getCommands = async () => {
|
||||
const map: { [key: string]: Command; } = {};
|
||||
(await walk("./build/commands")).forEach((val) => map[val.name] = val);
|
||||
return map;
|
||||
};
|
||||
@@ -0,0 +1,8 @@
|
||||
import { Command } from "./index.js";
|
||||
|
||||
export default {
|
||||
name: "instance",
|
||||
exec: ({ message }) => {
|
||||
message.reply("Test");
|
||||
}
|
||||
} as Command;
|
||||
@@ -1,6 +1,6 @@
|
||||
import "dotenv/config";
|
||||
import Fosscord from "fosscord-gopnik";
|
||||
import Bot from "./Bot";
|
||||
import Bot from "./Bot.js"; // huh?
|
||||
|
||||
const client = new Fosscord.Client({
|
||||
intents: ["GUILD_MESSAGES"],
|
||||
@@ -15,7 +15,6 @@ const client = new Fosscord.Client({
|
||||
const bot = new Bot(client);
|
||||
|
||||
client.on("ready", bot.onReady);
|
||||
|
||||
client.on("messageCreate", bot.onMessageCreate);
|
||||
|
||||
client.login(process.env.TOKEN);
|
||||
Reference in New Issue
Block a user