diff --git a/.gitignore b/.gitignore index cbe79e5b..d0781de3 100644 --- a/.gitignore +++ b/.gitignore @@ -16,6 +16,7 @@ config.json command-leaderboard.json command-last-run.json blacklist.json +cleverbot.json # Tensorflow Models tf_models/ diff --git a/Xiao.js b/Xiao.js index 6b9a10a4..01a6e1d4 100644 --- a/Xiao.js +++ b/Xiao.js @@ -121,6 +121,18 @@ client.on('ready', async () => { } }, 1.8e+6); + // Import Cleverbot users + try { + const results = client.importCleverbotAllowed(); + if (results) { + client.logger.info('[CLEVERBOT] cleverbot.json successfully loaded.'); + } else { + client.logger.error('[CLEVERBOT] cleverbot.json is not formatted correctly.'); + } + } catch (err) { + client.logger.error(`[CLEVERBOT] Could not parse cleverbot.json:\n${err.stack}`); + } + // Import blacklist try { const results = client.importBlacklist(); diff --git a/commands/cleverbot/cleverbot-end.js b/commands/cleverbot/cleverbot-end.js index d2af4ce2..f247a3d0 100644 --- a/commands/cleverbot/cleverbot-end.js +++ b/commands/cleverbot/cleverbot-end.js @@ -12,6 +12,9 @@ module.exports = class CleverbotEndCommand extends Command { } run(msg) { + if (!this.client.isOwner(msg.author) && !this.client.allowedUsers.includes(msg.author.id)) { + return msg.say('You are not currently allowed to use Cleverbot.'); + } const cleverbot = this.client.cleverbots.get(msg.channel.id); if (!cleverbot) return msg.say('There is not a Cleverbot conversation in this channel.'); clearTimeout(cleverbot.timeout); diff --git a/commands/cleverbot/cleverbot.js b/commands/cleverbot/cleverbot.js index 44eba802..27298416 100644 --- a/commands/cleverbot/cleverbot.js +++ b/commands/cleverbot/cleverbot.js @@ -22,6 +22,9 @@ module.exports = class CleverbotCommand extends Command { } run(msg) { + if (!this.client.isOwner(msg.author) && !this.client.allowedUsers.includes(msg.author.id)) { + return msg.say('You are not currently allowed to use Cleverbot.'); + } if (this.client.cleverbots.has(msg.channel.id)) { return msg.say('There is already a Cleverbot conversation in this channel.'); } diff --git a/commands/util/allow-cleverbot.js b/commands/util/allow-cleverbot.js new file mode 100644 index 00000000..55b07a4c --- /dev/null +++ b/commands/util/allow-cleverbot.js @@ -0,0 +1,30 @@ +const Command = require('../../structures/Command'); + +module.exports = class AllowCleverbotCommand extends Command { + constructor(client) { + super(client, { + name: 'allow-cleverbot', + aliases: ['allow-clevs'], + group: 'util', + memberName: 'allow-cleverbot', + description: 'Allows a user to use Cleverbot.', + details: 'Only the bot owner(s) may use this command.', + ownerOnly: true, + guarded: true, + args: [ + { + key: 'target', + prompt: 'Who do you want to allow? Use the ID.', + type: 'string' + } + ] + }); + } + + async run(msg, { target }) { + if (this.client.allowedUsers.includes(target)) return msg.say(`🧠 \`${target}\` is already allowed.`); + this.client.allowedUsers.push(target); + this.client.exportCleverbotAllowed(); + return msg.say(`🧠 Allowed \`${target}\` to use Cleverbot.`); + } +}; diff --git a/commands/util/blacklist.js b/commands/util/blacklist.js index 70877029..19040161 100644 --- a/commands/util/blacklist.js +++ b/commands/util/blacklist.js @@ -22,7 +22,7 @@ module.exports = class BlacklistCommand extends Command { }, { key: 'target', - prompt: 'What do you want to blacklist? Use the ID.', + prompt: 'Who do you want to blacklist? Use the ID.', type: 'string' } ] diff --git a/commands/util/disallow-cleverbot.js b/commands/util/disallow-cleverbot.js new file mode 100644 index 00000000..caac0d5b --- /dev/null +++ b/commands/util/disallow-cleverbot.js @@ -0,0 +1,31 @@ +const Command = require('../../structures/Command'); +const { removeFromArray } = require('../../util/Util'); + +module.exports = class DisallowCleverbotCommand extends Command { + constructor(client) { + super(client, { + name: 'disallow-cleverbot', + aliases: ['disallow-clevs'], + group: 'util', + memberName: 'disallow-cleverbot', + description: 'Disallows a user from using Cleverbot.', + details: 'Only the bot owner(s) may use this command.', + ownerOnly: true, + guarded: true, + args: [ + { + key: 'target', + prompt: 'Who do you want to disallow? Use the ID.', + type: 'string' + } + ] + }); + } + + async run(msg, { target }) { + if (!this.client.allowedUsers.includes(target)) return msg.say(`🧠 \`${target}\` is not allowed.`); + removeFromArray(this.client.allowedUsers, target); + this.client.exportCleverbotAllowed(); + return msg.say(`🧠 Disallowed \`${target}\` from using Cleverbot.`); + } +}; diff --git a/commands/util/unblacklist.js b/commands/util/unblacklist.js index a2487860..ddd6db81 100644 --- a/commands/util/unblacklist.js +++ b/commands/util/unblacklist.js @@ -23,7 +23,7 @@ module.exports = class UnblacklistCommand extends Command { }, { key: 'target', - prompt: 'What do you want to unblacklist? Use the ID.', + prompt: 'Who do you want to unblacklist? Use the ID.', type: 'string' } ] diff --git a/package.json b/package.json index 770f4081..36f9c5b5 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "xiao", - "version": "140.0.0", + "version": "140.1.0", "description": "Your personal server companion.", "main": "Xiao.js", "private": true, diff --git a/structures/Client.js b/structures/Client.js index 820d07d2..05184057 100644 --- a/structures/Client.js +++ b/structures/Client.js @@ -49,6 +49,7 @@ module.exports = class XiaoClient extends CommandoClient { this.games = new Collection(); this.dispatchers = new Map(); this.cleverbots = new Map(); + this.allowedUsers = []; this.phone = new PhoneManager(this); this.activities = activities; this.leaveMessages = leaveMsgs; @@ -194,6 +195,32 @@ module.exports = class XiaoClient extends CommandoClient { return buf; } + importCleverbotAllowed() { + const read = fs.readFileSync(path.join(__dirname, '..', 'cleverbot.json'), { encoding: 'utf8' }); + const file = JSON.parse(read); + if (!Array.isArray(file)) return null; + for (const id of file) { + if (typeof id !== 'string') continue; + if (this.allowedUsers.includes(id)) continue; + this.allowedUsers.push(id); + } + return file; + } + + exportCleverbotAllowed() { + let text = '[\n '; + if (this.allowedUsers.length) { + for (const id of this.allowedUsers.guild) { + text += `"${id}",\n `; + } + text = text.slice(0, -3); + } + text += '\n]\n'; + const buf = Buffer.from(text); + fs.writeFileSync(path.join(__dirname, '..', 'cleverbot.json'), buf, { encoding: 'utf8' }); + return buf; + } + importCommandLeaderboard(add = false) { const read = fs.readFileSync(path.join(__dirname, '..', 'command-leaderboard.json'), { encoding: 'utf8'