Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings
This repository was archived by the owner on Apr 30, 2024. It is now read-only.

Commit 44983d6

Browse files
2 parents 2f076de + 7941666 commit 44983d6

File tree

4 files changed

+13
-7
lines changed

4 files changed

+13
-7
lines changed

‎Attributes/OptionAttribute.cs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,11 @@ public sealed class OptionAttribute : Attribute
2626
public OptionAttribute(string name, string description)
2727
{
2828
if(name.Length > 32)
29-
throw new ArgumentException("Slash command option names cannot go over 100 characters.");
29+
throw new ArgumentException("Slash command option names cannot go over 32 characters.");
3030
if (description.Length > 100)
3131
throw new ArgumentException("Slash command option descriptions cannot go over 100 characters.");
3232
Name = name.ToLower();
3333
Description = description;
3434
}
3535
}
36-
}
36+
}

‎README.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,8 @@ public async Task TestCommand(InteractionContext ctx)
105105
}
106106
```
107107
You can also override `BeforeExecutionAsync` and `AfterExecutionAsync` to run code before and after all the commands in a module. This does not apply to groups, you have the override them individually for the group's class.
108+
`BeforeExecutionAsync` can also be used to prevent the command from running.
109+
108110
### Arguments
109111
If you want the user to be able to give more data to the command, you can add some arguments.
110112

‎SlashCommandModule.cs‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,9 @@ public abstract class SlashCommandModule
1111
/// Called before the execution of command in the module.
1212
/// </summary>
1313
/// <param name="ctx">The context.</param>
14-
public virtual Task BeforeExecutionAsync(InteractionContext ctx)
15-
=> Task.CompletedTask;
14+
/// <returns> Whether or not to execute the slash command. </returns>
15+
public virtual Task<bool> BeforeExecutionAsync(InteractionContext ctx)
16+
=> Task.FromResult(true);
1617

1718
/// <summary>
1819
/// Called after the execution of a command in the module.

‎SlashCommandsExtension.cs‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -376,11 +376,14 @@ internal async Task RunCommand(InteractionContext context, MethodInfo method, IE
376376

377377
await RunPreexecutionChecksAsync(method, context);
378378

379-
await (module?.BeforeExecutionAsync(context) ?? Task.CompletedTask);
379+
varshouldExecute=await (module?.BeforeExecutionAsync(context) ?? Task.FromResult(true));
380380

381-
await (Task)method.Invoke(classinstance, args.ToArray());
381+
if (shouldExecute)
382+
{
383+
await (Task)method.Invoke(classinstance, args.ToArray());
382384

383-
await (module?.AfterExecutionAsync(context) ?? Task.CompletedTask);
385+
await (module?.AfterExecutionAsync(context) ?? Task.CompletedTask);
386+
}
384387
}
385388

386389
internal object CreateInstance(Type t, IServiceProvider services)

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /