Skip to content

Commit

Permalink
[Bot] Switched to Interaction framework
Browse files Browse the repository at this point in the history
  • Loading branch information
Pythonic-Rainbow committed Jan 19, 2024
1 parent 1969477 commit f2ac3ff
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 32 deletions.
1 change: 1 addition & 0 deletions Bot/Bot.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

<ItemGroup>
<PackageReference Include="ClashOfClans" Version="9.0.0" />
<PackageReference Include="Discord.Net.Interactions" Version="3.13.0" />
<PackageReference Include="Discord.Net.WebSocket" Version="3.13.0" />
<PackageReference Include="sqlite-net-pcl" Version="1.8.116" />
</ItemGroup>
Expand Down
22 changes: 12 additions & 10 deletions Bot/Dc/CmdHandlers.cs
Original file line number Diff line number Diff line change
@@ -1,34 +1,36 @@
using Discord.WebSocket;
using Discord.Interactions;
using Discord.WebSocket;

Check warning on line 2 in Bot/Dc/CmdHandlers.cs

View workflow job for this annotation

GitHub Actions / inspect

"[RedundantUsingDirective] Using directive is not required by the code and can be safely removed" on /home/runner/work/Hyperstellar/Hyperstellar/Bot/Dc/CmdHandlers.cs(2,28)

Check warning on line 2 in Bot/Dc/CmdHandlers.cs

View workflow job for this annotation

GitHub Actions / inspect

"[IDE0005] Using directive is unnecessary." on /home/runner/work/Hyperstellar/Hyperstellar/Bot/Dc/CmdHandlers.cs(2,28)
using Hyperstellar.Sql;

namespace Hyperstellar.Dc;

internal class CmdHandlers
public class CmdHandlers : InteractionModuleBase
{
internal static async Task ShutdownAsync(SocketSlashCommand cmd)

[SlashCommand("shutdown", "[Admin] Shuts down the bot")]
public async Task ShutdownAsync(bool commit = true)
{
if (cmd.User.Id != 264756129916125184)
if (Context.User.Id != 264756129916125184)
{
return;
}

bool commit = cmd.Data.Options.Count == 0 || (bool)cmd.Data.Options.First().Value;
await cmd.RespondAsync("Ok", ephemeral: true);
await RespondAsync("Ok", ephemeral: true);
if (commit)
{
Db.Commit();
}
Environment.Exit(0);
}

internal static async Task CommitAsync(SocketSlashCommand cmd)
[SlashCommand("commit", "[Admin] Commits db")]
public async Task CommitAsync()
{
if (cmd.User.Id != 264756129916125184)
if (Context.User.Id != 264756129916125184)
{
return;
}

Db.Commit();
await cmd.RespondAsync("Committed", ephemeral: true);
await RespondAsync("Committed", ephemeral: true);
}
}
28 changes: 7 additions & 21 deletions Bot/Dc/Discord.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using Discord;
using System.Reflection;
using Discord;
using Discord.Interactions;
using Discord.WebSocket;
using static Hyperstellar.Coc;

Expand All @@ -11,6 +13,7 @@ internal sealed class Discord
private static SocketTextChannel s_botLog;
#pragma warning restore CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider declaring as nullable.

private static readonly InteractionService s_interactionSvc = new(s_bot);

Check warning on line 16 in Bot/Dc/Discord.cs

View workflow job for this annotation

GitHub Actions / inspect

"[StaticMemberInitializerReferesToMemberBelow] Static member initializer refers to static member below or in other type part" on /home/runner/work/Hyperstellar/Hyperstellar/Bot/Dc/Discord.cs(16,586)
internal static readonly DiscordSocketClient s_bot = new();

private static Task Log(LogMessage msg)
Expand All @@ -23,37 +26,20 @@ private static async Task Ready()
{
s_botLog = (SocketTextChannel)s_bot.GetChannel(Secrets.s_botLogId);
_ = Task.Run(BotReadyAsync);

SlashCommandBuilder guildCmd = new SlashCommandBuilder()
.WithName("shutdown")
.WithDescription("[Admin] Shuts down the bot")
.AddOption("commit", ApplicationCommandOptionType.Boolean, "Commit db?");
await s_bot.CreateGlobalApplicationCommandAsync(guildCmd.Build());

guildCmd = new SlashCommandBuilder()
.WithName("commit")
.WithDescription("[Admin] Commits db");
await s_bot.CreateGlobalApplicationCommandAsync(guildCmd.Build());
}

private static async Task SlashCmdXAsync(SocketSlashCommand cmd)
{
switch (cmd.Data.Name)
{
case "shutdown":
await CmdHandlers.ShutdownAsync(cmd);
break;
case "commit":
await CmdHandlers.CommitAsync(cmd);
break;
}
var ctx = new SocketInteractionContext(s_bot, cmd);
await s_interactionSvc.ExecuteCommandAsync(ctx, null);
}

internal static async Task InitAsync()
{
s_bot.Log += Log;
s_bot.Ready += Ready;
s_bot.SlashCommandExecuted += SlashCmdXAsync;
await s_interactionSvc.AddModulesAsync(Assembly.GetEntryAssembly(), null);
await s_bot.LoginAsync(TokenType.Bot, Secrets.s_discord);
await s_bot.StartAsync();
}
Expand Down
2 changes: 1 addition & 1 deletion Bot/Program.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace Hyperstellar;
namespace Hyperstellar;

public class Program
{
Expand Down
1 change: 1 addition & 0 deletions Bot/Sql/Db.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ internal sealed class Db
internal static void Commit()
{
s_db.Commit();
Console.WriteLine("Db committed");
s_db.BeginTransaction();
}

Expand Down

0 comments on commit f2ac3ff

Please sign in to comment.