From e8ed30dd0e45492cc2d2e64af7acba8b65616e15 Mon Sep 17 00:00:00 2001 From: WubzyGD <63122940+WubzyGD@users.noreply.github.com> Date: Sat, 10 Oct 2020 18:16:36 -0400 Subject: [PATCH] Finish prefixes and fixed bugs --- bot.js | 3 +++ commands/prefix.js | 12 +++++++++--- commands/staffrole.js | 2 +- events/message.js | 2 +- events/ready.js | 13 ++++++++++++- models/guild.js | 3 ++- 6 files changed, 28 insertions(+), 7 deletions(-) diff --git a/bot.js b/bot.js index 845618c..6875b40 100644 --- a/bot.js +++ b/bot.js @@ -15,5 +15,8 @@ async function init() { client.developers = ["330547934951112705", "673477059904929802"]; client.utils = {}; client.utils.logch = async () => {return client.guilds.cache.get('762707532417335296').channels.cache.get('762732961753595915');}; + + client.guildconfig = {}; + client.guildconfig.prefixes = new Map(); } init(); \ No newline at end of file diff --git a/commands/prefix.js b/commands/prefix.js index 33d0e35..2602530 100644 --- a/commands/prefix.js +++ b/commands/prefix.js @@ -1,5 +1,5 @@ const Discord = require('discord.js'); -const GuildSettings = require('../util/guild'); +const GuildSettings = require('../models/guild'); module.exports = { name: "prefix", @@ -14,14 +14,20 @@ module.exports = { let tguild = await GuildSettings.findOne({gid: message.guild.id}) ? await GuildSettings.findOne({gid: message.guild.id}) : new GuildSettings({gid: message.guild.id}); + if (!tguild.prefix) {tguild.prefix = '';} if (!message.member.permissions.has("ADMINISTRATOR") && (!tguild.staffrole.length || !message.guild.roles.cache.has(tguild.staffrole) || !message.member.roles.cache.has(tguild.staffrole))) {return message.reply("You don't have the permissions to use this command here.");} if (!args.length) {return message.channel.send(`Syntax: \`${prefix} \`. My current prefix in this server is \`${tguild.prefix.length ? tguild.prefix : 'n?'}\``);} let np = args[0]; if (np.length > 7) {return message.reply("Hmmm, that prefix is a bit long. Try making something smaller!");} - if (!np.match(/^[a-zA-Z0-9,.!?<>\-_+=/;$#%^&*]$/m)) {return message.reply('Your custom prefix contains some *wonky* characters. Please use only alphanumerics and basic symbols.');} + if (!np.match(/^[a-zA-Z0-9,.!?<>\-_+=/;$#%^&*]+$/)) {return message.reply('Your custom prefix contains some *wonky* characters. Please use only alphanumerics and basic symbols.');} tguild.prefix = ['c', 'clear', 'n', 'none'].includes(np.trim().toLowerCase()) ? '' : np; tguild.save(); - let upm = message.reply("sure thing!"); + if (['c', 'clear', 'n', 'none'].includes(np.trim().toLowerCase())) { + client.guildconfig.prefixes.set(message.guild.id, null); + return message.reply('this server\'s prefix has been reset to the default, `n?`.'); + } + client.guildconfig.prefixes.set(message.guild.id, np); + let upm = await message.reply("sure thing!"); await require('../util/wait')(1750); return upm.edit(new Discord.MessageEmbed() .setAuthor('Prefix updated!', message.author.avatarURL()) diff --git a/commands/staffrole.js b/commands/staffrole.js index 133ea35..4adc215 100644 --- a/commands/staffrole.js +++ b/commands/staffrole.js @@ -32,7 +32,7 @@ module.exports = { } else { tguild.staffrole = role.id; tguild.save(); - let upm = message.reply("sure thing!"); + let upm = await message.reply("sure thing!"); await require('../util/wait')(1750); return upm.edit(new Discord.MessageEmbed() .setAuthor('Staff role updated!', message.author.avatarURL()) diff --git a/events/message.js b/events/message.js index 71b4e07..50abd0a 100644 --- a/events/message.js +++ b/events/message.js @@ -11,7 +11,7 @@ module.exports = async (client, message) => { if (message.guild && !message.member.permissions.has("SEND_MESSAGES")) {return undefined;} - var prefix = 'n?'; + var prefix = message.guild ? client.guildconfig.prefixes.has(message.guild.id) ? client.guildconfig.prefixes.get(message.guild.id) !== null ? client.guildconfig.prefixes.get(message.guild.id) : 'n?' : 'n?' : 'n?'; var msg = message.content.toLowerCase(); var mention = message.mentions.users.first(); diff --git a/events/ready.js b/events/ready.js index 56f3a59..b9b6d98 100644 --- a/events/ready.js +++ b/events/ready.js @@ -2,6 +2,7 @@ const Discord = require('discord.js'); const chalk = require('chalk'); const moment = require('moment'); const mongoose = require('mongoose'); +const GuildSettings = require('../models/guild'); var prefix = 'n?'; @@ -16,13 +17,16 @@ module.exports = async client => { console.error(`\n${chalk.red('[ERROR]')} >> ${chalk.yellow(`At [${date}] | Occurred while trying to connect to Mongo Cluster`)}`, e); } + /*let db = mongoose.connection; + await db.guild.update({}, {"$set": {'prefix': ''}}, false, true);*/ + console.log(`\n${chalk.green('[BOOT]')} >> [${moment().format('L LTS')}] -> ${chalk.greenBright("Connected to Discord")}.`); let date = new Date; date = date.toString().slice(date.toString().search(":") - 2, date.toString().search(":") + 6); console.log(`\n${chalk.gray('[INFO]')} >> ${chalk.white(`Logged in at ${date}.`)}`); console.log(`\n${chalk.gray('[INFO]')} >> ${chalk.white(`Logged in as ${client.user.username}!`)}`); console.log(`${chalk.gray('[INFO]')} >> ${chalk.white(`Client ID: ${client.user.id}`)}`); console.log(`${chalk.gray('[INFO]')} >> ${chalk.white(`Running on ${client.guilds.cache.size} servers!`)}`); - console.log(`${chalk.gray('[INFO]')} >> ${chalk.white(`Serving ${client.users.cache.size} users!`)}`); + console.log(`${chalk.gray('[INFO]')} >> ${chalk.white(`Serving ${client.users.cache.size} users!`)}`); let responses = { "PLAYING": [ @@ -41,6 +45,13 @@ module.exports = async client => { const BotDataSchema = require('../models/bot'); + const setP = async () => {let tg; for (tg of Array.from(client.guilds.cache.values)) { + let tguild = await GuildSettings.findOne({gid: tg.id}); + if (tguild && tguild.prefix && tguild.prefix.length) {client.guildconfig.prefixes.set(tg.id, tguild.prefix);} + }}; + setP(); + setInterval(setP, 120000); + let botData = await BotDataSchema.findOne({finder: 'lel'}) ? await BotDataSchema.findOne({finder: 'lel'}) : new BotDataSchema({ diff --git a/models/guild.js b/models/guild.js index dc4e2c1..6efcad8 100644 --- a/models/guild.js +++ b/models/guild.js @@ -13,7 +13,8 @@ const guildSchema = new mongoose.Schema({ joinrole: {type: String, default: ''}, cooldowns: {type: Boolean, default: false}, levelmessage: {type: String, default: '**{{u}}** has reached level **{{l}}**! :tada:'}, - levelmessages: {type: Boolean, default: false} + levelmessages: {type: Boolean, default: false}, + prefix: {type: String, default: ''} }); module.exports = mongoose.model("guild", guildSchema); \ No newline at end of file