Skip to content

Commit

Permalink
chore: update disable.ts
Browse files Browse the repository at this point in the history
Noticed a few bugs.
1) Changed invalid import from discord.js. Implicitly had an any type for `onFail`.
```diff
-ReplyMessageOptions
+MessageReplyOptions
```
2) Corrected the improper catching of errors by implementing try/catch blocks. Included more detailed error responses.
3) Default reply for slash with onFail inputted was invalid because it could also be a string.
  • Loading branch information
Peter-MJ-Parker authored Feb 23, 2024
1 parent f1627ac commit 31f08f2
Showing 1 changed file with 31 additions and 18 deletions.
49 changes: 31 additions & 18 deletions plugins/disable.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
// @ts-nocheck
//@ts-nocheck
/**
* @plugin
* Disables a command entirely, for whatever reasons you may need.
*
* @author @jacoobes [<@182326315813306368>]
* @author @Peter-MJ-Parker [<@371759410009341952>]
* @version 2.0.0
* @version 2.1.0
* @example
* ```ts
* import { disable } from "../plugins/disable";
Expand All @@ -20,36 +20,49 @@
* @end
*/
import { CommandType, CommandControlPlugin, controller } from "@sern/handler";
import { InteractionReplyOptions, ReplyMessageOptions } from "discord.js";
import { InteractionReplyOptions, MessageReplyOptions } from "discord.js";

export function disable(
onFail?:
| string
| Omit<InteractionReplyOptions, "fetchReply">
| ReplyMessageOptions,
| MessageReplyOptions
) {
return CommandControlPlugin<CommandType.Both>(async (ctx, [args]) => {
if (onFail !== undefined) {
switch (args) {
case "text":
//reply to text command
const msg = await ctx.reply(onFail);
setTimeout(() => {
//deletes the bots reply to the user
msg.delete();
//deletes the original authors message (text command).
ctx.message.delete();
//waits 5 seconds before deleting messages
}, 5000).catch((e) => {
//logs error to console (if any).
console.log(e);
});
try {
//reply to text command
const msg = await ctx.reply(onFail);
setTimeout(() => {
//deletes the bots reply to the user
msg.delete();
//deletes the original authors message (text command).
ctx.message.delete();
//waits 5 seconds before deleting messages
}, 5000);
} catch (error) {
console.log(
"Could not delete disabled response due to: " +
error
);
}

break;

case "slash":
//ephemeral response to say the command is disabled with users response.
await ctx.reply({ content: onFail, ephemeral: true });
//response to say the command is disabled with users response.
let reply = await ctx.reply(onFail);
try {
setTimeout(async () => {
await reply.delete();
}, 5000);
} catch (error) {
console.log(
"Could not delete disabled response due to it being ephemeral."
);
}
break;

default:
Expand Down

0 comments on commit 31f08f2

Please sign in to comment.