Skip to content

Commit 71eca99

Browse files
committed
refactor: Catch errors for all PS events
1 parent ed5f092 commit 71eca99

7 files changed

Lines changed: 55 additions & 31 deletions

File tree

src/ps/commands/help.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { prefix } from '@/config/ps';
2-
import { LivePS } from '@/sentinel/live';
2+
import { LivePSStuff } from '@/sentinel/live';
33
import { Perms } from '@/types/perms';
44
import { ChatError } from '@/utils/chatError';
55
import { Button, Username } from '@/utils/components/ps';
@@ -167,7 +167,7 @@ export const command: PSCommand = {
167167
// Parse the command to try and find what we need
168168
let parsed: ReturnType<typeof parse>;
169169
try {
170-
parsed = LivePS.commands.parse(args, [], $T);
170+
parsed = LivePSStuff.commands.parse(args, [], $T);
171171
if (parsed.command.flags?.noDisplay) throw new Error(); // Don't show this here!
172172
} catch (e) {
173173
throw new ChatError('COULD_NOT_FIND_COMMAND' as ToTranslate); // TODO

src/ps/handlers/commands/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { prefix } from '@/config/ps';
22
import { i18n } from '@/i18n';
3-
import { LivePS } from '@/sentinel/live';
3+
import { LivePSStuff } from '@/sentinel/live';
44
import { ChatError } from '@/utils/chatError';
55
import { log } from '@/utils/logger';
66

@@ -31,7 +31,7 @@ export async function commandHandler(message: PSMessage, indirect: IndirectCtx |
3131

3232
if (!messageContent.startsWith(prefix)) return;
3333

34-
const { parse, permissions, spoof } = LivePS.commands;
34+
const { parse, permissions, spoof } = LivePSStuff.commands;
3535

3636
try {
3737
const usePermissions: typeof permissions = (...args) => {

src/ps/handlers/commands/permissions.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { PSRoomConfigs } from '@/cache';
22
import { admins } from '@/config/ps';
33
import { KNOWN_RANK_MAPPINGS, RANK_ORDER } from '@/ps/constants';
4-
import { LivePS } from '@/sentinel/live';
4+
import { LivePSStuff } from '@/sentinel/live';
55

66
import type { Perms } from '@/types/perms';
77
import type { AuthKey, PSMessage } from '@/types/ps';
@@ -66,7 +66,7 @@ export function permissions(perm: Perms, command: string[] | null, message: PSMe
6666
if (roomConfig?.permissions?.[lookup]) return permissions(roomConfig.permissions[lookup], null, message);
6767
}
6868
if (typeof perm === 'symbol') {
69-
const groupedPerms = LivePS.commands.GROUPED_PERMS;
69+
const groupedPerms = LivePSStuff.commands.GROUPED_PERMS;
7070
if (perm in groupedPerms) {
7171
const symbolName = Symbol.keyFor(perm)!;
7272
if (roomConfig?.permissions?.[symbolName]) return permissions(roomConfig.permissions[symbolName], null, message);

src/ps/handlers/index.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { LivePSHandlers } from '@/sentinel/live';
2+
import { errorLog } from '@/utils/logger';
3+
4+
import type { Client } from 'ps-client';
5+
6+
export function registerEvent<Event extends keyof LivePSHandlers>(client: Client, event: Event): (typeof LivePSHandlers)[Event] {
7+
// @ts-expect-error -- TS doesn't like this, but it will end up being checked when calling
8+
return (...args: Parameters<(typeof LivePSHandlers)[Event]>) => {
9+
try {
10+
// @ts-expect-error -- See above
11+
return LivePSHandlers[event].apply(client, args);
12+
} catch (err) {
13+
if (err instanceof Error) errorLog(err);
14+
}
15+
};
16+
}

src/ps/index.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,28 @@ import { Client } from 'ps-client';
22

33
import { password, rooms, username } from '@/config/ps';
44
import { IS_ENABLED } from '@/enabled';
5+
import { registerEvent } from '@/ps/handlers';
56
import { startPSCron } from '@/ps/handlers/cron';
67
import { transformHTML } from '@/ps/handlers/html';
78
import loadPS from '@/ps/loaders';
8-
import { LivePS } from '@/sentinel/live';
99
import { log } from '@/utils/logger';
1010

1111
const PS = new Client({ username, password, rooms, transformHTML });
1212
PS.on('login', () => log(`Connected to PS! [${username}]`));
1313

1414
if (IS_ENABLED.PS) loadPS().then(() => PS.connect());
1515

16-
PS.on('message', (...args) => LivePS.commands.commandHandler.call(PS, ...args));
17-
PS.on('message', (...args) => LivePS.interfaceHandler.call(PS, ...args));
18-
PS.on('message', (...args) => LivePS.autoResHandler.call(PS, ...args));
19-
PS.on('message', (...args) => LivePS.pageHandler.call(PS, ...args));
16+
PS.on('message', registerEvent(PS, 'commandHandler'));
17+
PS.on('message', registerEvent(PS, 'interfaceHandler'));
18+
PS.on('message', registerEvent(PS, 'autoResHandler'));
19+
PS.on('message', registerEvent(PS, 'pageHandler'));
2020

21-
PS.on('join', (...args) => LivePS.joinHandler.call(PS, ...args));
22-
PS.on('name', (...args) => LivePS.nickHandler.call(PS, ...args));
23-
PS.on('leave', (...args) => LivePS.leaveHandler.call(PS, ...args));
24-
PS.on('notify', (...args) => LivePS.notifyHandler.call(PS, ...args));
25-
PS.on('raw', (...args) => LivePS.rawHandler.call(PS, ...args));
26-
PS.on('tournament', (...args) => LivePS.tourHandler.call(PS, ...args));
21+
PS.on('join', registerEvent(PS, 'joinHandler'));
22+
PS.on('name', registerEvent(PS, 'nickHandler'));
23+
PS.on('leave', registerEvent(PS, 'leaveHandler'));
24+
PS.on('notify', registerEvent(PS, 'notifyHandler'));
25+
PS.on('raw', registerEvent(PS, 'rawHandler'));
26+
PS.on('tournament', registerEvent(PS, 'tourHandler'));
2727

2828
if (IS_ENABLED.PS) startPSCron.bind(PS)();
2929

src/sentinel/live.ts

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,9 @@ import { tourHandler } from '@/ps/handlers/tours';
2626
export const LiveData = {};
2727

2828
/** @see {@link LiveData} */
29-
export const LivePS = {
29+
export const LivePSHandlers = {
3030
autoResHandler,
31-
commands: {
32-
commandHandler,
33-
parse,
34-
permissions,
35-
spoof,
36-
GROUPED_PERMS,
37-
},
31+
commandHandler,
3832
interfaceHandler,
3933
joinHandler,
4034
leaveHandler,
@@ -44,3 +38,14 @@ export const LivePS = {
4438
rawHandler,
4539
tourHandler,
4640
};
41+
export type LivePSHandlers = typeof LivePSHandlers;
42+
43+
/** @see {@link LiveData} */
44+
export const LivePSStuff = {
45+
commands: {
46+
parse,
47+
permissions,
48+
spoof,
49+
GROUPED_PERMS,
50+
},
51+
};

src/sentinel/registers/ps.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { promises as fs } from 'fs';
22

33
import { Games } from '@/ps/games';
44
import { reloadCommands } from '@/ps/loaders/commands';
5-
import { LivePS } from '@/sentinel/live';
5+
import { LivePSHandlers, LivePSStuff } from '@/sentinel/live';
66
import { cachebust } from '@/utils/cachebust';
77
import { fsPath } from '@/utils/fsPath';
88

@@ -17,7 +17,10 @@ const PS_EVENT_HANDLERS = {
1717
'raw-handler': { imports: ['rawHandler'], importPath: '@/ps/handlers/raw', fileName: 'raw' },
1818
'notify-handler': { imports: ['notifyHandler'], importPath: '@/ps/handlers/notifications', fileName: 'notifications' },
1919
'tour-handler': { imports: ['tourHandler'], importPath: '@/ps/handlers/tours', fileName: 'tours' },
20-
} satisfies Record<string, { imports: (keyof typeof LivePS)[]; importPath: string; fileName: string /* TODO: remove fileName */ }>;
20+
} satisfies Record<
21+
string,
22+
{ imports: (keyof typeof LivePSHandlers)[]; importPath: string; fileName: string /* TODO: remove fileName */ }
23+
>;
2124

2225
export const PS_REGISTERS: Register[] = [
2326
{
@@ -66,17 +69,17 @@ export const PS_REGISTERS: Register[] = [
6669
const importPath = `@/ps/handlers/commands/${file}`;
6770
cachebust(importPath);
6871
const hotHandler = await import(importPath);
69-
LivePS.commands[file] = hotHandler[file];
72+
LivePSStuff.commands[file] = hotHandler[file];
7073
})
7174
);
7275

7376
cachebust('@/ps/handlers/commands/customPerms');
7477
const { GROUPED_PERMS: newGroupedPerms } = await import('@/ps/handlers/commands/customPerms');
75-
LivePS.commands.GROUPED_PERMS = newGroupedPerms;
78+
LivePSStuff.commands.GROUPED_PERMS = newGroupedPerms;
7679

7780
cachebust('@/ps/handlers/commands');
7881
const { commandHandler } = await import('@/ps/handlers/commands');
79-
LivePS.commands.commandHandler = commandHandler;
82+
LivePSHandlers.commandHandler = commandHandler;
8083
},
8184
},
8285

@@ -87,7 +90,7 @@ export const PS_REGISTERS: Register[] = [
8790
reload: async () => {
8891
cachebust(handlerData.importPath);
8992
const hotHandler = await import(handlerData.importPath);
90-
handlerData.imports.forEach(namedImport => (LivePS[namedImport] = hotHandler[namedImport]));
93+
handlerData.imports.forEach(namedImport => (LivePSHandlers[namedImport] = hotHandler[namedImport]));
9194
},
9295
})),
9396
];

0 commit comments

Comments
 (0)