Skip to content

Commit 41e691e

Browse files
committed
feat(guards): format code
1 parent d001c18 commit 41e691e

File tree

3 files changed

+306
-82
lines changed

3 files changed

+306
-82
lines changed

packages/guards/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
"author": "Dylan Neiger",
2020
"license": "ISC",
2121
"devDependencies": {
22-
"@spark.ts/handler": "^1.5.0",
22+
"@spark.ts/handler": "workspace:^",
2323
"discord.js": "^14.7.1",
2424
"eslint": "^8.32.0",
2525
"eslint-config-airbnb-base": "^15.0.0",

packages/guards/src/index.ts

Lines changed: 38 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,54 @@
1-
// IMPORTS \\
2-
import {CommandPlugin,CommandType,Command} from "@spark.ts/handler";
3-
import * as Discord from "discord.js";
1+
import { CommandPlugin, CommandType, Command } from '@spark.ts/handler';
2+
import { ChatInputCommandInteraction, Message } from 'discord.js';
43

5-
// TYPES \\
64
type RemoveMethods<T> = T extends {} ? {
75
[P in keyof T as T[P] extends (...args: any[]) => unknown ? never : P]: RemoveMethods<T[P]>;
86
} : T;
97

108
type ConditionalMethod = (args: {
119
command: Command,
12-
interaction: RemoveMethods<Discord.ChatInputCommandInteraction>,
13-
message: RemoveMethods<Discord.Message>
14-
}) => unknown
10+
interaction: RemoveMethods<ChatInputCommandInteraction>,
11+
message: RemoveMethods<Message>;
12+
}) => unknown;
1513

16-
interface Conditional{
14+
interface Conditional {
1715
condition: ConditionalMethod | unknown;
1816
onFalse?: string;
1917
}
2018

21-
// EXPORTS \\
22-
export function Guard(...conditionals: Conditional[]): CommandPlugin<CommandType.Both>{
23-
return {
24-
name: "guard",
25-
description: "Checks through a list of defined conditions before running a command.",
26-
run({command,controller,message,interaction}){
27-
for(const conditional of conditionals){
28-
let condition = conditional.condition;
29-
if(typeof conditional.condition === "function"){
30-
condition = conditional.condition({command,message,interaction});
31-
}
32-
33-
if(!condition){
34-
if("onFalse" in conditional){
35-
if(command.type === CommandType.Slash) interaction?.reply(conditional.onFalse!);
36-
else if(command.type === CommandType.Text) message?.reply(conditional.onFalse!);
37-
}
38-
return controller.stop();
39-
}
40-
}
41-
42-
return controller.next();
19+
export function Guard(...conditionals: Conditional[]): CommandPlugin<CommandType.Both> {
20+
return {
21+
name: 'guard',
22+
description: 'Checks through a list of defined conditions before running a command.',
23+
run({
24+
command, controller, message, interaction,
25+
}) {
26+
for (const conditional of conditionals) {
27+
let { condition } = conditional;
28+
if (typeof conditional.condition === 'function') {
29+
condition = conditional.condition({ command, message, interaction });
4330
}
44-
}
31+
32+
if (!condition) {
33+
if ('onFalse' in conditional) {
34+
if (command.type === CommandType.Slash) interaction?.reply(conditional.onFalse!);
35+
else if (command.type === CommandType.Text) message?.reply(conditional.onFalse!);
36+
}
37+
return controller.stop();
38+
}
39+
}
40+
41+
return controller.next();
42+
},
43+
};
4544
}
4645

47-
// HELPERS \\
48-
export abstract class Helpers{
49-
static InGuild(guilds: string[]): ConditionalMethod{
50-
return ({command,message,interaction}) => guilds.includes((command.type === "slash" ? interaction : message).guildId!);
51-
}
46+
export abstract class Helpers {
47+
static InGuild(guilds: string[]): ConditionalMethod {
48+
return ({ command, message, interaction }) => guilds.includes((command.type === 'slash' ? interaction : message).guildId!);
49+
}
5250

53-
static InChannel(channels: string[]): ConditionalMethod{
54-
return ({command,message,interaction}) => channels.includes((command.type === "slash" ? interaction : message).channelId!);
55-
}
56-
}
51+
static InChannel(channels: string[]): ConditionalMethod {
52+
return ({ command, message, interaction }) => channels.includes((command.type === 'slash' ? interaction : message).channelId!);
53+
}
54+
}

0 commit comments

Comments
 (0)