Skip to content

Commit

Permalink
Add /clap
Browse files Browse the repository at this point in the history
  • Loading branch information
selfdocumentingcode committed Aug 24, 2024
1 parent 50e81b1 commit 04c1efc
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 12 deletions.
57 changes: 57 additions & 0 deletions Kattbot/CommandHandlers/Random/ClapText.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using DSharpPlus.CommandsNext;
using DSharpPlus.Entities;
using Kattbot.Helpers;
using MediatR;

namespace Kattbot.CommandHandlers.Random;

public class ClapTextRequest : CommandRequest
{
public ClapTextRequest(CommandContext ctx, string text)
: base(ctx)
{
Text = text;
}

public string Text { get; }
}

public class ClapTextRequestHandler : IRequestHandler<ClapTextRequest>
{
public async Task Handle(ClapTextRequest request, CancellationToken cancellationToken)
{
CommandContext ctx = request.Ctx;
string text = request.Text;
DiscordMessage? reply = ctx.Message.ReferencedMessage;

if (reply is not null)
{
text = reply.Content;
}

if (string.IsNullOrEmpty(text))
{
await ctx.RespondAsync("What am I supposed to clap?");
return;
}

DiscordEmoji clapEmoji = DiscordEmoji.FromUnicode(EmojiMap.Clap);

string[] textParts = text.Split(' ');

string innerClappedMessage = string.Join($" {clapEmoji} ", textParts.Select(x => x.ToUpper()));

var clappedMessage = $"{clapEmoji} {innerClappedMessage} {clapEmoji}";

if (clappedMessage.Length > DiscordConstants.MaxMessageLength)
{
await ctx.RespondAsync("I can't clap that much!");
return;
}

await ctx.RespondAsync(clappedMessage);
}
}
4 changes: 2 additions & 2 deletions Kattbot/CommandHandlers/Speech/SpeechCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public SpeakTextRequest(CommandContext ctx, string text)
Text = text;
}

public string Text { get; set; }
public string Text { get; }
}

public class SpeakRequestHandler : IRequestHandler<SpeakTextRequest>
Expand Down Expand Up @@ -79,7 +79,7 @@ public async Task Handle(SpeakTextRequest request, CancellationToken cancellatio

private static string GetRandomVoice()
{
int random = new Random().Next(AudioVoices.Length);
int random = new System.Random().Next(AudioVoices.Length);

return AudioVoices[random];
}
Expand Down
14 changes: 11 additions & 3 deletions Kattbot/CommandModules/RandomModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using DSharpPlus.CommandsNext;
using DSharpPlus.CommandsNext.Attributes;
using Kattbot.Attributes;
using Kattbot.CommandHandlers.Random;
using Kattbot.CommandHandlers.Speech;
using Kattbot.Workers;

Expand All @@ -21,7 +22,7 @@ public RandomModule(CommandQueueChannel commandParallelQueue)
[Command("meow")]
public Task Meow(CommandContext ctx)
{
int result = new Random().Next(0, 10);
int result = new Random().Next(minValue: 0, maxValue: 10);

if (result == 0)
{
Expand All @@ -34,7 +35,7 @@ public Task Meow(CommandContext ctx)
[Command("mjau")]
public Task Mjau(CommandContext ctx)
{
int result = new Random().Next(0, 10);
int result = new Random().Next(minValue: 0, maxValue: 10);

if (result == 0)
{
Expand All @@ -51,10 +52,17 @@ public Task GetRandomPrep(CommandContext ctx, string placeholder)
}

[Command("speak")]
[Cooldown(1, 60, CooldownBucketType.User)]
[Cooldown(maxUses: 1, resetAfter: 10, CooldownBucketType.User)]
public async Task Speak(CommandContext ctx, [RemainingText] string text)
{
var request = new SpeakTextRequest(ctx, text);
await _commandParallelQueue.Writer.WriteAsync(request);
}

[Command("clap")]
public async Task Clap(CommandContext ctx, [RemainingText] string message)
{
var request = new ClapTextRequest(ctx, message);
await _commandParallelQueue.Writer.WriteAsync(request);
}
}
6 changes: 1 addition & 5 deletions Kattbot/Helpers/EmojiMap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,6 @@

public static class EmojiMap
{
public static readonly string NumberOne = "\u0031\u20E3";
public static readonly string NumberTwo = "\u0032\u20E3";
public static readonly string PointLeft = "\uD83D\uDC48";
public static readonly string PointRight = "\uD83D\uDC49";
public static readonly string WhiteCheckmark = "\u2705";
public static readonly string RedX = "\u274C";
public static readonly string Clap = "\uD83D\uDC4F";
}
5 changes: 3 additions & 2 deletions Kattbot/Infrastructure/ServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@ public static void AddDiscordClient(this IServiceCollection services, IConfigura

clientBuilder.SetLogLevel(logLevel);

clientBuilder.RegisterEventHandlers();

clientBuilder.RegisterCommands(configuration);

clientBuilder.RegisterEventHandlers();

// This replacement has to happen after the DiscordClientBuilder.CreateDefault call
// and before the DiscordClient is built.
services.Replace<IGatewayController, NoWayGateway>();
Expand Down Expand Up @@ -80,6 +80,7 @@ private static void RegisterCommands(this DiscordClientBuilder builder, IConfigu
EnableDefaultHelp = false,
EnableMentionPrefix = false,
};

builder.UseCommandsNext(
commands =>
{
Expand Down

0 comments on commit 04c1efc

Please sign in to comment.