diff --git a/src/plugins/debug/ping.ts b/src/plugins/debug/ping.ts index 04f6b7e..51aee23 100644 --- a/src/plugins/debug/ping.ts +++ b/src/plugins/debug/ping.ts @@ -1,5 +1,5 @@ import { Command } from '../Command'; -import { MessageEmbed } from 'discord.js'; +import { defaultEmbed } from '../../util/DefaultEmbed'; export const command = new Command( 'ping', @@ -7,19 +7,17 @@ export const command = new Command( const m = await msg.channel.send('Ping?'); m.delete({ timeout: 100 }); - const embed = new MessageEmbed() + const embed = defaultEmbed() .setTitle('Pong! :ping_pong:') .addField('Bot Latency', `${Math.round(lifeguard.ws.ping)}ms`) .addField( 'Message Latency', `${m.createdTimestamp - msg.createdTimestamp}ms` ) - .setColor(0x7289da) .setFooter( `Executed By ${msg.author.tag}`, msg.author.avatarURL() ?? msg.author.defaultAvatarURL - ) - .setTimestamp(); + ); msg.channel.send(embed); }, diff --git a/src/plugins/dev/eval.ts b/src/plugins/dev/eval.ts index 73d8b7f..e592c74 100644 --- a/src/plugins/dev/eval.ts +++ b/src/plugins/dev/eval.ts @@ -1,7 +1,7 @@ import { Command } from '../Command'; -import { MessageEmbed } from 'discord.js'; import { inspect } from 'util'; import { runInNewContext } from 'vm'; +import { defaultEmbed } from '../../util/DefaultEmbed'; function parseBlock(script: string) { const cbr = /^(([ \t]*`{3,4})([^\n]*)([\s\S]+?)(^[ \t]*\2))/gm; @@ -47,7 +47,7 @@ export const command = new Command( { lifeguard, msg, - MessageEmbed, + defaultEmbed, dbUser, }, { filename: msg.guild?.id.toString() } @@ -56,20 +56,16 @@ export const command = new Command( const end = Date.now(); if (typeof exec === 'string') { - const embed = new MessageEmbed() + const embed = defaultEmbed() .addField('Input', makeCodeBlock(script, 'js')) .addField('Output', makeCodeBlock(exec, 'js')) - .setFooter(`Script Executed in ${end - start}ms`) - .setTimestamp() - .setColor(0x7289da); + .setFooter(`Script Executed in ${end - start}ms`); msg.channel.send(embed); } else { - const embed = new MessageEmbed() + const embed = defaultEmbed() .addField('Input', makeCodeBlock(script, 'js')) .addField('Output', makeCodeBlock(`${exec.name}: ${exec.message}`)) - .setFooter(`Script Executed in ${end - start}ms`) - .setTimestamp() - .setColor(0x7289da); + .setFooter(`Script Executed in ${end - start}ms`); msg.channel.send(embed); } }, diff --git a/src/plugins/global/blacklist.ts b/src/plugins/global/blacklist.ts new file mode 100644 index 0000000..972f4ad --- /dev/null +++ b/src/plugins/global/blacklist.ts @@ -0,0 +1,23 @@ +import { Command } from '../Command'; +import { parseUser } from '../../util/parseUser'; + +export const command = new Command( + 'blacklist', + async (lifeguard, msg, args) => { + const u = parseUser(args[0]); + try { + await lifeguard.db.users.findOneAndUpdate( + { id: u }, + { $set: { blacklisted: true } }, + { returnOriginal: false } + ); + msg.channel.send(`<@${u}> was sucessfully blacklisted`); + } catch (err) { + msg.channel.send(err.message); + } + }, + { + level: 4, + usage: ['blacklist {user}'], + } +); diff --git a/src/plugins/info/help.ts b/src/plugins/info/help.ts index 39317c9..d9f5052 100644 --- a/src/plugins/info/help.ts +++ b/src/plugins/info/help.ts @@ -1,13 +1,20 @@ import { Command } from '../Command'; -import { MessageEmbed, Collection } from 'discord.js'; +import { MessageEmbed, Collection, GuildMember, Guild } from 'discord.js'; import { Plugin } from '../Plugin'; +import { calcUserLevel } from '../../assertions/userLevel'; +import { defaultEmbed } from '../../util/DefaultEmbed'; -function convertPlugins(plugins: Collection) { +function convertPlugins( + plugins: Collection, + member: GuildMember, + guild: Guild +) { return plugins .map((plugin, key) => ({ name: key, cmds: [...plugin.values()] .filter(cmd => !cmd.options.hidden) + .filter(cmd => calcUserLevel(member, guild) >= cmd.options.level) .map(cmd => cmd.name) .sort((a, b) => a.localeCompare(b)), })) @@ -18,19 +25,25 @@ export const command = new Command( 'help', (lifeguard, msg, args) => { if (!args.length) { - const plugins = convertPlugins(lifeguard.plugins); + const plugins = convertPlugins( + lifeguard.plugins, + msg.member as GuildMember, + msg.guild as Guild + ); - const embed = new MessageEmbed() + const embed = defaultEmbed() .setTitle('Lifeguard Help') - .setColor(0x7289da) .setFooter( `Executed By ${msg.author.tag}`, msg.author.avatarURL() ?? msg.author.defaultAvatarURL - ) - .setTimestamp(); + ); for (const plugin of plugins) { - embed.addField(plugin.name, plugin.cmds.join('\n')); + // console.log(plugin) + // embed.addField(plugin.name, plugin.cmds.join('\n')); + if (plugin.cmds.length > 0) { + embed.addField(plugin.name, plugin.cmds.join('\n')); + } } msg.channel.send(embed); @@ -39,14 +52,12 @@ export const command = new Command( const cmd = plugin?.get(args[0]); if (cmd) { - const embed = new MessageEmbed() + const embed = defaultEmbed() .setTitle(cmd.name) - .setColor(0x7289da) .setFooter( `Executed By ${msg.author.tag}`, msg.author.avatarURL() ?? msg.author.defaultAvatarURL - ) - .setTimestamp(); + ); const options = Object.entries(cmd.options); options.map(([key, val]) => { diff --git a/src/plugins/moderation/role.ts b/src/plugins/moderation/role.ts new file mode 100644 index 0000000..3ef1e80 --- /dev/null +++ b/src/plugins/moderation/role.ts @@ -0,0 +1,38 @@ +import { Command } from '../Command'; +import { parseUser } from '../../util/parseUser'; + +export const command = new Command( + 'role', + async (lifeguard, msg, args) => { + const [cmd, uid, rid, ...r] = args; + const u = parseUser(uid); + const role = msg.guild?.roles.get(rid); + switch (cmd) { + case 'add': + if (role) { + const member = msg.guild?.members.get(u); + member?.roles.add(role, r.join(' ')); + msg.channel.send(`Added ${role.name} to ${member}`); + } + break; + + case 'rmv': + if (role) { + const member = msg.guild?.members.get(u); + member?.roles.remove(role, r.join(' ')); + msg.channel.send(`Removed ${role.name} from ${member}`); + } + break; + + default: + break; + } + }, + { + level: 1, + usage: [ + 'role add {user} {role id} [reason]', + 'role rmv {user} {role id} [reason]', + ], + } +); diff --git a/src/plugins/pluginLoader.ts b/src/plugins/pluginLoader.ts index 5dc5dd3..96881f2 100644 --- a/src/plugins/pluginLoader.ts +++ b/src/plugins/pluginLoader.ts @@ -21,8 +21,10 @@ export async function PluginLoader() { const files = await readDir(`${folderDir}`); for await (const file of files) { if (file.endsWith('.js')) { - const command: Command = require(`./${folder}/${file}`).command; - plugin.set(command.name, command); + const command = require(`./${folder}/${file}`).command; + if (command instanceof Command) { + plugin.set(command.name, command); + } } } diff --git a/src/util/DefaultEmbed.ts b/src/util/DefaultEmbed.ts new file mode 100644 index 0000000..cdc0414 --- /dev/null +++ b/src/util/DefaultEmbed.ts @@ -0,0 +1,5 @@ +import { MessageEmbed } from 'discord.js'; + +export function defaultEmbed() { + return new MessageEmbed().setColor(0x7289da).setTimestamp(); +} diff --git a/src/util/parseUser.ts b/src/util/parseUser.ts new file mode 100644 index 0000000..cda0f25 --- /dev/null +++ b/src/util/parseUser.ts @@ -0,0 +1,13 @@ +export function parseUser(user: string) { + if (user.startsWith("<@") && user.endsWith(">")) { + user = user.slice(2, -1); + + if (user.startsWith("!")) { + user = user.slice(1); + } + + return user; + } else { + return user; + } +}