Skip to content

Commit

Permalink
Implement /unmute as slash command
Browse files Browse the repository at this point in the history
  • Loading branch information
oliverbooth committed May 3, 2022
1 parent 7847bf5 commit 2b4f474
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 97 deletions.
70 changes: 70 additions & 0 deletions Hammer/CommandModules/UnmuteCommand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
using System;
using System.Threading.Tasks;
using BrackeysBot.API.Extensions;
using BrackeysBot.Core.API;
using BrackeysBot.Core.API.Attributes;
using DSharpPlus.CommandsNext;
using DSharpPlus.CommandsNext.Attributes;
using DSharpPlus.Entities;
using DSharpPlus.Exceptions;
using DSharpPlus.SlashCommands;
using DSharpPlus.SlashCommands.Attributes;
using Hammer.Extensions;
using Hammer.Services;
using NLog;

namespace Hammer.CommandModules;

/// <summary>
/// Represents a module which implements the <c>unmute</c> command.
/// </summary>
internal sealed class UnmuteCommand : ApplicationCommandModule
{
private static readonly ILogger Logger = LogManager.GetCurrentClassLogger();
private readonly MuteService _muteService;

/// <summary>
/// Initializes a new instance of the <see cref="UnmuteCommand" /> class.
/// </summary>
/// <param name="muteService">The mute service.</param>
public UnmuteCommand(MuteService muteService)
{
_muteService = muteService;
}

[SlashCommand("unmute", "Unmutes a user.", false)]
[SlashRequireGuild]
public async Task UnmuteSlashCommandAsync(InteractionContext context,
[Option("user", "The user to unmute.")] DiscordUser user,
[Option("reason", "The reason for the mute revocation."), RemainingText] string? reason = null)
{
await context.DeferAsync(true).ConfigureAwait(false);

var embed = new DiscordEmbedBuilder();
try
{
await _muteService.RevokeMuteAsync(user, context.Member!, reason).ConfigureAwait(false);

embed.WithAuthor(user);
embed.WithColor(DiscordColor.SpringGreen);
embed.WithTitle("Unmuted user");
embed.WithDescription(reason);

reason = reason.WithWhiteSpaceAlternative("None");
Logger.Info($"{context.Member} unmuted {user}. Reason: {reason}");
}
catch (Exception exception)
{
Logger.Error(exception, "Could not revoke mute");

embed.WithColor(DiscordColor.Red);
embed.WithTitle("⚠️ Error revoking mute");
embed.WithDescription($"{exception.GetType().Name} was thrown while revoking the mute.");
embed.WithFooter("See log for further details.");
}

var builder = new DiscordWebhookBuilder();
builder.AddEmbed(embed);
await context.EditResponseAsync(builder).ConfigureAwait(false);
}
}
96 changes: 0 additions & 96 deletions Hammer/CommandModules/UnmuteCommandModule.cs

This file was deleted.

2 changes: 1 addition & 1 deletion Hammer/HammerPlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -205,11 +205,11 @@ protected override async Task OnLoad()
commandsNext.RegisterCommands<BanCommandModule>();
commandsNext.RegisterCommands<KickCommandModule>();
commandsNext.RegisterCommands<UnbanCommandModule>();
commandsNext.RegisterCommands<UnmuteCommandModule>();

Logger.Info("Registering slash commands");
SlashCommandsExtension slashCommands = DiscordClient.GetSlashCommands();
slashCommands.RegisterCommands<MuteCommand>();
slashCommands.RegisterCommands<UnmuteCommand>();
slashCommands.RegisterCommands<WarnCommand>();

Logger.Info("Registering InteractivityExtension");
Expand Down

0 comments on commit 2b4f474

Please sign in to comment.