Files
SupportBot/Structures/Client.js
T

147 lines
4.0 KiB
JavaScript
Raw Normal View History

2021-10-04 20:41:05 +01:00
// SupportBot | Emerald Services
// Client Structure
const fs = require("fs");
const Discord = require("discord.js");
const Command = require("./Command.js");
const Event = require("./Event.js");
const intents = new Discord.Intents(32767);
2021-10-24 22:41:46 +01:00
const yaml = require("js-yaml");
2021-10-27 19:30:10 -05:00
const supportbot = yaml.load(
fs.readFileSync("./Configs/supportbot.yml", "utf8")
);
2021-10-04 20:41:05 +01:00
class Client extends Discord.Client {
2021-10-24 22:41:46 +01:00
constructor() {
super({ intents });
2021-10-04 20:41:05 +01:00
2021-10-24 22:41:46 +01:00
this.commands = new Discord.Collection();
}
async getChannel(channel, guild) {
return guild.channels.cache.find(
(c) =>
(c.type === "GUILD_TEXT" || c.type === "GUILD_NEWS") &&
(c.id === channel || c.name.toLowerCase() === channel.toLowerCase())
);
}
async getRole(role, guild) {
return guild.roles.cache.find(
(r) => r.id === role || r.name.toLowerCase() === role.toLowerCase()
);
}
async getCategory(category, guild) {
return guild.channels.cache.find(
(c) =>
c.type === "GUILD_CATEGORY" &&
(c.id === category || c.name.toLowerCase() === category.toLowerCase())
);
}
2021-10-04 20:41:05 +01:00
2021-10-24 22:41:46 +01:00
start(token) {
const commandFiles = fs
.readdirSync("./Commands")
.filter((file) => file.endsWith(".js"));
2021-10-04 20:41:05 +01:00
2021-10-25 00:13:27 +01:00
const addonFiles = fs
.readdirSync("./Addons")
.filter((file) => file.endsWith(".js"));
2021-10-25 00:02:17 -05:00
const addons = addonFiles.map((file) => require(`../Addons/${file}`));
2021-10-24 22:41:46 +01:00
const commands = commandFiles.map((file) => require(`../Commands/${file}`));
2021-10-04 20:41:05 +01:00
2021-10-24 22:41:46 +01:00
console.log(`\u001b[34;1m`, "▬▬▬▬▬▬▬ Commands ▬▬▬▬▬▬▬");
2021-10-04 20:41:05 +01:00
2021-10-24 22:41:46 +01:00
commands.forEach((cmd) => {
console.log(`\u001b[31;1m`, `${cmd.name}`, `\u001b[32;1m`, "Loaded");
this.commands.set(cmd.name, cmd);
});
2021-10-21 19:28:23 +01:00
2021-10-24 22:41:46 +01:00
addons.forEach((addon) => {
2021-10-25 00:02:17 -05:00
console.log(
`\u001b[33m`,
`[ADDON]`,
`\u001b[31;1m`,
`${addon.name}`,
`\u001b[32;1m`,
"Loaded"
);
2021-10-24 22:41:46 +01:00
this.commands.set(addon.name, addon);
});
2021-10-21 19:28:23 +01:00
2021-10-25 00:13:27 +01:00
console.log(`\u001b[34;1m`, "▬▬▬▬▬▬▬ Commands ▬▬▬▬▬▬▬");
2021-10-21 19:28:23 +01:00
2021-10-24 22:41:46 +01:00
// Slash Commands
2021-10-21 19:28:23 +01:00
2021-10-24 22:41:46 +01:00
const slashCommands = commands.map((cmd) => ({
2021-10-25 00:02:17 -05:00
name: cmd.name.toLowerCase(),
2021-10-24 22:41:46 +01:00
description: cmd.description,
permissions: [],
options: cmd.slashCommandOptions,
defaultPermission: true,
2021-10-25 00:02:17 -05:00
}));
2021-10-25 00:13:27 +01:00
const slashCommands1 = addons.map((addon) => ({
2021-10-25 00:02:17 -05:00
name: addon.name.toLowerCase(),
2021-10-25 00:13:27 +01:00
description: addon.description,
permissions: [],
options: addon.slashCommandOptions,
defaultPermission: true,
2021-10-24 22:41:46 +01:00
}));
2021-10-21 19:28:23 +01:00
2021-10-24 22:41:46 +01:00
this.removeAllListeners();
this.on("ready", async () => {
2021-10-25 00:02:17 -05:00
const allCommands = slashCommands.concat(slashCommands1);
2021-11-17 19:03:35 -06:00
await this.application?.commands.set(allCommands, supportbot.Guild);
2021-10-24 22:41:46 +01:00
console.log(" ");
2021-11-17 19:03:35 -06:00
console.log(`\u001b[34;1m`, "▬▬▬▬▬▬▬ Slash Commands ▬▬▬▬▬▬▬"),
2021-10-24 22:41:46 +01:00
console.log(" ");
2021-10-04 20:41:05 +01:00
2021-10-25 00:02:17 -05:00
slashCommands.forEach((cmd) =>
console.log(
`\u001b[31;1m`,
`${cmd.name}`,
`\u001b[32;1m`,
`| Slash Command Registered`
)
2021-10-24 22:41:46 +01:00
);
2021-10-25 00:13:27 +01:00
2021-10-25 00:02:17 -05:00
slashCommands1.forEach((addon) =>
console.log(
`\u001b[33m`,
`[ADDON]`,
`\u001b[31;1m`,
`${addon.name}`,
`\u001b[32;1m`,
`| Slash Command Registered`
)
2021-10-25 00:13:27 +01:00
);
2021-10-24 22:41:46 +01:00
});
2021-10-21 19:28:23 +01:00
2021-10-24 22:41:46 +01:00
console.log(" ");
console.log(`\u001b[34;1m`, "▬▬▬▬▬▬▬ Events ▬▬▬▬▬▬▬");
2021-10-21 19:28:23 +01:00
2021-10-24 22:41:46 +01:00
fs.readdirSync("./Events")
.filter((file) => file.endsWith(".js"))
.forEach((file) => {
const event = require(`../Events/${file}`);
console.log(`\u001b[31;1m`, `${event.event}`, `\u001b[32;1m`, "Loaded");
2021-10-04 20:41:05 +01:00
2021-10-24 22:41:46 +01:00
this.on(event.event, event.run.bind(null, this));
});
2021-10-21 19:28:23 +01:00
2021-10-24 22:41:46 +01:00
console.log(`\u001b[34;1m`, "▬▬▬▬▬▬▬ Events ▬▬▬▬▬▬▬");
2021-12-04 21:26:55 -06:00
if (process.argv[2] != "test") {
2021-12-04 21:19:51 -06:00
this.login(token);
} else {
console.log(
`\u001b[31;1m`,
"RUNNING IN TEST MODE, IF NOT INTENDED, PLEASE USE npm start"
);
}
2021-10-24 22:41:46 +01:00
}
2021-10-04 20:41:05 +01:00
}
2021-10-24 22:41:46 +01:00
module.exports = Client;