Skip to content

Commit

Permalink
[Bot] Started db pattern redesign
Browse files Browse the repository at this point in the history
  • Loading branch information
Pythonic-Rainbow committed Sep 18, 2024
1 parent 4a83a9c commit ef7f62a
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 19 deletions.
2 changes: 1 addition & 1 deletion Bot/Clash/Coc.cs
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ static async Task InitClanAsync()
}
catch (ClashOfClansException ex)
{
if (ex.Error.Reason == "accessDenied")
if (ex.Error.Reason.StartsWith("accessDenied"))
{
counter++;
continue;

Check warning on line 298 in Bot/Clash/Coc.cs

View workflow job for this annotation

GitHub Actions / inspect

"[RedundantJumpStatement] Redundant control flow jump statement" on /home/runner/work/Hyperstellar/Hyperstellar/Bot/Clash/Coc.cs(298,10633)
Expand Down
2 changes: 1 addition & 1 deletion Bot/Discord/Attr/RequireAdmin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace Hyperstellar.Discord.Attr;

internal sealed class RequireAdmin : PreconditionAttribute
{
public override Task<PreconditionResult> CheckRequirementsAsync(IInteractionContext context, ICommandInfo commandInfo, IServiceProvider services) => Db.s_admins.Contains(context.User.Id)
public override Task<PreconditionResult> CheckRequirementsAsync(IInteractionContext context, ICommandInfo commandInfo, IServiceProvider services) => BotAdmin.s_admins.Contains(context.User.Id)
? Task.FromResult(PreconditionResult.FromSuccess())
: Task.FromResult(PreconditionResult.FromError("Bro you're not an admin smh"));
}
2 changes: 1 addition & 1 deletion Bot/Discord/Cmds.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public async Task CommitAsync()
[SlashCommand("admin", "[Owner] Makes the Discord user an admin")] // Maybe rename to addadmin
public async Task AdminAsync(SocketGuildUser user)
{
bool success = Db.AddAdmin(user.Id);
bool success = new BotAdmin(user.Id).Insert() == 1;
if (success)
{
await RespondAsync("Success!", ephemeral: true);
Expand Down
12 changes: 11 additions & 1 deletion Bot/Sql/BotAdmin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,20 @@

namespace Hyperstellar.Sql;

internal sealed class BotAdmin(ulong id)
internal sealed class BotAdmin(ulong id) : DbObj
{
internal static readonly HashSet<ulong> s_admins = s_db.Table<BotAdmin>().Select(a => a.Id).ToHashSet();

Check warning on line 7 in Bot/Sql/BotAdmin.cs

View workflow job for this annotation

GitHub Actions / inspect

"[IDE0320] Anonymous function can be made static" on /home/runner/work/Hyperstellar/Hyperstellar/Bot/Sql/BotAdmin.cs(7,180)


[PrimaryKey, NotNull]
public ulong Id { get; set; } = id;

public BotAdmin() : this(0) { }

internal override int Insert()
{
int count = base.Insert();
s_admins.Add(Id);
return count;
}
}
17 changes: 2 additions & 15 deletions Bot/Sql/Db.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,9 @@ namespace Hyperstellar.Sql;

internal static 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();
internal static readonly SQLiteConnection s_db = DbObj.s_db;

internal static void Commit()
{
s_db.Commit();
s_db.BeginTransaction();
}
internal static void Commit() => DbObj.Commit();

internal static IEnumerable<Member> GetMembers() => s_db.Table<Member>();

Expand All @@ -35,14 +30,6 @@ internal static bool AddMembers(string[] members)

internal static Alt? TryGetAlt(string altId) => s_db.Table<Alt>().FirstOrDefault(a => a.AltId == altId);

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

internal static CocMemberAlias? TryGetAlias(string alias)
{
alias = alias.ToLower();
Expand Down
20 changes: 20 additions & 0 deletions Bot/Sql/DbObj.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using System.Collections;
using SQLite;

namespace Hyperstellar.Sql;

internal abstract class DbObj
{
// Make this private after migration
internal static readonly SQLiteConnection s_db = new("Hyperstellar.db");

internal static void Commit()
{
s_db.Commit();
s_db.BeginTransaction();
}

internal static int InsertAll(IEnumerable objects) => s_db.InsertAll(objects);

internal virtual int Insert() => s_db.Insert(this);
}

0 comments on commit ef7f62a

Please sign in to comment.