From f4ba42e742e0e855f445f68dcb293b94ffb61477 Mon Sep 17 00:00:00 2001 From: rhearmas <34490428+qu-ota@users.noreply.github.com> Date: Fri, 13 Dec 2019 10:56:28 -0500 Subject: [PATCH] dictionary; added a couple more function imports --- cmds_SharpBot/Utility/dictionary.js | 2 + commands/Utility/dictionary.js | 96 ++++++++++++++++++++++++ modules/functions.js | 111 +++++++++++++++++++++++++++- 3 files changed, 206 insertions(+), 3 deletions(-) create mode 100644 commands/Utility/dictionary.js diff --git a/cmds_SharpBot/Utility/dictionary.js b/cmds_SharpBot/Utility/dictionary.js index e1930f9..17d3ed6 100644 --- a/cmds_SharpBot/Utility/dictionary.js +++ b/cmds_SharpBot/Utility/dictionary.js @@ -1,3 +1,5 @@ +// This command has already been created. + const webdict = require('webdict'); const makeCommand = method => { diff --git a/commands/Utility/dictionary.js b/commands/Utility/dictionary.js new file mode 100644 index 0000000..4e63c75 --- /dev/null +++ b/commands/Utility/dictionary.js @@ -0,0 +1,96 @@ +/* +const makeCommand = method => { + return (bot, msg, args) => { + if (args.length < 1) { + throw 'Please provide a word to search!'; + } + + const parsed = bot.utils.parseArgs(args, ['e']); + const word = parsed.leftover.join(' '); + + webdict(method, word).then(res => { + let result; + if (!res || !res.definition || !res.definition[0]) { + result = 'No results found.'; + } else { + result = res.definition[0]; + } + + if (parsed.options.e) { + msg.edit(result); + return; + } + + msg.delete(); + msg.channel.send({ + embed: bot.utils.embed(`:book: ${word}`, result) + }); + }); + }; +}; + +module.exports = [ + { + run: makeCommand('dictionary'), + info: { + name: 'dictionary', + aliases: ['dict'], + usage: 'dictionary ', + description: 'Looks a word up in the dictionary.', + credits: 'NITEHAWK' + } + }, + { + run: makeCommand('urbandictionary'), + info: { + name: 'urban', + usage: 'urban ', + description: 'Looks a word up in the urban dictionary.', + credits: 'NITEHAWK' + } + } +]; +*/ + +const webdict = require('webdict'); + +exports.run = async (client, message, args, level) => { + message.delete(); + + if (!args[0]) return message.reply("you didn't provide a valid word to look up."); + + const parsed = client.parseArgs(args, ['e']); + const word = parsed.leftover.join(' '); + + webdict(method, word).then(res => { + let result; + if (!res || !res.definition || !res.definition[0]) { + result = 'No results found.'; + } else { + result = res.definition[0]; + } + + if (parsed.options.e) { + msg.edit(result); + return; + } + + msg.channel.send({ + embed: bot.utils.embed(`:book: ${word}`, result) + }); + }); +}; + +exports.conf = { + enabled: true, + guildOnly: false, + aliases: ['dict'], + permLevel: "User" +}; + +exports.help = { + name: "dictionary", + category: "Utility", + description: "Looks up a word in the dictionary.", + usage: "dictionary " +}; diff --git a/modules/functions.js b/modules/functions.js index 752efe9..f218de7 100644 --- a/modules/functions.js +++ b/modules/functions.js @@ -16,10 +16,10 @@ module.exports = (client) => { }; const defaultSettings = { - "prefix": "~", + "prefix": "/", "modLogChannel": "mod-log", - "modRole": "Moderator", - "adminRole": "Administrator", + "modRole": "modinator", + "adminRole": "adminator", "systemNotice": "true", "welcomeChannel": "welcome", "welcomeMessage": "Say hello to {{user}}, everyone! We all need a warm welcome sometimes :D", @@ -97,6 +97,46 @@ module.exports = (client) => { } return false; }; + + client.reloadCommand = async(commandName) => { + // step 1: unload the command + let command; + if (client.commands.has(commandName)) { + command = client.commands.get(commandName); + } else if (client.aliases.has(commandName)) { + command = client.commands.get(client.aliases.get(commandName)); + } + if (!command) return `The command \`${commandName}\` doesn\'t seem to exist, nor is it an alias. Try again!`; + + if (command.shutdown) { + await command.shutdown(client); + } + const mod = require.cache[require.resolve(`../commands/${command.help.category}/${command.help.name}`)]; + delete require.cache[require.resolve(`../commands/${command.help.category}/${command.help.name}.js`)]; + for (let i = 0; i < mod.parent.children.length; i++) { + if (mod.parent.children[i] === mod) { + mod.parent.children.splice(i, 1); + break; + } + } + return false; + + // step 2: load said command + try { + client.logger.log(`Loading Command: ${commandName}`); + const props = require(`../${commandName}`); + if (props.init) { + props.init(client); + } + client.commands.set(props.help.name, props); + props.conf.aliases.forEach(alias => { + client.aliases.set(alias, props.help.name); + }); + return false; + } catch (e) { + return `Unable to load command ${commandName}: ${e}`; + } + } Object.defineProperty(String.prototype, "toProperCase", { value: function() { @@ -123,4 +163,69 @@ module.exports = (client) => { client.logger.error(`Unhandled rejection: ${err}`); console.error(err); }); + + client.parseArgs = (args, options) => { + if (!options) + return args; + if (typeof options === 'string') + options = [options]; + + let optionValues = {}; + + let i; + for (i = 0; i < args.length; i++) { + let arg = args[i]; + if (!arg.startsWith('-')) { + break; + } + + let label = arg.substr(1); + + if (options.indexOf(label + ':') > -1) { + let leftover = args.slice(i + 1).join(' '); + let matches = leftover.match(/^"(.+?)"/); + if (matches) { + optionValues[label] = matches[1]; + i += matches[0].split(' ').length; + } else { + i++; + optionValues[label] = args[i]; + } + } else if (options.indexOf(label) > -1) { + optionValues[label] = true; + } else { + break; + } + } + + return { + options: optionValues, + leftover: args.slice(i) + }; + }; + + client.embed = (title, description = '', fields = [], options = {}) => { + let url = options.url || ''; + let color = options.color || randomColor(); + + if (options.inline) { + if (fields.length % 3 === 2) { + fields.push({ name: '\u200b', value: '\u200b' }); + } + fields.forEach(obj => { + obj.inline = true; + }); + } + + return new RichEmbed({ fields, video: options.video || url }) + .setTitle(title) + .setColor(color) + .setDescription(description) + .setURL(url) + .setImage(options.image) + .setTimestamp(options.timestamp ? timestampToDate(options.timestamp) : null) + .setFooter(options.footer === true ? randomFooter() : (options.footer ? options.footer : ''), options.footer ? global.bot.user.avatarURL : undefined) + .setAuthor(options.author === undefined ? '' : options.author) + .setThumbnail(options.thumbnail); + }; };