-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #68 from flyingfisch/49-add-slash-commands
Switched commands from text commands to slash commands
- Loading branch information
Showing
20 changed files
with
377 additions
and
227 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
using System; | ||
using System.Reflection; | ||
using System.Threading.Tasks; | ||
using Discord; | ||
using Discord.Interactions; | ||
using Discord.WebSocket; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.Logging; | ||
|
||
namespace FischBot.Handlers | ||
{ | ||
public class InteractionHandler | ||
{ | ||
private readonly DiscordSocketClient _discordClient; | ||
private readonly InteractionService _commands; | ||
private readonly IServiceProvider _services; | ||
private readonly ILogger _logger; | ||
|
||
public InteractionHandler(DiscordSocketClient discordClient, InteractionService commands, IServiceProvider services) | ||
{ | ||
_discordClient = discordClient; | ||
_commands = commands; | ||
_services = services; | ||
_logger = services.GetRequiredService<ILogger<InteractionHandler>>(); | ||
} | ||
|
||
public async Task InitializeAsync() | ||
{ | ||
await _commands.AddModulesAsync(Assembly.GetEntryAssembly(), _services); | ||
|
||
_discordClient.InteractionCreated += HandleInteraction; | ||
|
||
_commands.SlashCommandExecuted += SlashCommandExecuted; | ||
_commands.ContextCommandExecuted += ContextCommandExecuted; | ||
_commands.ComponentCommandExecuted += ComponentCommandExecuted; | ||
} | ||
|
||
private async Task HandleInteraction(SocketInteraction interaction) | ||
{ | ||
var context = new SocketInteractionContext(_discordClient, interaction); | ||
|
||
try | ||
{ | ||
await _commands.ExecuteCommandAsync(context, _services); | ||
} | ||
catch (Exception) | ||
{ | ||
if (interaction.Type == InteractionType.ApplicationCommand) | ||
{ | ||
await interaction.GetOriginalResponseAsync().ContinueWith(async (msg) => await msg.Result.DeleteAsync()); | ||
} | ||
} | ||
} | ||
|
||
private async Task ComponentCommandExecuted(ComponentCommandInfo command, Discord.IInteractionContext context, IResult result) | ||
{ | ||
if (result.IsSuccess) | ||
{ | ||
_logger.LogInformation($"Component command [{command.Name}] executed for [{context.User.Username}] on [{context.Guild.Name}]"); | ||
} | ||
else | ||
{ | ||
_logger.LogError($"Component command failed to execute for [{context.User.Username}] <-> [{result}]!"); | ||
await context.Interaction.RespondAsync($"Sorry, {context.User.Username}... something went wrong -> [{result}]!", ephemeral: true); | ||
} | ||
} | ||
|
||
private async Task ContextCommandExecuted(ContextCommandInfo command, Discord.IInteractionContext context, IResult result) | ||
{ | ||
if (result.IsSuccess) | ||
{ | ||
_logger.LogInformation($"Context command [{command.Name}] executed for [{context.User.Username}] on [{context.Guild.Name}]"); | ||
} | ||
else | ||
{ | ||
_logger.LogError($"Context command failed to execute for [{context.User.Username}] <-> [{result}]!"); | ||
await context.Interaction.RespondAsync($"Sorry, {context.User.Username}... something went wrong -> [{result}]!", ephemeral: true); | ||
} | ||
} | ||
|
||
private async Task SlashCommandExecuted(SlashCommandInfo command, Discord.IInteractionContext context, IResult result) | ||
{ | ||
if (result.IsSuccess) | ||
{ | ||
_logger.LogInformation($"Slash command [{command.Name}] executed for [{context.User.Username}] on [{context.Guild.Name}]"); | ||
} | ||
else | ||
{ | ||
_logger.LogError($"Slash command failed to execute for [{context.User.Username}] <-> [{result}]!"); | ||
await context.Interaction.RespondAsync($"Sorry, {context.User.Username}... something went wrong -> [{result}]!", ephemeral: true); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
using System.Threading.Tasks; | ||
using Discord; | ||
using Discord.Interactions; | ||
using FischBot.Services.DiscordModuleService; | ||
|
||
namespace FischBot.Modules | ||
{ | ||
public class FischBotInteractionModuleBase<T> : InteractionModuleBase where T : class, IInteractionContext | ||
{ | ||
private readonly IDiscordModuleService _moduleService; | ||
|
||
public FischBotInteractionModuleBase(IDiscordModuleService moduleService) : base() | ||
{ | ||
_moduleService = moduleService; | ||
} | ||
|
||
protected override async Task RespondAsync( | ||
string text = null, | ||
Embed[] embeds = null, | ||
bool isTTS = false, | ||
bool ephemeral = false, | ||
AllowedMentions allowedMentions = null, | ||
RequestOptions options = null, | ||
MessageComponent components = null, | ||
Embed embed = null) | ||
{ | ||
await _moduleService.RespondAsync(Context, text, embeds, isTTS, ephemeral, allowedMentions, options, components, embed); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.