Skip to content

Commit

Permalink
[Bot] Add admin command
Browse files Browse the repository at this point in the history
  • Loading branch information
Pythonic-Rainbow committed Jan 20, 2024
1 parent f2ac3ff commit cad3f5a
Show file tree
Hide file tree
Showing 7 changed files with 89 additions and 41 deletions.
11 changes: 10 additions & 1 deletion Bot/Bot.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,21 @@
<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" />
<PackageReference Include="SQLitePCLRaw.bundle_green" Version="2.1.7" />
</ItemGroup>

<ItemGroup>
<Reference Include="SQLite-net">
<HintPath>SQLite-net.dll</HintPath>
</Reference>
</ItemGroup>

<ItemGroup>
<None Update="secrets.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="SQLite-net.dll">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>
</Project>
36 changes: 0 additions & 36 deletions Bot/Dc/CmdHandlers.cs

This file was deleted.

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

namespace Hyperstellar.Dc;

public class Commands : InteractionModuleBase

Check warning on line 7 in Bot/Dc/Commands.cs

View workflow job for this annotation

GitHub Actions / inspect

"[CA1724] The type name Commands conflicts in whole or in part with the namespace name 'Discord.Commands'. Change either name to eliminate the conflict." on /home/runner/work/Hyperstellar/Hyperstellar/Bot/Dc/Commands.cs(7,119)

Check warning on line 7 in Bot/Dc/Commands.cs

View workflow job for this annotation

GitHub Actions / build

The type name Commands conflicts in whole or in part with the namespace name 'Discord.Commands'. Change either name to eliminate the conflict. (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1724)
{
[RequireOwner]
[SlashCommand("shutdown", "Shuts down the bot")]
public async Task ShutdownAsync(bool commit = true)
{
await RespondAsync("Ok", ephemeral: true);
if (commit)
{
Db.Commit();
}
Environment.Exit(0);
}

[RequireOwner]
[SlashCommand("commit", "Commits db")]
public async Task CommitAsync()
{
Db.Commit();
await RespondAsync("Committed", ephemeral: true);
}

[RequireOwner]
[SlashCommand("admin", "Makes the Discord user an admin")]
public async Task AdminAsync(SocketGuildUser user)
{
bool success = Db.AddAdmin(user.Id);
if (success)
{
await RespondAsync("Success!", ephemeral: true);
}
else
{
await RespondAsync("Error", ephemeral: true);
}
}
}
14 changes: 12 additions & 2 deletions Bot/Dc/Discord.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,19 @@ private static Task Log(LogMessage msg)
return Task.CompletedTask;
}

private static async Task Ready()
private static Task Ready()
{
s_botLog = (SocketTextChannel)s_bot.GetChannel(Secrets.s_botLogId);
_ = Task.Run(BotReadyAsync);
Task.Run(BotReadyAsync);

SlashCommandBuilder cmd = new SlashCommandBuilder().WithName("admin").WithDescription("Makes the Discord user an admin");
cmd.AddOption("user", ApplicationCommandOptionType.User, "the user", isRequired: true);
Task.Run(() =>
{
s_bot.CreateGlobalApplicationCommandAsync(cmd.Build());
Console.WriteLine("Registered command");
});
return Task.CompletedTask;
}

private static async Task SlashCmdXAsync(SocketSlashCommand cmd)
Expand All @@ -39,6 +48,7 @@ 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
Binary file added Bot/SQLite-net.dll
Binary file not shown.
13 changes: 13 additions & 0 deletions Bot/Sql/BotAdmin.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using SQLite;

namespace Hyperstellar.Sql;

internal sealed class BotAdmin
{
[PrimaryKey, NotNull]
public ulong Id { get; set; }

public BotAdmin() => Id = 0;

public BotAdmin(ulong id) => Id = id;
}
13 changes: 11 additions & 2 deletions Bot/Sql/Db.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ namespace Hyperstellar.Sql;
internal sealed class Db
{
internal static readonly SQLiteConnection s_db = new("Hyperstellar.db");
internal static readonly HashSet<ulong> s_admins = s_db.Table<BotAdmin>().Select(a => a.Id).ToHashSet();

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

View workflow job for this annotation

GitHub Actions / inspect

"[CollectionNeverQueried.Global] Content of collection 's_admins' is only updated but never used" on /home/runner/work/Hyperstellar/Hyperstellar/Bot/Sql/Db.cs(8,192)

internal static void Commit()
{
Expand All @@ -27,13 +28,21 @@ internal static bool RemoveMembers(string[] members)
return count == members.Length;
}

internal static bool AddAdmin(ulong id)
{
var admin = new BotAdmin(id);
int count = s_db.Insert(admin);
s_admins.Add(id);
return count == 1;
}

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

Check warning on line 47 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(47,1239)
}

0 comments on commit cad3f5a

Please sign in to comment.