Skip to content

Commit

Permalink
Add user local/global blacklisting
Browse files Browse the repository at this point in the history
To prevent given users from triggering commands. Global blacklist will prevent in ALL guilds as well as DMs
  • Loading branch information
zajrik committed Mar 9, 2017
1 parent a7518df commit 12042f3
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 1 deletion.
14 changes: 13 additions & 1 deletion src/lib/command/CommandDispatcher.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { PermissionResolvable, TextChannel } from 'discord.js';
import { PermissionResolvable, TextChannel, User } from 'discord.js';
import { Message } from '../types/Message';
import { GuildStorage } from '../storage/GuildStorage';
import { Command } from '../command/Command';
Expand Down Expand Up @@ -32,6 +32,8 @@ export class CommandDispatcher<T extends Bot>
const dm: boolean = ['dm', 'group'].includes(message.channel.type);
if (!dm) message.guild.storage = this._bot.guildStorages.get(message.guild);

if (this.isBlacklisted(message.author, message, dm)) return;

const [commandCalled, command, prefix, name]: [boolean, Command<T>, string, string] = this.isCommandCalled(message);
if (!commandCalled) return;

Expand Down Expand Up @@ -168,6 +170,16 @@ export class CommandDispatcher<T extends Bot>
command.roles.includes(role.name)).size > 0;
}

/**
* Check if the calling user is blacklisted
*/
private isBlacklisted(user: User, message: Message, dm: boolean): boolean
{
if (this._bot.storage.exists(`blacklist/${user.id}`)) return true;
if (!dm && message.guild.storage.settingExists(`blacklist/${user.id}`)) return true;
return false;
}

/**
* Execute the provided command with the provided args
*/
Expand Down
54 changes: 54 additions & 0 deletions src/lib/command/base/blacklist/Blacklist.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import { Bot } from '../../../bot/Bot';
import { Message } from '../../../types/Message';
import { Command } from '../../Command';
import { Middleware } from '../../middleware/Middleware';
import { User } from 'discord.js';

export default class Blacklist extends Command<Bot>
{
public constructor(bot: Bot)
{
super(bot, {
name: 'blacklist',
description: 'Blacklist a user from calling commands',
aliases: ['bl'],
usage: '<prefix>blacklist <user>, [\'global\']',
extraHelp: 'If global, this will block the user from calling commands in ANY server and DMs',
group: 'base',
permissions: ['ADMINISTRATOR']
});

this.use(Middleware.resolveArgs({ '<user>': 'User' }));
this.use(Middleware.expect({ '<user>': 'User' }));
}

public async action(message: Message, [user, global]: [User, string]): Promise<Message | Message[]>
{
if (user.id === message.author.id)
return message.channel.send(`I don't think you want to blacklist yourself.`);

if (user.bot) return message.channel.send(
`Bots already cannot call commands and do not need to be blacklisted.`);

if (global === 'global')
{
if (!this.bot.isOwner(message.author))
return message.channel.send('Only bot owners may blacklist globally.');

if (this.bot.storage.exists(`blacklist/${user.id}`))
return message.channel.send('That user is already globally blacklisted.');

this.bot.storage.setItem(`blacklist/${user.id}`, true);
return message.channel.send(`Added ${user.username}#${user.discriminator} to the global blacklist.`);
}

if ((await message.guild.fetchMember(user)).hasPermission('ADMINISTRATOR'))
return message.channel.send('You may not use this command on that person.');

if (message.guild.storage.settingExists(`blacklist/${user.id}`))
return message.channel.send('That user is already blacklisted in this server.');

message.guild.storage.setSetting(`blacklist/${user.id}`, true);
return message.channel.send(`Added ${user.username}#${user.discriminator} to this server's blacklist.`);
}
}
45 changes: 45 additions & 0 deletions src/lib/command/base/blacklist/Whitelist.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { Bot } from '../../../bot/Bot';
import { Message } from '../../../types/Message';
import { Command } from '../../Command';
import { Middleware } from '../../middleware/Middleware';
import { User } from 'discord.js';

export default class Whitelist extends Command<Bot>
{
public constructor(bot: Bot)
{
super(bot, {
name: 'whitelist',
description: 'Remove a user from the command blacklist',
aliases: ['wl'],
usage: '<prefix>whitelist <user>, [\'global\']',
extraHelp: '',
group: 'base',
permissions: ['ADMINISTRATOR']
});

this.use(Middleware.resolveArgs({ '<user>': 'User' }));
this.use(Middleware.expect({ '<user>': 'User' }));
}

public action(message: Message, [user, global]: [User, string]): Promise<Message | Message[]>
{
if (global === 'global')
{
if (!this.bot.isOwner(message.author))
return message.channel.send('Only bot owners may remove a global blacklisting.');

if (!this.bot.storage.exists(`blacklist/${user.id}`))
return message.channel.send('That user is not currently globally blacklisted.');

this.bot.storage.removeItem(`blacklist/${user.id}`);
return message.channel.send(`Removed ${user.username}#${user.discriminator} from the global blacklist.`);
}

if (!message.guild.storage.settingExists(`blacklist/${user.id}`))
return message.channel.send('That user is not currently blacklisted in this server.');

message.guild.storage.removeSetting(`blacklist/${user.id}`);
return message.channel.send(`Removed ${user.username}#${user.discriminator} from this server's blacklist.`);
}
}

0 comments on commit 12042f3

Please sign in to comment.