basic command functionality

This commit is contained in:
Benjamin Kyd
2020-07-15 15:46:30 +01:00
parent 078e1ac1b3
commit 9561d377a9
5 changed files with 145 additions and 4 deletions

View File

@@ -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 {

View File

@@ -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}`);
//}
});

View File

@@ -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}\``)
}

View File

6
src/discordhelpers.js Normal file
View File

@@ -0,0 +1,6 @@
const Eris = require('eris');
module.exports.IsUserAdmin = function (member)
{
return member.permission.has('administrator') || member.id == process.env.BOT_OWNER;
}