206 lines
6.6 KiB
JavaScript
206 lines
6.6 KiB
JavaScript
const { Collection, Client, EmbedBuilder, Routes } = require('discord.js');
|
|
const MongoClient = require('mongodb').MongoClient;
|
|
const { REST } = require('@discordjs/rest');
|
|
const mongoose = require('mongoose');
|
|
const Logger = require("../util/Logger");
|
|
const path = require("path");
|
|
const fs = require('fs');
|
|
|
|
class QuarksBot extends Client {
|
|
|
|
constructor(options, config) {
|
|
super(options)
|
|
|
|
this.config = config;
|
|
this.commands = new Collection();
|
|
this.logger = new Logger(path.join(__dirname, "..", "logs/Logs.log"));
|
|
|
|
if (this.config.Token === "")
|
|
return new TypeError(
|
|
"The config.js is not filled out. Please make sure nothing is blank, otherwise the bot will not work properly."
|
|
);
|
|
|
|
this.db;
|
|
this.dbo;
|
|
this.connectMongo(this.config.mongoURI, this.config.dbo);
|
|
this.LoadCommands();
|
|
this.LoadEvents();
|
|
|
|
this.Ready = false;
|
|
|
|
this.ws.on("INTERACTION_CREATE", async (interaction) => {
|
|
const start = new Date().getTime();
|
|
if (interaction.type!=3) {
|
|
client.log("Interaction")
|
|
let GuildDB = await this.GetGuild(interaction.guild_id);
|
|
|
|
const command = interaction.data.name.toLowerCase();
|
|
const args = interaction.data.options;
|
|
|
|
//Easy to send respnose so ;)
|
|
interaction.guild = await this.guilds.fetch(interaction.guild_id);
|
|
interaction.send = async (message) => {
|
|
const rest = new REST({ version: '10' }).setToken(client.config.Token);
|
|
|
|
return await rest.post(Routes.interactionCallback(interaction.id, interaction.token), {
|
|
body: {
|
|
type: 4,
|
|
// data: typeof message == "string"
|
|
// ? { content: message, ...(message.ephemeral && {flags: (1 << 6)}), }
|
|
// : message.embeds[0] && message.embeds[0].type === "rich"
|
|
// ? { embeds: message.embeds, ...(message.ephemeral && {flags: (1 << 6)}), }
|
|
// : { message, ...(message.ephemeral && {flags: (1 << 6)}), },
|
|
data: message,
|
|
}
|
|
});
|
|
};
|
|
let cmd = client.commands.get(command);
|
|
if ((cmd.name == 'ping' || cmd.name == 'stats') && cmd.SlashCommand && cmd.SlashCommand.run) cmd.SlashCommand.run(this, interaction, args, { GuildDB }, start);
|
|
else if (cmd.SlashCommand && cmd.SlashCommand.run) cmd.SlashCommand.run(this, interaction, args, { GuildDB });
|
|
}
|
|
});
|
|
|
|
const client = this;
|
|
}
|
|
|
|
async connectMongo(mongoURI, dbo) {
|
|
// Connect to MongoDB database.
|
|
this.db = await MongoClient.connect(mongoURI);
|
|
this.dbo = this.db.db(dbo);
|
|
|
|
mongoose.connect(`${mongoURI}/${dbo}`);
|
|
this.log('Successfully connected to mongoDB');
|
|
}
|
|
|
|
exists(n) {return null != n && undefined != n && "" != n}
|
|
|
|
secondsToDhms(seconds) {
|
|
seconds = Number(seconds);
|
|
const d = Math.floor(seconds / (3600*24));
|
|
const h = Math.floor(seconds % (3600*24) / 3600);
|
|
const m = Math.floor(seconds % 3600 / 60);
|
|
const s = Math.floor(seconds % 60);
|
|
|
|
const dDisplay = d > 0 ? d + (d == 1 ? " day, " : " days, ") : "";
|
|
const hDisplay = h > 0 ? h + (h == 1 ? " hour, " : " hours, ") : "";
|
|
const mDisplay = m > 0 ? m + (m == 1 ? " minute, " : " minutes, ") : "";
|
|
const sDisplay = s > 0 ? s + (s == 1 ? " second" : " seconds") : "";
|
|
return dDisplay + hDisplay + mDisplay + sDisplay;
|
|
}
|
|
|
|
LoadCommands() {
|
|
let CommandsDir = path.join(__dirname, '..', 'commands');
|
|
fs.readdir(CommandsDir, (err, files) => {
|
|
if (err) this.log(err);
|
|
else
|
|
files.forEach((file) => {
|
|
let cmd = require(CommandsDir + "/" + file);
|
|
if (!cmd.name || !cmd.description)
|
|
return this.log(
|
|
"Unable to load Command: " +
|
|
file.split(".")[0] +
|
|
", Reason: File doesn't had run/name/desciption"
|
|
);
|
|
this.commands.set(file.split(".")[0].toLowerCase(), cmd);
|
|
this.log("Command Loaded: " + file.split(".")[0]);
|
|
});
|
|
});
|
|
}
|
|
|
|
LoadEvents() {
|
|
let EventsDir = path.join(__dirname, '..', 'events');
|
|
fs.readdir(EventsDir, (err, files) => {
|
|
if (err) this.log(err);
|
|
else
|
|
files.forEach((file) => {
|
|
const event = require(EventsDir + "/" + file);
|
|
this.on(file.split(".")[0], event.bind(null, this));
|
|
this.logger.log("Event Loaded: " + file.split(".")[0]);
|
|
});
|
|
});
|
|
}
|
|
|
|
sendTime(Channel, Error) {
|
|
let embed = new EmbedBuilder()
|
|
.setColor(this.config.EmbedColor)
|
|
.setDescription(Error);
|
|
|
|
Channel.send(embed);
|
|
}
|
|
|
|
RegisterSlashCommands() {
|
|
this.guilds.cache.forEach((guild) => {
|
|
require("../util/RegisterSlashCommands")(this, guild.id);
|
|
});
|
|
}
|
|
|
|
async GetGuild(GuildId) {
|
|
let guild = await this.dbo.collection("guilds").findOne({"server.serverID":GuildId}).then(guild => guild);
|
|
|
|
// If guild not found, generate guild default
|
|
if (!guild) {
|
|
guild = {
|
|
server: {
|
|
serverID: GuildId,
|
|
allowedChannels: [],
|
|
actionChannel: null,
|
|
tweetChannel: null,
|
|
advertsChannel: null,
|
|
dispatchChannel: null,
|
|
startingBalance: 1000.00,
|
|
incomeRoles: [],
|
|
botAdmin: null,
|
|
commercialRole: null,
|
|
officerRole: null,
|
|
dispatcherRole: null,
|
|
onlyOfficersSearch: false,
|
|
}
|
|
}
|
|
this.dbo.collection("guilds").insertOne(guild, function(err, res) {
|
|
if (err) throw err;
|
|
});
|
|
}
|
|
|
|
let guildData = {
|
|
serverID: GuildId,
|
|
customChannelStatus: guild.server.allowedChannels.length > 0 ? true : false,
|
|
allowedChannels: guild.server.allowedChannels,
|
|
actionChannel: guild.server.actionChannel,
|
|
tweetChannel: guild.server.tweetChannel,
|
|
advertsChannel: guild.server.advertsChannel,
|
|
dispatchChannel: guild.server.dispatchChannel,
|
|
startingBalance: guild.server.startingBalance,
|
|
incomeRoleStatus: guild.server.incomeRoles.length > 0 ? true : false,
|
|
incomeRoles: guild.server.incomeRoles,
|
|
commercialRole: guild.server.commercialRole,
|
|
officerRole: guild.server.officerRole,
|
|
dispatcherRole: guild.server.dispatcherRole,
|
|
onlyOfficersSearch: guild.server.onlyOfficersSearch,
|
|
botAdmin: guild.server.botAdmin,
|
|
}
|
|
return guildData;
|
|
}
|
|
|
|
log(Text) {
|
|
this.logger.log(Text);
|
|
}
|
|
|
|
error(Text) {
|
|
this.logger.error(Text);
|
|
}
|
|
|
|
sendError(Channel, Error) {
|
|
let embed = new EmbedBuilder()
|
|
.setTitle("An error occured")
|
|
.setColor(client.config.Colors.Red)
|
|
.setDescription(Error)
|
|
|
|
Channel.send(embed);
|
|
}
|
|
|
|
build() {
|
|
this.login(this.config.Token);
|
|
}
|
|
}
|
|
|
|
module.exports = QuarksBot; |