Files
quarksBot/structures/QuarksBot.js
T

189 lines
5.6 KiB
JavaScript

const { Collection, Client, MessageEmbed } = require('discord.js');
const MongoClient = require('mongodb').MongoClient;
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) => {
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) => {
return await this.api
.interactions(interaction.id, interaction.token)
.callback.post({
data: {
type: 4,
data:
typeof message == "string"
? { content: message }
: message.type && message.type === "rich"
? { embeds: [message] }
: message,
},
});
};
let cmd = client.commands.get(command);
if (cmd.SlashCommand && cmd.SlashCommand.run)
cmd.SlashCommand.run(this, interaction, args, { GuildDB });
});
const client = this;
}
async connectMongo(mongoURI, dbo) {
this.db = await MongoClient.connect(mongoURI);
this.dbo = this.db.db(dbo);
this.log('Successfully connected to mongoDB');
}
exists(n) {return null != n && undefined != n && "" != n}
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 MessageEmbed()
.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 customChannelStatus, allowedChannels;
let actionChannel, tweetChannel;
let guild = await this.dbo.collection("guilds").findOne({"server.serverID":GuildId}).then(guild => guild);
// If guild not found, generate guild default
if (!guild) {
let newGuild = {
server: {
serverID: GuildId,
hasCustomChannels: false,
actionChannel: null,
tweetChannel: null
}
}
this.dbo.collection("guilds").insertOne(newGuild, function(err, res) {
if (err) throw err;
});
customChannelStatus = newGuild.server.hasCustomChannels;
allowedChannels = null;
actionChannel = null;
tweetChannel = null;
} else {
customChannelStatus = guild.server.hasCustomChannels;
actionChannel = guild.server.actionChannel;
tweetChannel = guild.server.tweetChannel;
if (this.exists(guild.server.allowedChannels) && guild.server.allowedChannels.length>0) {
allowedChannels = guild.server.allowedChannels;
} else allowedChannels = null;
}
let guildData = {
allowedChannels: allowedChannels,
customChannelStatus: customChannelStatus,
actionChannel: actionChannel,
tweetChannel: tweetChannel,
serverID: GuildId
}
return guildData;
}
/*
This command is to verify a user
has the correct role to use a
command ie. has cop role to use
name-search
*/
async verifyUseCommand(serverID, rolesCache, isList) {
let { customRoleStatus } = await this.GetGuild(serverID)
if (customRoleStatus) {
let hasRole = await this.checkRoleStatus(rolesCache, serverID, isList);
if (hasRole) {
return true // User has role, can use command
} else return false // User does not have role, can't use command
} else return true // There is no role limits
}
log(Text) {
this.logger.log(Text);
}
sendError(Channel, Error) {
let embed = new MessageEmbed()
.setTitle("An error occured")
.setColor("RED")
.setDescription(Error)
Channel.send(embed);
}
build() {
this.login(this.config.Token);
}
}
module.exports = QuarksBot;