preload commands, add commands command
This commit is contained in:
parent
1e81dfba6a
commit
2148e15643
3 changed files with 43 additions and 5 deletions
20
commands/commands.js
Normal file
20
commands/commands.js
Normal file
|
@ -0,0 +1,20 @@
|
|||
import fs from "fs";
|
||||
|
||||
const command = {
|
||||
name: "commands",
|
||||
desc: "get list of commands",
|
||||
run: async (client, msg) => {
|
||||
const commandFiles = fs
|
||||
.readdirSync("./commands")
|
||||
.map((cmd) => cmd.replace(".js", ""));
|
||||
|
||||
await client
|
||||
.say(
|
||||
msg.channelName,
|
||||
`${msg.displayName}, List of commands: ${commandFiles.join(", ")}`
|
||||
)
|
||||
.catch((err) => console.log(err));
|
||||
},
|
||||
};
|
||||
|
||||
export default { command };
|
12
index.js
12
index.js
|
@ -1,5 +1,7 @@
|
|||
import { client } from "./clients/twitch.js";
|
||||
import { getCommands } from "./utils/commands.js";
|
||||
|
||||
const commands = await getCommands();
|
||||
client.on("ready", async () => console.log("Successfully connected to chat"));
|
||||
client.on("close", async (error) => {
|
||||
if (error != null) {
|
||||
|
@ -18,11 +20,11 @@ client.on("PRIVMSG", async (msg) => {
|
|||
}
|
||||
|
||||
if (splitted[0] === "%") {
|
||||
import(`./commands/${splitted[1]}.js`)
|
||||
.then((command) => command.default.command.run(client, msg, splitted))
|
||||
.catch(() =>
|
||||
client.say(msg.channelName, `${msg.displayName}, not a command PoroSad`)
|
||||
);
|
||||
try {
|
||||
await commands[splitted[1]].run(client, msg, splitted);
|
||||
} catch {
|
||||
client.say(msg.channelName, `${msg.displayName}, not a command PoroSad`);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
|
|
16
utils/commands.js
Normal file
16
utils/commands.js
Normal file
|
@ -0,0 +1,16 @@
|
|||
import fs from "fs";
|
||||
|
||||
export async function getCommands() {
|
||||
const commands = {};
|
||||
const commandFiles = fs
|
||||
.readdirSync("./commands")
|
||||
.map((file) => file.replace(".js", ""));
|
||||
|
||||
for (let file of commandFiles) {
|
||||
await import(`../commands/${file}.js`).then(
|
||||
(cmd) => (commands[file] = cmd.default.command)
|
||||
);
|
||||
}
|
||||
|
||||
return commands;
|
||||
}
|
Loading…
Reference in a new issue