Skip to content

Commit

Permalink
feat: add possibility to specify elevated roles (#85)
Browse files Browse the repository at this point in the history
  • Loading branch information
ibito authored Jul 12, 2020
1 parent dc18b1a commit 0b02319
Show file tree
Hide file tree
Showing 12 changed files with 52 additions and 16 deletions.
3 changes: 2 additions & 1 deletion config/config.example.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@
"stayInChannel": false,
"timeout": 10,
"deafen": false,
"game": ""
"game": "",
"elevatedRoles": ["sound remover", "moderator"]
}
7 changes: 5 additions & 2 deletions src/bot/commands/AvatarCommand.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { ClientUser, Message, Permissions } from 'discord.js';
import { ClientUser, Message } from 'discord.js';

import Config from '@config/Config';
import localize from '@util/i18n/localize';
import UserCommand from './base/UserCommand';
import userHasElevatedRole from './helpers/userHasElevatedRole';

export default class AvatarCommand implements UserCommand {
public readonly TRIGGERS = ['avatar'];
Expand All @@ -22,7 +23,9 @@ export default class AvatarCommand implements UserCommand {

public run(message: Message, params: string[]) {
if (!message.member) return;
if (!message.member.hasPermission(Permissions.FLAGS.ADMINISTRATOR!)) return;

const allowedToRunCommand = userHasElevatedRole(message.member);
if (!allowedToRunCommand) return;

if (params.length === this.NUMBER_OF_PARAMETERS && params[0] === 'remove') {
this.user.setAvatar('');
Expand Down
7 changes: 5 additions & 2 deletions src/bot/commands/ConfigCommand.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { ClientUser, Message, Permissions } from 'discord.js';
import { ClientUser, Message } from 'discord.js';

import Config from '@config/Config';
import localize from '@util/i18n/localize';
import Command from './base/Command';
import userHasElevatedRole from './helpers/userHasElevatedRole';

export default class ConfigCommand implements Command {
public readonly TRIGGERS = ['config', 'set'];
Expand All @@ -22,7 +23,9 @@ export default class ConfigCommand implements Command {

public run(message: Message, params: string[]) {
if (!message.member) return;
if (!message.member.hasPermission(Permissions.FLAGS.ADMINISTRATOR!)) return;

const allowedToRunCommand = userHasElevatedRole(message.member);
if (!allowedToRunCommand) return;

if (params.length < this.NUMBER_OF_PARAMETERS) {
message.channel.send(this.USAGE);
Expand Down
7 changes: 5 additions & 2 deletions src/bot/commands/IgnoreCommand.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
import { Message, Permissions } from 'discord.js';
import { Message } from 'discord.js';

import * as ignoreList from '@util/db/IgnoreList';
import localize from '@util/i18n/localize';
import Command from './base/Command';
import userHasElevatedRole from './helpers/userHasElevatedRole';

export default class IgnoreCommand implements Command {
public readonly TRIGGERS = ['ignore'];
public readonly USAGE = 'Usage: !ignore <user>';

public run(message: Message) {
if (!message.member) return;
if (!message.member.hasPermission(Permissions.FLAGS.ADMINISTRATOR!)) return;

const allowedToRunCommand = userHasElevatedRole(message.member);
if (!allowedToRunCommand) return;

const { users } = message.mentions;
if (users.size < 1) {
Expand Down
7 changes: 5 additions & 2 deletions src/bot/commands/RemoveCommand.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import fs from 'fs';

import { Message, Permissions } from 'discord.js';
import { Message } from 'discord.js';

import * as sounds from '@util/db/Sounds';
import localize from '@util/i18n/localize';
import { existsSound, getPathForSound } from '@util/SoundUtil';
import Command from './base/Command';
import userHasElevatedRole from './helpers/userHasElevatedRole';

export default class RemoveCommand implements Command {
public readonly TRIGGERS = ['remove'];
Expand All @@ -14,7 +15,9 @@ export default class RemoveCommand implements Command {

public run(message: Message, params: string[]) {
if (!message.member) return;
if (!message.member.hasPermission(Permissions.FLAGS.ADMINISTRATOR!)) return;

const allowedToRunCommand = userHasElevatedRole(message.member);
if (!allowedToRunCommand) return;

if (params.length !== this.NUMBER_OF_PARAMETERS) {
message.channel.send(this.USAGE);
Expand Down
7 changes: 5 additions & 2 deletions src/bot/commands/RenameCommand.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import fs from 'fs';

import { Message, Permissions } from 'discord.js';
import { Message } from 'discord.js';

import * as soundsDb from '@util/db/Sounds';
import localize from '@util/i18n/localize';
import { getExtensionForSound, getSounds } from '@util/SoundUtil';
import Command from './base/Command';
import userHasElevatedRole from './helpers/userHasElevatedRole';

export default class RenameCommand implements Command {
public readonly TRIGGERS = ['rename'];
Expand All @@ -14,7 +15,9 @@ export default class RenameCommand implements Command {

public run(message: Message, params: string[]) {
if (!message.member) return;
if (!message.member.hasPermission(Permissions.FLAGS.ADMINISTRATOR!)) return;

const allowedToRunCommand = userHasElevatedRole(message.member);
if (!allowedToRunCommand) return;

if (params.length !== this.NUMBER_OF_PARAMETERS) {
message.channel.send(this.USAGE);
Expand Down
7 changes: 5 additions & 2 deletions src/bot/commands/TagCommand.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { Message, Permissions } from 'discord.js';
import { Message } from 'discord.js';

import * as sounds from '@util/db/Sounds';
import localize from '@util/i18n/localize';
import { getSounds } from '@util/SoundUtil';
import Command from './base/Command';
import userHasElevatedRole from './helpers/userHasElevatedRole';

export default class TagCommand implements Command {
public readonly TRIGGERS = ['tag'];
Expand All @@ -30,7 +31,9 @@ export default class TagCommand implements Command {

if (params[0] === 'clear') {
if (!message.member) return;
if (!message.member.hasPermission(Permissions.FLAGS.ADMINISTRATOR!)) return;

const allowedToRunCommand = userHasElevatedRole(message.member);
if (allowedToRunCommand) return;

sounds.clearTags(sound);
return;
Expand Down
7 changes: 5 additions & 2 deletions src/bot/commands/UnignoreCommand.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
import { Message, Permissions } from 'discord.js';
import { Message } from 'discord.js';

import * as ignoreList from '@util/db/IgnoreList';
import localize from '@util/i18n/localize';
import Command from './base/Command';
import userHasElevatedRole from './helpers/userHasElevatedRole';

export default class UnignoreCommand implements Command {
public readonly TRIGGERS = ['unignore'];
public readonly USAGE = 'Usage: !unignore <user>';

public run(message: Message) {
if (!message.member) return;
if (!message.member.hasPermission(Permissions.FLAGS.ADMINISTRATOR!)) return;

const allowedToRunCommand = userHasElevatedRole(message.member);
if (!allowedToRunCommand) return;

const { users } = message.mentions;
if (users.size < 1) {
Expand Down
11 changes: 11 additions & 0 deletions src/bot/commands/helpers/userHasElevatedRole.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { config } from '@util/Container';
import { GuildMember, Permissions } from 'discord.js';

const userHasElevatedRole = (member: GuildMember) =>
member.roles.cache.some(
r =>
config.elevatedRoles.includes(r.name) ||
member.hasPermission(Permissions.FLAGS.ADMINISTRATOR!)
);

export default userHasElevatedRole;
1 change: 1 addition & 0 deletions src/config/Config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export default class Config implements ConfigInterface {
public timeout!: number;
public deafen!: boolean;
public game!: string;
public elevatedRoles!: string[];

private readonly CONFIG_PATH = path.join(process.cwd(), 'config', 'config.json');
private readonly MODIFIABLE_FIELDS = [
Expand Down
1 change: 1 addition & 0 deletions src/config/ConfigInterface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@ export default interface ConfigInterface {
timeout?: number;
deafen?: boolean;
game?: string;
elevatedRoles: string[];
}
3 changes: 2 additions & 1 deletion src/config/DefaultConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ const DEFAULT_CONFIG: ConfigInterface = {
stayInChannel: false,
timeout: 10, // Minutes
deafen: false,
game: 'SoundBoard'
game: 'SoundBoard',
elevatedRoles: ['Moderator']
};

export default DEFAULT_CONFIG;

0 comments on commit 0b02319

Please sign in to comment.