Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

auth | add role permissions for commands #112

Merged
merged 2 commits into from
Jul 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions src/common/decoratos/role.decorator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { CommandInteraction } from 'discord.js';

// Role decorator factory
export function Role(requiredRole: CommandAccessLevel) {
return function (
target,
propertyKey: string,
descriptor: PropertyDescriptor,
) {
const originalMethod = descriptor.value;

descriptor.value = async function (...args) {
const interaction: CommandInteraction = args[0];
const roles = interaction.guild.roles.cache;
if (
!roles.has(requiredRole) &&
requiredRole !== CommandAccessLevel.member
) {
await interaction.reply({
content: 'You do not have the required role to execute this command.',
ephemeral: true,
});
return false;
}
return originalMethod.apply(this, args);
};
};
}

export enum CommandAccessLevel {
Developer = '1265375756637180027',
vip = '484479705148293120',
member = '484479403817173002',
}
76 changes: 39 additions & 37 deletions src/modules/command/commands/activate-birthday-shoutout.ts
Original file line number Diff line number Diff line change
@@ -1,41 +1,43 @@
import {
CacheType,
CommandInteraction,
SlashCommandBuilder,
} from 'discord.js';
import { ACommand } from '../command.abstract';
import { Inject } from '@nestjs/common';
import { CacheType, CommandInteraction, SlashCommandBuilder } from 'discord.js';
import { ACommand } from '../command.abstract';
import { Inject } from '@nestjs/common';
import { BirthdayEntryService } from '../../models/birthday/service/birthday-entry.service';
import { CreateOrUpdateBirthdayEntryDto } from '../../models/birthday/dto/create-or-update-birthday-entry.dto';

export default class ActivateBirthdayEntryShoutoutCommand extends ACommand {
constructor(
@Inject(BirthdayEntryService)
private readonly birthdayEntryService: BirthdayEntryService,
) {
super();
}
data = new SlashCommandBuilder()
.setName('activate-birthday-shoutout')
.setDescription(
'Use this if you want Bingus to shout you out on your birthday (default)!',
);

async execute(arg: CommandInteraction<CacheType>): Promise<boolean> {
await arg.deferReply();
const instance: CreateOrUpdateBirthdayEntryDto ={
username: arg.user.username,
secName: arg.user.displayName,
active: true
};
const inactive =
await this.birthdayEntryService.updateBirthdayEntry(instance);
if (!inactive) {
await arg.editReply({ content: 'I don\'t know your birthday yet! 🎉'});
} else {
await arg.editReply({ content: 'You will now receive a birthday shoutout from Bingus! 🎉'});
}
return true;
import {
CommandAccessLevel,
Role,
} from '../../../common/decoratos/role.decorator';

export default class ActivateBirthdayEntryShoutoutCommand extends ACommand {
constructor(
@Inject(BirthdayEntryService)
private readonly birthdayEntryService: BirthdayEntryService,
) {
super();
}
data = new SlashCommandBuilder()
.setName('activate-birthday-shoutout')
.setDescription(
'Use this if you want Bingus to shout you out on your birthday (default)!',
);

@Role(CommandAccessLevel.member)
async execute(arg: CommandInteraction<CacheType>): Promise<boolean> {
await arg.deferReply();
const instance: CreateOrUpdateBirthdayEntryDto = {
username: arg.user.username,
secName: arg.user.displayName,
active: true,
};
const inactive =
await this.birthdayEntryService.updateBirthdayEntry(instance);
if (!inactive) {
await arg.editReply({ content: "I don't know your birthday yet! 🎉" });
} else {
await arg.editReply({
content: 'You will now receive a birthday shoutout from Bingus! 🎉',
});
}
return true;
}
}
5 changes: 5 additions & 0 deletions src/modules/command/commands/add-birthday-entry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ import { ACommand } from '../command.abstract';
import { Inject } from '@nestjs/common';
import { BirthdayEntryService } from '../../models/birthday/service/birthday-entry.service';
import { CreateOrUpdateBirthdayEntryDto } from '../../models/birthday/dto/create-or-update-birthday-entry.dto';
import {
CommandAccessLevel,
Role,
} from '../../../common/decoratos/role.decorator';

export default class AddBirthdayEntryCommand extends ACommand {
constructor(
Expand All @@ -31,6 +35,7 @@ export default class AddBirthdayEntryCommand extends ACommand {
option.setName('year').setDescription('your year of birth'),
);

@Role(CommandAccessLevel.member)
async execute(arg: CommandInteraction<CacheType>): Promise<boolean> {
const day = arg.options.get('day');
const month = arg.options.get('month');
Expand Down
9 changes: 8 additions & 1 deletion src/modules/command/commands/bug.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
import { Injectable } from '@nestjs/common';
import { SlashCommandBuilder } from 'discord.js';
import { ACommand } from '../command.abstract';
import {
CommandAccessLevel,
Role,
} from '../../../common/decoratos/role.decorator';

@Injectable()
export class BugReport extends ACommand {
data = new SlashCommandBuilder().setName('bug').setDescription('report a bug to my maintainers!');
data = new SlashCommandBuilder()
.setName('bug')
.setDescription('report a bug to my maintainers!');

@Role(CommandAccessLevel.member)
async execute(interaction) {
return this.run(async () => {
await interaction.reply(
Expand Down
9 changes: 8 additions & 1 deletion src/modules/command/commands/cbd.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
import { Injectable } from '@nestjs/common';
import { SlashCommandBuilder } from 'discord.js';
import { ACommand } from '../command.abstract';
import {
CommandAccessLevel,
Role,
} from '../../../common/decoratos/role.decorator';

@Injectable()
export class CBDCommand extends ACommand {
data = new SlashCommandBuilder().setName('cbd').setDescription('der hurensohn!');
data = new SlashCommandBuilder()
.setName('cbd')
.setDescription('der hurensohn!');

@Role(CommandAccessLevel.vip)
async execute(interaction) {
return this.run(async () => {
await interaction.reply('Wenn der Hurensohn nur wüsste...');
Expand Down
9 changes: 8 additions & 1 deletion src/modules/command/commands/coinflip.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
import { Injectable } from '@nestjs/common';
import { SlashCommandBuilder } from 'discord.js';
import { ACommand } from '../command.abstract';
import {
CommandAccessLevel,
Role,
} from '../../../common/decoratos/role.decorator';

@Injectable()
export class CoinflipCommand extends ACommand {
data = new SlashCommandBuilder().setName('coinflip').setDescription('Bingus flips a coin for you');
data = new SlashCommandBuilder()
.setName('coinflip')
.setDescription('Bingus flips a coin for you');

@Role(CommandAccessLevel.member)
async execute(interaction) {
return this.run(async () => {
await interaction.reply(this.coinflip());
Expand Down
5 changes: 5 additions & 0 deletions src/modules/command/commands/deactivate-birthday-shoutout.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ import { ACommand } from '../command.abstract';
import { Inject } from '@nestjs/common';
import { BirthdayEntryService } from '../../models/birthday/service/birthday-entry.service';
import { CreateOrUpdateBirthdayEntryDto } from '../../models/birthday/dto/create-or-update-birthday-entry.dto';
import {
CommandAccessLevel,
Role,
} from '../../../common/decoratos/role.decorator';

export default class DeactivateBirthdayEntryShoutoutCommand extends ACommand {
constructor(
Expand All @@ -17,6 +21,7 @@ export default class DeactivateBirthdayEntryShoutoutCommand extends ACommand {
"Use this if you don't want Bingus to shout you out on your birthday!",
);

@Role(CommandAccessLevel.member)
async execute(arg: CommandInteraction<CacheType>): Promise<boolean> {
await arg.deferReply();
const instance: CreateOrUpdateBirthdayEntryDto = {
Expand Down
5 changes: 5 additions & 0 deletions src/modules/command/commands/get-a-quote.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ import {
import { ACommand } from '../command.abstract';
import { Inject } from '@nestjs/common';
import { SomeoneOnceSaidService } from '../../models/someone-once-said/service/someone-once-said.service';
import {
CommandAccessLevel,
Role,
} from '../../../common/decoratos/role.decorator';

export default class GetRandomQuote extends ACommand {
constructor(
Expand All @@ -19,6 +23,7 @@ export default class GetRandomQuote extends ACommand {
.setName('randomquote')
.setDescription('get a random quote of a user');

@Role(CommandAccessLevel.member)
async execute(arg: CommandInteraction<CacheType>): Promise<boolean> {
const someoneOnceSaid = await this.someoneonceSaidService.getRandomQuote();
if (!someoneOnceSaid) return;
Expand Down
9 changes: 8 additions & 1 deletion src/modules/command/commands/gold.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
import { Injectable } from '@nestjs/common';
import { SlashCommandBuilder } from 'discord.js';
import { ACommand } from '../command.abstract';
import {
CommandAccessLevel,
Role,
} from '../../../common/decoratos/role.decorator';

@Injectable()
export class GoldCommand extends ACommand {
data = new SlashCommandBuilder().setName('gold').setDescription('schweigen ist gold!');
data = new SlashCommandBuilder()
.setName('gold')
.setDescription('schweigen ist gold!');

@Role(CommandAccessLevel.vip)
public execute(arg: any /*Interaction<CacheType>*/): Promise<boolean> {
return this.run(async () => {
await arg.reply({
Expand Down
5 changes: 5 additions & 0 deletions src/modules/command/commands/hello.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
import { Injectable } from '@nestjs/common';
import { SlashCommandBuilder } from 'discord.js';
import { ACommand } from '../command.abstract';
import {
CommandAccessLevel,
Role,
} from '../../../common/decoratos/role.decorator';

@Injectable()
export class HelloCommand extends ACommand {
data = new SlashCommandBuilder()
.setName('hello')
.setDescription('Hello World!');

@Role(CommandAccessLevel.Developer)
async execute(interaction) {
return this.run(async () => {
await interaction.reply('moin!');
Expand Down
5 changes: 5 additions & 0 deletions src/modules/command/commands/pingpong.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
import { Injectable } from '@nestjs/common';
import { SlashCommandBuilder } from 'discord.js';
import { ACommand } from '../command.abstract';
import {
CommandAccessLevel,
Role,
} from '../../../common/decoratos/role.decorator';

@Injectable()
export class PingPongCommand extends ACommand {
data = new SlashCommandBuilder()
.setName('ping')
.setDescription('Replies with Pong!');

@Role(CommandAccessLevel.Developer)
public execute(arg: any /*Interaction<CacheType>*/): Promise<boolean> {
return this.run(async () => {
await arg.reply('Pong!');
Expand Down
15 changes: 6 additions & 9 deletions src/modules/command/commands/poll.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
import { Inject, Injectable } from '@nestjs/common';
import {
ActionRowBuilder,
ButtonBuilder,
ButtonStyle,
CacheType,
CommandInteraction,
EmbedBuilder,
SlashCommandBuilder,
} from 'discord.js';
import { CacheType, CommandInteraction, SlashCommandBuilder } from 'discord.js';
import { ACommand } from '../command.abstract';
import { PollService } from '../../models/poll/service/poll.service';
import {
CommandAccessLevel,
Role,
} from '../../../common/decoratos/role.decorator';

@Injectable()
export class PollCommand extends ACommand {
Expand All @@ -31,6 +27,7 @@ export class PollCommand extends ACommand {
.setRequired(true),
);

@Role(CommandAccessLevel.member)
public execute(arg: CommandInteraction<CacheType>): Promise<boolean> {
return this.run(async () => {
this.pollService.create(arg);
Expand Down
9 changes: 8 additions & 1 deletion src/modules/command/commands/reported.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
import { Injectable } from '@nestjs/common';
import { SlashCommandBuilder } from 'discord.js';
import { ACommand } from '../command.abstract';
import {
CommandAccessLevel,
Role,
} from '../../../common/decoratos/role.decorator';

@Injectable()
export class ReportedCommand extends ACommand {
data = new SlashCommandBuilder().setName('reported').setDescription('swag reports someone');
data = new SlashCommandBuilder()
.setName('reported')
.setDescription('swag reports someone');

@Role(CommandAccessLevel.member)
public execute(arg: any /*Interaction<CacheType>*/): Promise<boolean> {
return this.run(async () => {
await arg.reply({
Expand Down
5 changes: 5 additions & 0 deletions src/modules/command/commands/someone-once-said.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ import { ACommand } from '../command.abstract';
import { Inject } from '@nestjs/common';
import { SomeoneOnceSaid } from '../../../schemas/someone-once-said.schema';
import { SomeoneOnceSaidService } from '../../models/someone-once-said/service/someone-once-said.service';
import {
CommandAccessLevel,
Role,
} from '../../../common/decoratos/role.decorator';

export default class SomeoneOnceSaidCommand extends ACommand {
constructor(
Expand All @@ -23,6 +27,7 @@ export default class SomeoneOnceSaidCommand extends ACommand {
option.setName('phrase').setDescription('What was said'),
);

@Role(CommandAccessLevel.vip)
async execute(arg: CommandInteraction<CacheType>): Promise<boolean> {
const phrase = arg.options.get('phrase');
if (!phrase) {
Expand Down
5 changes: 5 additions & 0 deletions src/modules/command/commands/version.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ import { Inject, Injectable } from '@nestjs/common';
import { SlashCommandBuilder } from 'discord.js';
import { ACommand } from '../command.abstract';
import { VersionService } from '../../models/version/service/version.service';
import {
CommandAccessLevel,
Role,
} from '../../../common/decoratos/role.decorator';

@Injectable()
export class VersionCommand extends ACommand {
Expand All @@ -16,6 +20,7 @@ export class VersionCommand extends ACommand {
'show the version of the currently running bingus instance',
);

@Role(CommandAccessLevel.Developer)
async execute(interaction) {
const version = await this.versionService.getVersion();
return this.run(async () => {
Expand Down