Skip to content

Commit

Permalink
try adding a button to checkin/out
Browse files Browse the repository at this point in the history
  • Loading branch information
loukylor committed Oct 30, 2024
1 parent 5c68fc2 commit abb9327
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 14 deletions.
39 changes: 30 additions & 9 deletions TrickfireCheckIn/Discord/Bot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using DSharpPlus.Commands;
using DSharpPlus.Commands.Processors.SlashCommands;
using DSharpPlus.Entities;
using DSharpPlus.EventArgs;
using DSharpPlus.Exceptions;
using Microsoft.Extensions.Logging;
using System.Reflection;
Expand Down Expand Up @@ -34,6 +35,10 @@ public Bot(string token)
// Add our commands from our code (anything with the command
// decorator)
extension.AddCommands(Assembly.GetExecutingAssembly());
})
.ConfigureEventHandlers(events =>
{
events.HandleComponentInteractionCreated(OnComponentInteraction);
});

Client = builder.Build();
Expand All @@ -42,6 +47,16 @@ public Bot(string token)
State.Members.CollectionChanged += (_, _) => { _needToUpdateEmbed = true; };
}

private static Task OnComponentInteraction(DiscordClient _, ComponentInteractionCreatedEventArgs e)
{
if (e.Id != "CheckInOutButton")
{
return Task.CompletedTask;
}

return Commands.CheckInOutInternal(e.Interaction);
}

/// <summary>
/// Connects the bot and starts it.
/// </summary>
Expand Down Expand Up @@ -120,7 +135,7 @@ await Client.UpdateStatusAsync(new DiscordActivity(
/// </summary>
private async Task UpdateListMessage()
{
DiscordEmbed listEmbed = CreateEmbed();
DiscordMessageBuilder builder = CreateMessage();

// Check if message exists
DiscordGuild tfGuild = await Client.GetGuildAsync(Config.Instance.TrickfireGuild);
Expand All @@ -138,7 +153,7 @@ private async Task UpdateListMessage()
{
// If it does, update it
DiscordMessage message = await channel.GetMessageAsync(Config.Instance.ListMessage);
await message.ModifyAsync(listEmbed);
await message.ModifyAsync(builder);
}
catch (DiscordException ex)
{
Expand All @@ -147,7 +162,7 @@ private async Task UpdateListMessage()
return;
}
// If not, update the config with the new message
Config.Instance.ListMessage = (await channel.SendMessageAsync(listEmbed)).Id;
Config.Instance.ListMessage = (await channel.SendMessageAsync(builder)).Id;
Config.Instance.SaveConfig();
}

Expand All @@ -157,18 +172,18 @@ private async Task UpdateListMessage()
/// Returns an embed listing the members in <see cref="Config.Members"/>.
/// </summary>
/// <returns>An embed listing the checked in members</returns>
private static DiscordEmbed CreateEmbed()
private static DiscordMessageBuilder CreateMessage()
{
// Create embed without members
DiscordEmbedBuilder builder = new DiscordEmbedBuilder()
DiscordEmbedBuilder embed = new DiscordEmbedBuilder()
.WithTitle("Members in the Shop")
.WithFooter("Made by Kyler 😎")
.WithTimestamp(DateTime.Now)
.WithColor(new DiscordColor("2ecc71"));

StringBuilder sb = new(
"A list of members currently in the shop (INV-011), kept up to date.\n" +
"Check in or out using the `/CheckInOut` command!\n\n"
"Check in or out using the button or `/checkinout` command!\n\n"
);

// Add members to description string
Expand All @@ -180,9 +195,15 @@ private static DiscordEmbed CreateEmbed()
}

// Add description
builder.WithDescription(sb.ToString());

return builder.Build();
embed.WithDescription(sb.ToString());

return new DiscordMessageBuilder()
.AddComponents(new DiscordButtonComponent(
DiscordButtonStyle.Primary,
"CheckInOutButton",
"Check In or Out"
))
.AddEmbed(embed.Build());
}
}
}
21 changes: 16 additions & 5 deletions TrickfireCheckIn/Discord/Commands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using DSharpPlus.Commands.Processors.SlashCommands.Metadata;
using DSharpPlus.Entities;
using DSharpPlus.Exceptions;
using System;
using System.ComponentModel;

namespace TrickfireCheckIn.Discord
Expand Down Expand Up @@ -69,11 +70,16 @@ public static ValueTask Ping(SlashCommandContext context)
[Command("checkinout")]
[Description("Checks you into/out of the shop and updates the member list")]
[InteractionAllowedContexts(DiscordInteractionContextType.Guild)]
public static ValueTask CheckInOut(SlashCommandContext context)
public static Task CheckInOut(SlashCommandContext context)
{
return CheckInOutInternal(context.Interaction);
}

internal static Task CheckInOutInternal(DiscordInteraction interaction)
{
// Member is not since this command cannot be called outside of
// guilds
DiscordMember member = context.Member!;
DiscordMember member = (interaction.User as DiscordMember)!;

// Find index of member in list
int memberIndex = -1;
Expand All @@ -89,22 +95,27 @@ public static ValueTask CheckInOut(SlashCommandContext context)
// Update member list
if (memberIndex == -1)
{
State.Members.Add((member, context.Interaction.CreationTimestamp));
State.Members.Add((member, interaction.CreationTimestamp));
}
else
{
State.Members.RemoveAt(memberIndex);
}

// Send confirmation response
DiscordInteractionResponseBuilder builder = new() { IsEphemeral = true };
if (memberIndex == -1)
{
return context.RespondAsync("Checked in. " + GetCheckInMessage(), true);
builder.WithContent("Checked in. " + GetCheckInMessage());
}
else
{
return context.RespondAsync($"Checked out. " + GetCheckOutMessage(), true);
builder.WithContent($"Checked out. " + GetCheckOutMessage());
}
return interaction.CreateResponseAsync(
DiscordInteractionResponseType.ChannelMessageWithSource,
builder
);
}

// Make a fake weighted random using range checking
Expand Down

0 comments on commit abb9327

Please sign in to comment.