From 9561d377a9e79c07c86db7e59c8cd6a81a17ff31 Mon Sep 17 00:00:00 2001 From: Benjamin Kyd Date: Wed, 15 Jul 2020 15:46:30 +0100 Subject: [PATCH] basic command functionality --- src/database.js | 14 ++++- src/discord.js | 8 ++- src/discordcommands.js | 121 ++++++++++++++++++++++++++++++++++++- src/discordembedbuilder.js | 0 src/discordhelpers.js | 6 ++ 5 files changed, 145 insertions(+), 4 deletions(-) create mode 100644 src/discordembedbuilder.js create mode 100644 src/discordhelpers.js diff --git a/src/database.js b/src/database.js index 4aab938..1e8047a 100644 --- a/src/database.js +++ b/src/database.js @@ -54,7 +54,7 @@ module.exports.NewGuild = async function(id, name, prefix, logchannel, logsettin id: id, name: name, prefix: prefix, - logchannel: logchannel, + logchannel: logchannel, // -1 if not set logsettings: logsettings, modcases: modcases }); @@ -65,6 +65,18 @@ module.exports.NewGuild = async function(id, name, prefix, logchannel, logsettin } } + +module.exports.FetchAllGuilds = async function() +{ + try { + let guild = await Guild.findAll(); + return guild == null ? -1 : guild; + } catch (e) { + Logger.error(`An error occured while fetching guilds : ${e}`); + return -1; + } +} + module.exports.FetchGuild = async function(id) { try { diff --git a/src/discord.js b/src/discord.js index 25d7d58..9dd0440 100644 --- a/src/discord.js +++ b/src/discord.js @@ -30,7 +30,13 @@ module.exports.setup = async function() default: type = 3; break; } - this.bot.editStatus('online', {name: game, type: type}); + this.bot.editStatus('online', {name: game, type: type});[] + + //let array = await this.bot.getMessages('346104470901358595', 10) + //for (message of array) + //{ + // console.log(`${message.author.username}#${message.author.discriminator}: ${message.content}`); + //} }); diff --git a/src/discordcommands.js b/src/discordcommands.js index 7ee75d8..a489abd 100644 --- a/src/discordcommands.js +++ b/src/discordcommands.js @@ -1,13 +1,130 @@ const Logger = require('./logger.js'); +const Database = require('./database.js'); +const Discord = require('./discord.js'); +const DiscordHelpers = require('./discordhelpers.js'); -let Commands = {}; +let Commands = []; + +let GuildsAndPrefixs = []; module.exports.registerCommands = async function() { Logger.info('Registering commands'); + + Commands['initserv'] = { command: 'initserv', alias: 'nil', name: 'Initialize Server', desc: 'Initialises a new Guild', callback: InitializeGuild, adminOnly: true }; + Commands['setprefix'] = { command: 'setprefix', alias: 'prefix', name: 'Set Prefix', desc: 'Sets the servers prefix for the guild', callback: SetPrefix, adminOnly: true }; + + + // create a cache of prefix's so that the database doesn't have to be + // queried every single time, new guilds should also add themselve's + // and shit like that + + const guilds = await Database.FetchAllGuilds(); + + for (guild of guilds) + { + GuildsAndPrefixs[guild.id] = guild.prefix; + } + + console.log(GuildsAndPrefixs); + } module.exports.newMessage = async function(message) { - console.log(message.content); + // console.log(message.content); + + // If there is no guild in the prefix cache + if (!GuildsAndPrefixs[message.guildID]) + { + let res = await Database.FetchGuild(message.guildID); + if (res == -1) + { + let GuildName = ''; + try { + GuildName = (await Discord.bot.getRESTGuild(message.guildID)).name; + } catch (e) { + GuildName = 'not defined'; + } + Database.NewGuild(message.guildID, GuildName, '*', -1, {}, 0); + GuildsAndPrefixs[message.guildID] = '*' + } else + { + GuildsAndPrefixs[message.guildID] = '*' + } + } + + const msg = message.content.split(' '); + + // does the message start with the prefix + if (!msg[0].startsWith(GuildsAndPrefixs[message.guildID])) return; + + // splits the command from the prefix + const command = msg[0].split(GuildsAndPrefixs[message.guildID])[1].toLowerCase(); + + // splits the args from the command + const args = msg.slice(1, msg.length); + + + const CallCommand = (command) => { + // check if admin is required + if (command.adminOnly) + { + if (DiscordHelpers.IsUserAdmin(message.member)) + { + command.callback(message, args); + return; + } + Discord.bot.createMessage(message.channel.id, 'The `administrator` privaledge is required to execute that command'); + return; + } + command.callback(message, args); + }; + + + // command found in array + if (Commands[command]) + { + CallCommand(Commands[command]); + return; + } + + // checking alias' + // TODO: make this more efficient + // not really needed though as there + // will be AT MOST 20 commands + + for (cmd in Commands) + { + if (Commands[cmd].alias == 'nil') continue; + if (Commands[cmd].alias == command) + { + CallCommand(Commands[cmd]); + break; + } + } + +} + +async function InitializeGuild(message, args) +{ + console.log('InitGuild called'); +} + +async function SetPrefix(message, args) +{ + if (!args[0]) + { + Discord.bot.createMessage(message.channel.id, 'You must provide a new prefix'); + return; + } + + const NewPrefix = args[0]; + + await Database.UpdateGuildPrefix(message.guildID, NewPrefix); + + // Update cache + GuildsAndPrefixs[message.guildID] = NewPrefix; + + Discord.bot.createMessage(message.channel.id, `New prefix for guild : \`${NewPrefix}\``) } diff --git a/src/discordembedbuilder.js b/src/discordembedbuilder.js new file mode 100644 index 0000000..e69de29 diff --git a/src/discordhelpers.js b/src/discordhelpers.js new file mode 100644 index 0000000..1872cd7 --- /dev/null +++ b/src/discordhelpers.js @@ -0,0 +1,6 @@ +const Eris = require('eris'); + +module.exports.IsUserAdmin = function (member) +{ + return member.permission.has('administrator') || member.id == process.env.BOT_OWNER; +}