Skip to content

Commit

Permalink
Refactor permission checks in account and login commands
Browse files Browse the repository at this point in the history
  • Loading branch information
ryandward committed Apr 15, 2024
1 parent c8ee9a5 commit 8882dd9
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
12 changes: 11 additions & 1 deletion commands/utility/account.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import { AutocompleteInteraction, CommandInteraction, SlashCommandBuilder } from 'discord.js';
import {
AutocompleteInteraction,
CommandInteraction,
GuildMember,
SlashCommandBuilder,
} from 'discord.js';
import { FindManyOptions, ILike } from 'typeorm';
import { AppDataSource } from '../../app_data.js';
import { SharedAccounts } from '../../entities/SharedAccounts.js';
Expand All @@ -20,7 +25,12 @@ export const data = new SlashCommandBuilder()
);

export async function autocomplete(interaction: AutocompleteInteraction) {
const member = interaction.member as GuildMember;
try {
const hasPermission = member?.roles.cache.some(role => role.name === 'Officer');
if (!hasPermission) {
throw new Error('You do not have permission to use this command.');
}
const focusedOption = interaction.options.getFocused(true);
if (!focusedOption) return;

Expand Down
12 changes: 11 additions & 1 deletion commands/utility/login.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import { AutocompleteInteraction, CommandInteraction, SlashCommandBuilder } from 'discord.js';
import {
AutocompleteInteraction,
CommandInteraction,
GuildMember,
SlashCommandBuilder,
} from 'discord.js';
import _ from 'lodash';
import { FindManyOptions, ILike } from 'typeorm';
import { AppDataSource } from '../../app_data.js';
Expand Down Expand Up @@ -69,7 +74,12 @@ export async function execute(interaction: CommandInteraction): Promise<void> {
const toonName = _.capitalize(options.get('toon')?.value as string);
const accountName = options.get('account')?.value as string;

const member = interaction.member as GuildMember;
try {
const hasPermission = member?.roles.cache.some(role => role.name === 'Officer');
if (!hasPermission) {
throw new Error('You do not have permission to use this command.');
}
let sharedToon = await AppDataSource.manager.findOne(SharedToons, {
where: { Name: toonName },
relations: ['Account'],
Expand Down

0 comments on commit 8882dd9

Please sign in to comment.