Skip to content

Commit

Permalink
feat: interactivity commands
Browse files Browse the repository at this point in the history
  • Loading branch information
thutasann committed Jun 1, 2024
1 parent 8c7cdb6 commit 3e588ce
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 38 deletions.
1 change: 1 addition & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
56 changes: 56 additions & 0 deletions Commands/CardGame.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
using csharp_discord_bot.Helpers;
using DSharpPlus.CommandsNext;
using DSharpPlus.CommandsNext.Attributes;
using DSharpPlus.Entities;

namespace csharp_discord_bot.Commands
{
public class CardGameEmbed : BaseCommandModule
{
[Command("cardgame")]
public async Task CardGame(CommandContext ctx)
{
var userCard = new CardSystem();
var botCard = new CardSystem();

var userCardEmbed = new DiscordEmbedBuilder
{
Title = $"Your card is {userCard.SelectedCard}",
Color = DiscordColor.Lilac,
};

await ctx.Channel.SendMessageAsync(embed: userCardEmbed);

var botCardEmbed = new DiscordEmbedBuilder
{
Title = $"The bot Drew a {botCard.SelectedCard}",
Color = DiscordColor.Orange
};

await ctx.Channel.SendMessageAsync(embed: botCardEmbed);

if (userCard.SelectedNumber > botCard.SelectedNumber)
{
// User wins
var winMessage = new DiscordEmbedBuilder
{
Title = "Congrat!, You Win the game!!",
Color = DiscordColor.Green
};

await ctx.Channel.SendMessageAsync(embed: winMessage);
}
else
{
// Bot wins
var loseMessage = new DiscordEmbedBuilder
{
Title = "You Lose the game!!",
Color = DiscordColor.Red
};

await ctx.Channel.SendMessageAsync(embed: loseMessage);
}
}
}
}
61 changes: 24 additions & 37 deletions Commands/TestCommands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using DSharpPlus.CommandsNext;
using DSharpPlus.CommandsNext.Attributes;
using DSharpPlus.Entities;
using DSharpPlus.Interactivity.Extensions;

namespace csharp_discord_bot.Commands
{
Expand Down Expand Up @@ -45,49 +46,35 @@ public async Task EmbedMessage(CommandContext ctx)
await ctx.Channel.SendMessageAsync(message);
}

[Command("cardgame")]
public async Task CardGame(CommandContext ctx)
[Command("interactivity-test")]
public async Task InterActivityTest(CommandContext ctx)
{
var userCard = new CardSystem();
var botCard = new CardSystem();

var userCardEmbed = new DiscordEmbedBuilder
{
Title = $"Your card is {userCard.SelectedCard}",
Color = DiscordColor.Lilac,
};

await ctx.Channel.SendMessageAsync(embed: userCardEmbed);

var botCardEmbed = new DiscordEmbedBuilder
Console.WriteLine("Interactivity Test...");
var interactivity = DiscordSetup.Client?.GetInteractivity();
if (interactivity is not null)
{
Title = $"The bot Drew a {botCard.SelectedCard}",
Color = DiscordColor.Orange
};

await ctx.Channel.SendMessageAsync(embed: botCardEmbed);

if (userCard.SelectedNumber > botCard.SelectedNumber)
{
// User wins
var winMessage = new DiscordEmbedBuilder
var messageToRetriever = await interactivity.WaitForMessageAsync(message => message.Content == "Hello");
if (messageToRetriever.Result.Content == "Hello")
{
Title = "Congrat!, You Win the game!!",
Color = DiscordColor.Green
};

await ctx.Channel.SendMessageAsync(embed: winMessage);
await ctx.Channel.SendMessageAsync($"{ctx.User.Username} said Hello.");
}
}
else
}

[Command("reaction")]
public async Task ReactionTest(CommandContext ctx)
{
Console.WriteLine("Reaction Testing...");
string jumpUrl = "https://discord.com/channels/1220625821643837541/1220625821643837544/1246442292152369182";

var interactivity = DiscordSetup.Client?.GetInteractivity();
if (interactivity is not null)
{
// Bot wins
var loseMessage = new DiscordEmbedBuilder
var messageToReact = await interactivity.WaitForReactionAsync(message => message.Message.JumpLink == new Uri(jumpUrl));
if (messageToReact.Result.Message.JumpLink == new Uri(jumpUrl))
{
Title = "You Lose the game!!",
Color = DiscordColor.Red
};

await ctx.Channel.SendMessageAsync(embed: loseMessage);
await ctx.Channel.SendMessageAsync($"{ctx.User.Username} used the emoji with the name {messageToReact.Result.Emoji.Name}");
}
}
}
}
Expand Down
12 changes: 11 additions & 1 deletion Helpers/DiscordSetup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
using csharp_discord_bot.Config;
using DSharpPlus;
using DSharpPlus.CommandsNext;
using DSharpPlus.Interactivity;
using DSharpPlus.Interactivity.Extensions;

namespace csharp_discord_bot.Helpers
{
Expand All @@ -10,7 +12,7 @@ namespace csharp_discord_bot.Helpers
/// </summary>
public static class DiscordSetup
{
private static DiscordClient? Client { get; set; }
public static DiscordClient? Client { get; set; }
private static CommandsNextExtension? Commands { get; set; }

/// <summary>
Expand All @@ -33,6 +35,12 @@ public static async Task MainAsync()

Client = new DiscordClient(discordConfig);

// Set the default timeout for Commands that ues interactivity
Client.UseInteractivity(new InteractivityConfiguration()
{
Timeout = TimeSpan.FromMinutes(1)
});

var commandsConfig = new CommandsNextConfiguration()
{
StringPrefixes = [jsonReader.Prefix],
Expand All @@ -43,7 +51,9 @@ public static async Task MainAsync()

Commands = Client.UseCommandsNext(commandsConfig);

// register commands
Commands.RegisterCommands<TestCommands>();
Commands.RegisterCommands<CardGameEmbed>();

await Client.ConnectAsync();
await Task.Delay(-1); // to keep bot running forever, as long as the program is running
Expand Down
1 change: 1 addition & 0 deletions csharp-discord-bot.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
<ItemGroup>
<PackageReference Include="DSharpPlus" Version="5.0.0-nightly-02267" />
<PackageReference Include="DSharpPlus.CommandsNext" Version="5.0.0-nightly-02267" />
<PackageReference Include="DSharpPlus.Interactivity" Version="5.0.0-nightly-02267" />
</ItemGroup>

</Project>

0 comments on commit 3e588ce

Please sign in to comment.