Skip to content

Commit

Permalink
chore: Add permissions to Discord commands
Browse files Browse the repository at this point in the history
  • Loading branch information
PartMan7 committed Oct 30, 2024
1 parent 515923c commit 31e4057
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 4 deletions.
3 changes: 2 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ PS_PASSWORD=PASSWORD
PS_ADMINS=PartMan,FakePart
PS_ROOMS=botdevelopment

DISCORD_TOKEN=TOKEN_HERE
DISCORD_CLIENT_ID=ID_HERE
DISCORD_TOKEN=TOKEN_HERE
DISCORD_ADMINS=ADMIN_ID_1,ADMIN_ID_2

WEB_PORT=8080

Expand Down
1 change: 1 addition & 0 deletions src/config/discord.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ import '@/env';

export const token = process.env.DISCORD_TOKEN ?? 'token';
export const clientId = process.env.DISCORD_CLIENT_ID ?? 'client_id';
export const admins = process.env.DISCORD_ADMINS?.split(/ *, */) ?? [];
1 change: 1 addition & 0 deletions src/config/ps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import '@/env';
import { Tools } from 'ps-client';

export const ranks = ['locked', 'muted', 'regular', 'whitelist', 'voice', 'driver', 'mod', 'bot', 'owner', 'admin'] as const;

export const owner = process.env.PS_OWNER ?? 'PartMan';
const _admins = process.env.PS_ADMINS?.split(/ *, */) ?? [];
export const admins = _admins.map(Tools.toID);
Expand Down
9 changes: 9 additions & 0 deletions src/discord/handlers/chat.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,22 @@
import { DiscCommands } from '@/cache';
import { admins } from '@/config/discord';

import type { Interaction } from 'discord.js';
import { ACCESS_DENIED } from '@/text';

export default async function messageHandler(interaction: Interaction): Promise<void> {
if (!interaction.isChatInputCommand()) return;

const command = DiscCommands[interaction.commandName];
if (!command) return;

if (command.perms === 'admin') {
if (!admins.includes(interaction.user.id)) throw new ChatError(ACCESS_DENIED);
}
if (typeof command.perms === 'function') {
if (!command.perms(interaction)) throw new ChatError(ACCESS_DENIED);
}

try {
await command.run(interaction);
} catch (err) {
Expand Down
4 changes: 2 additions & 2 deletions src/types/chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,15 +153,15 @@ export type DiscCommand = {
// If enabled, replaces 'access denied' errors with 'command not found'
conceal?: true;
// Ensures a command can only be run from a room
roomOnly?: true;
serverOnly?: true;
// Ensures a command can only be run from a PM
pmOnly?: true;
};
/**
* Aliases for the command.
*/
aliases?: string[];
perms?: 'admin' | ((message: DiscInteraction) => boolean);
perms?: 'admin' | ((interaction: DiscInteraction) => boolean);
servers?: string[];
args?: null; // TODO
run(interaction: DiscInteraction): Promise<any>;
Expand Down
3 changes: 2 additions & 1 deletion src/web/loaders/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ export function renderReact(req: Request, res: Response, next: NextFunction): vo
const page = await renderTemplate('static-react.html', { title, content });
return res.send(page);
}
// TODO: Hydrate interactive content
const preHydrated = renderToString(jsx);
const page = await renderTemplate('react.html', { title, preHydrated });
const page = await renderTemplate('react.html', { title, preHydrated, content: '???' });
return res.send(page);
};
Object.assign(res, { render });
Expand Down

0 comments on commit 31e4057

Please sign in to comment.