Moved back to MongoDB, added some extra flags to flag
This commit is contained in:
parent
0ebd0a0cee
commit
6eee7c6058
18 changed files with 303 additions and 269 deletions
|
@ -6,5 +6,12 @@ const client = require("../utils/client.js");
|
|||
// run when the bot is added to a guild
|
||||
module.exports = async (guild) => {
|
||||
logger.log("info", `[GUILD JOIN] ${guild.name} (${guild.id}) added the bot. Owner: ${client.users.get(guild.ownerID).username}#${client.users.get(guild.ownerID).discriminator} (${guild.ownerID})`);
|
||||
await db.query("INSERT INTO guilds (guild_id, tags, prefix, warns, disabled) VALUES ($1, $2, $3, $4, $5)", [guild.id, misc.tagDefaults, "&", {}, []]);
|
||||
const guildDB = new db.guilds({
|
||||
id: guild.id,
|
||||
tags: misc.tagDefaults,
|
||||
prefix: "&",
|
||||
warns: {},
|
||||
disabledChannels: []
|
||||
});
|
||||
await guildDB.save();
|
||||
};
|
||||
|
|
|
@ -16,9 +16,9 @@ module.exports = async (message) => {
|
|||
if (!message.channel.guild.members.get(client.user.id).permission.has("sendMessages") || !message.channel.permissionsOf(client.user.id).has("sendMessages")) return;
|
||||
|
||||
// prefix can be a mention or a set of special characters
|
||||
const guildDB = await database.query("SELECT * FROM guilds WHERE guild_id = $1", [message.channel.guild.id]);
|
||||
const guildDB = await database.guilds.findOne({ id: message.channel.guild.id }).lean().exec();
|
||||
const prefixMention = new RegExp(`^<@!?${client.user.id}> `);
|
||||
const prefix = prefixMention.test(message.content) ? message.content.match(prefixMention)[0] : guildDB.rows[0].prefix;
|
||||
const prefix = prefixMention.test(message.content) ? message.content.match(prefixMention)[0] : guildDB.prefix;
|
||||
|
||||
// ignore other stuff
|
||||
if (message.content.startsWith(prefix) === false) return;
|
||||
|
@ -30,7 +30,7 @@ module.exports = async (message) => {
|
|||
const command = args.shift().toLowerCase();
|
||||
|
||||
// don't run if message is in a disabled channel
|
||||
if (guildDB.rows[0].disabled && guildDB.rows[0].disabled.includes(message.channel.id) && command != "channel") return;
|
||||
if (guildDB.disabledChannels && guildDB.disabledChannels.includes(message.channel.id) && command != "channel") return;
|
||||
|
||||
// check if command exists
|
||||
const cmd = collections.commands.get(command) || collections.commands.get(collections.aliases.get(command));
|
||||
|
@ -39,8 +39,10 @@ module.exports = async (message) => {
|
|||
// actually run the command
|
||||
logger.log("info", `${message.author.username} (${message.author.id}) ran command ${command}`);
|
||||
try {
|
||||
const count = await database.query("SELECT * FROM counts WHERE command = $1", [collections.aliases.has(command) ? collections.aliases.get(command) : command]);
|
||||
await database.query("UPDATE counts SET count = $1 WHERE command = $2", [count.rows[0].count + 1, collections.aliases.has(command) ? collections.aliases.get(command) : command]);
|
||||
const global = (await database.global.findOne({}).exec());
|
||||
const count = global.cmdCounts.get(collections.aliases.has(command) ? collections.aliases.get(command) : command);
|
||||
global.cmdCounts.set(collections.aliases.has(command) ? collections.aliases.get(command) : command, parseInt(count) + 1);
|
||||
await global.save();
|
||||
const result = await cmd(message, args, content.replace(command, "").trim()); // we also provide the message content as a parameter for cases where we need more accuracy
|
||||
if (typeof result === "string" || (typeof result === "object" && result.embed)) {
|
||||
await client.createMessage(message.channel.id, result);
|
||||
|
|
|
@ -20,21 +20,38 @@ module.exports = async () => {
|
|||
|
||||
// make sure settings/tags exist
|
||||
for (const [id] of client.guilds) {
|
||||
const guildDB = await database.query("SELECT * FROM guilds WHERE guild_id = $1", [id]);
|
||||
if (guildDB.rows.length === 0) {
|
||||
const guildDB = await database.guilds.findOne({id: id});
|
||||
if (!guildDB) {
|
||||
logger.log(`Registering guild database entry for guild ${id}...`);
|
||||
await database.query("INSERT INTO guilds (guild_id, tags, prefix, warns, disabled) VALUES ($1, $2, $3, $4, $5)", [id, misc.tagDefaults, "&", {}, []]);
|
||||
const newGuild = new database.guilds({
|
||||
id: id,
|
||||
tags: misc.tagDefaults,
|
||||
prefix: "&",
|
||||
warns: {},
|
||||
disabledChannels: []
|
||||
});
|
||||
await newGuild.save();
|
||||
} else if (guildDB) {
|
||||
if (!guildDB.warns) {
|
||||
logger.log(`Creating warn object for guild ${id}...`);
|
||||
guildDB.set("warns", {});
|
||||
await guildDB.save();
|
||||
} else if (!guildDB.disabledChannels) {
|
||||
logger.log(`Creating disabled channels object for guild ${id}...`);
|
||||
guildDB.set("disabledChannels", []);
|
||||
await guildDB.save();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!run && first) {
|
||||
const job = new cron.CronJob("0 0 * * 0", async () => {
|
||||
logger.log("Deleting stale guild entries in database...");
|
||||
const guildDB = await database.query("SELECT * FROM guilds");
|
||||
for (const { guild_id } of guildDB.rows) {
|
||||
if (!client.guilds.get(guild_id)) {
|
||||
await database.query("DELETE FROM guilds WHERE guild_id = $1", [guild_id]);
|
||||
logger.log(`Deleted entry for guild ID ${guild_id}.`);
|
||||
const guildDB = await database.guilds.find({});
|
||||
for (const { id } of guildDB) {
|
||||
if (!client.guilds.get(id)) {
|
||||
await database.guilds.deleteMany({ id: id });
|
||||
logger.log(`Deleted entry for guild ID ${id}.`);
|
||||
}
|
||||
}
|
||||
logger.log("Finished deleting stale entries.");
|
||||
|
@ -42,21 +59,21 @@ module.exports = async () => {
|
|||
job.start();
|
||||
}
|
||||
|
||||
let counts;
|
||||
try {
|
||||
counts = await database.query("SELECT * FROM counts");
|
||||
} catch {
|
||||
counts = { rows: [] };
|
||||
}
|
||||
if (!counts.rows[0]) {
|
||||
const global = await database.global.findOne({});
|
||||
if (!global) {
|
||||
const countObject = {};
|
||||
for (const command of collections.commands.keys()) {
|
||||
await database.query("INSERT INTO counts (command, count) VALUES ($1, $2)", [command, 0]);
|
||||
countObject[command] = 0;
|
||||
}
|
||||
const newGlobal = new database.global({
|
||||
cmdCounts: countObject
|
||||
});
|
||||
await newGlobal.save();
|
||||
} else {
|
||||
for (const command of collections.commands.keys()) {
|
||||
const count = await database.query("SELECT * FROM counts WHERE command = $1", [command]);
|
||||
if (!count.rows[0]) {
|
||||
await database.query("INSERT INTO counts (command, count) VALUES ($1, $2)", [command, 0]);
|
||||
if (!global.cmdCounts.has(command)) {
|
||||
global.cmdCounts.set(command, 0);
|
||||
await global.save();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue