Skip to content

Commit

Permalink
[Bot] Add shutdown and commit commands
Browse files Browse the repository at this point in the history
  • Loading branch information
Pythonic-Rainbow committed Jan 19, 2024
1 parent ad86f72 commit d72157d
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 14 deletions.
1 change: 1 addition & 0 deletions Bot/.editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ dotnet_diagnostic.IDE0073.severity = none
csharp_style_unused_value_assignment_preference = discard_variable:suggestion
csharp_style_unused_value_expression_statement_preference = discard_variable:silent
dotnet_diagnostic.IDE0058.severity = none
dotnet_diagnostic.IDE0010.severity = none

# C++ Files
[*.{cpp,h,in}]
Expand Down
3 changes: 2 additions & 1 deletion Bot/Coc.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using ClashOfClans;
using ClashOfClans.Models;
using Hyperstellar.Sql;
using static Hyperstellar.Dc.Discord;

namespace Hyperstellar;

Expand Down Expand Up @@ -117,7 +118,7 @@ private static async Task CheckDonationsAsync(ClanUtil clan)
}
if (donationsDelta.Count > 0)
{
await Discord.DonationsChangedAsync(donationsDelta);
await DonationsChangedAsync(donationsDelta);
}
}

Expand Down
34 changes: 34 additions & 0 deletions Bot/Dc/CmdHandlers.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using Discord.WebSocket;
using Hyperstellar.Sql;

namespace Hyperstellar.Dc;

internal class CmdHandlers

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

View workflow job for this annotation

GitHub Actions / inspect

"[CA1852] Type 'CmdHandlers' can be sealed because it has no subtypes in its containing assembly and is not externally visible" on /home/runner/work/Hyperstellar/Hyperstellar/Bot/Dc/CmdHandlers.cs(6,93)

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

View workflow job for this annotation

GitHub Actions / build

Type 'CmdHandlers' can be sealed because it has no subtypes in its containing assembly and is not externally visible (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1852)
{
internal static async Task ShutdownAsync(SocketSlashCommand cmd)
{
if (cmd.User.Id != 264756129916125184)
{
return;
}

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

internal static async Task CommitAsync(SocketSlashCommand cmd)
{
if (cmd.User.Id != 264756129916125184)
{
return;
}

Db.Commit();
await cmd.RespondAsync("Committed", ephemeral: true);
}
}
35 changes: 30 additions & 5 deletions Bot/Discord.cs → Bot/Dc/Discord.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,58 @@
using Discord.WebSocket;
using static Hyperstellar.Coc;

namespace Hyperstellar;
namespace Hyperstellar.Dc;

internal sealed class Discord
{
private static readonly DiscordSocketClient s_bot = new();

#pragma warning disable CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider declaring as nullable.
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.

internal static readonly DiscordSocketClient s_bot = new();

private static Task Log(LogMessage msg)
{
Console.WriteLine(msg.ToString());
return Task.CompletedTask;
}

private static Task Ready()
private static async Task Ready()
{
s_botLog = (SocketTextChannel)s_bot.GetChannel(Secrets.s_botLogId);
Task.Run(BotReadyAsync);
return Task.CompletedTask;
_ = 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;
}
}

internal static async Task InitAsync()
{
s_bot.Log += Log;
s_bot.Ready += Ready;
s_bot.SlashCommandExecuted += SlashCmdXAsync;
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
Expand Up @@ -3,7 +3,7 @@
public class Program
{
public static async Task Main() => await Task.WhenAll([
Discord.InitAsync(),
Dc.Discord.InitAsync(),
Coc.InitAsync(),
Sql.Db.InitAsync()
]);
Expand Down
20 changes: 13 additions & 7 deletions Bot/Sql/Db.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,10 @@ internal sealed class Db
{
internal static readonly SQLiteConnection s_db = new("Hyperstellar.db");

internal static async Task InitAsync()
internal static void Commit()
{
while (true)
{
s_db.BeginTransaction();
await Task.Delay(5 * 60 * 1000);
s_db.Commit();
}
s_db.Commit();
s_db.BeginTransaction();
}

internal static IEnumerable<Member> GetMembers() => s_db.Table<Member>();
Expand All @@ -29,4 +25,14 @@ internal static bool RemoveMembers(string[] members)
}
return count == members.Length;
}

internal static async Task InitAsync()
{
while (true)
{
s_db.BeginTransaction();
await Task.Delay(5 * 60 * 1000);
s_db.Commit();
}
}

Check warning on line 37 in Bot/Sql/Db.cs

View workflow job for this annotation

GitHub Actions / inspect

"[FunctionNeverReturns] Function never returns" on /home/runner/work/Hyperstellar/Hyperstellar/Bot/Sql/Db.cs(37,908)
}

0 comments on commit d72157d

Please sign in to comment.