Skip to content

Commit

Permalink
[Bot] More checks for Alt command
Browse files Browse the repository at this point in the history
  • Loading branch information
Pythonic-Rainbow committed Jan 21, 2024
1 parent a15fa75 commit 13d10cf
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 13 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/bot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,9 @@ jobs:
with:
name: Bot
path: |
Bot/bin/Release/net8.0/linux-arm64/*
Bot/bin/Release/net8.0/linux-arm64
!Bot/bin/Release/net8.0/linux-arm64/*.pdb
!Bot/bin/Release/net8.0/linux-arm64/publish
inspect:
runs-on: ubuntu-latest
steps:
Expand Down
16 changes: 16 additions & 0 deletions Bot/Dc/Cmds.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,22 @@ public async Task AdminAsync(SocketGuildUser user)
[SlashCommand("alt", "Links an alt to a main")]
public async Task AltAsync(Member alt, Member main)
{
if (alt.CocId == main.CocId)
{
await RespondAsync("Bro alt must be different from main bruh");
return;
}
if (main.IsAlt())
{
await RespondAsync("Main can't be an alt in the database!");
return;
}
if (alt.IsMain())
{
await RespondAsync("Alt can't be a main in the database!");
return;
}

main.AddAlt(alt);
ClanMember clanAlt = Coc.GetMember(alt.CocId);
ClanMember clanMain = Coc.GetMember(main.CocId);
Expand Down
18 changes: 6 additions & 12 deletions Bot/Dc/Discord.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
using Discord.Interactions;
using Discord.WebSocket;
using Hyperstellar.Sql;
//using Microsoft.Extensions.DependencyInjection;
using static Hyperstellar.Coc;

namespace Hyperstellar.Dc;
Expand All @@ -13,11 +12,12 @@ internal sealed class Discord

#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;
private static InteractionService s_interactionSvc;
#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);
internal static readonly DiscordSocketClient s_bot = new();


private static Task Log(LogMessage msg)
{
Console.WriteLine(msg.ToString());
Expand All @@ -27,14 +27,8 @@ private static Task Log(LogMessage msg)
private static async Task Ready()
{
s_botLog = (SocketTextChannel)s_bot.GetChannel(Secrets.s_botLogId);
Task.Run(BotReadyAsync);
//await s_interactionSvc.RegisterCommandsGloballyAsync();
SlashCommandBuilder guildCmd = new SlashCommandBuilder().WithName("alt").WithDescription("Links an alt to a main");
guildCmd.AddOption("alt", ApplicationCommandOptionType.String, "alt", isRequired: true);
guildCmd.AddOption("main", ApplicationCommandOptionType.String, "main", isRequired: true);
await s_bot.CreateGlobalApplicationCommandAsync(guildCmd.Build());
Console.WriteLine("Registered commands");
//return Task.CompletedTask;
_ = Task.Run(BotReadyAsync);
await s_interactionSvc.RegisterCommandsGloballyAsync();
}

private static async Task SlashCmdXAsync(SocketSlashCommand cmd)
Expand All @@ -53,14 +47,14 @@ private static async Task InteractionXAsync(ICommandInfo info, IInteractionConte

internal static async Task InitAsync()
{
s_interactionSvc = new(s_bot);
s_bot.Log += Log;
s_bot.Ready += Ready;
s_bot.SlashCommandExecuted += SlashCmdXAsync;
s_interactionSvc.InteractionExecuted += InteractionXAsync;

s_interactionSvc.AddTypeConverter<Member>(new MemberConverter());

await s_interactionSvc.AddModulesAsync(Assembly.GetEntryAssembly(), null); // Custom SP
await s_interactionSvc.AddModulesAsync(Assembly.GetEntryAssembly(), null);
await s_bot.LoginAsync(TokenType.Bot, Secrets.s_discord);
await s_bot.StartAsync();
}
Expand Down
12 changes: 12 additions & 0 deletions Bot/Sql/Member.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,16 @@ public void AddAlt(Member altMember)
Alt alt = new(altMember.CocId, CocId);
Db.s_db.Insert(alt);
}

public bool IsAlt()
{
TableQuery<Alt> result = Db.s_db.Table<Alt>().Where(a => a.AltId == CocId);
return result.Count() > 0;
}

public bool IsMain()
{
TableQuery<Alt> result = Db.s_db.Table<Alt>().Where(a => a.MainId == CocId);
return result.Count() > 0;
}
}

0 comments on commit 13d10cf

Please sign in to comment.