feat: beginning of moderation

This commit is contained in:
Rauf 2020-01-25 12:02:34 -05:00
parent 2e3e0bbe0a
commit 07029ffc52
8 changed files with 115 additions and 29 deletions

View file

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

View file

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

View file

@ -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}'],
}
);

View file

@ -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<string, Plugin>) {
function convertPlugins(
plugins: Collection<string, Plugin>,
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]) => {

View file

@ -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]',
],
}
);

View file

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

5
src/util/DefaultEmbed.ts Normal file
View file

@ -0,0 +1,5 @@
import { MessageEmbed } from 'discord.js';
export function defaultEmbed() {
return new MessageEmbed().setColor(0x7289da).setTimestamp();
}

13
src/util/parseUser.ts Normal file
View file

@ -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;
}
}