Skip to content

Commit

Permalink
[Bot] Add basic support for alias
Browse files Browse the repository at this point in the history
  • Loading branch information
Pythonic-Rainbow committed Aug 20, 2024
1 parent df752d7 commit 86c3c06
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 8 deletions.
6 changes: 6 additions & 0 deletions Bot/Clash/Coc.cs
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,12 @@ private static async Task CheckDonationsAsync(ClanUtil clan)
return result?.Tag;
}

internal static ClanMember? TryGetMember(string id)
{
Clan._members.TryGetValue(id, out ClanMember? result);
return result;
}

internal static ClanMember GetMember(string id) => Clan._members[id];

internal static HashSet<ClanCapitalRaidSeasonAttacker> GetRaidAttackers(ClanCapitalRaidSeason season)
Expand Down
40 changes: 34 additions & 6 deletions Bot/Discord/Cmds.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,22 +91,22 @@ public async Task LinkAsync(Member coc, IGuildUser discord)
await RespondAsync("Linked");
}

[SlashCommand("info", "Shows info of a Coc member")]
public async Task InfoAsync(Member member)
[SlashCommand("info", "Shows info of a clan member")]
public async Task InfoAsync(Member clanMember)
{
ClanMember cocMem = Coc.GetMember(member.CocId);
ClanMember cocMem = Coc.GetMember(clanMember.CocId);

EmbedBuilder embed = new()
{
Title = cocMem.Name,
Author = new EmbedAuthorBuilder { Name = cocMem.Tag }
};

Alt? alt = member.TryToAlt();
Alt? alt = clanMember.TryToAlt();
if (alt == null)
{
embed.Description = string.Join(", ", member.GetAltsByMain().Select(a => Coc.GetMember(a.AltId).Name));
Main main = member.ToMain();
embed.Description = string.Join(", ", clanMember.GetAltsByMain().Select(a => Coc.GetMember(a.AltId).Name));

Check warning on line 108 in Bot/Discord/Cmds.cs

View workflow job for this annotation

GitHub Actions / inspect

"[IDE0320] Anonymous function can be made static" on /home/runner/work/Hyperstellar/Hyperstellar/Bot/Discord/Cmds.cs(108,2992)
Main main = clanMember.ToMain();
if (main.Discord != null)
{
embed.AddField("Discord", $"<@{main.Discord}>");
Expand All @@ -129,4 +129,32 @@ public async Task InfoAsync(Member member)

await RespondAsync(embed: embed.Build());
}

[RequireAdmin]
[SlashCommand("alias", "Sets an alias for a Coc member")]
public async Task AliasAsync(string alias, Member member)
{
bool success = Db.AddAlias(alias, member);
if (success)
{
await RespondAsync($"`{alias}` is now an alias of `{member.GetName()}`");
}
else
{
await RespondAsync("Failed to add alias.");
}
}

[SlashCommand("aliases", "Lists all aliases")]
public async Task AliasesAsync()
{
string msg = "";
foreach (CocMemberAlias alias in Db.GetAliases())
{
ClanMember? clanMember = Coc.TryGetMember(alias.CocId);
string name = clanMember == null ? "" : clanMember.Name;
msg += $"{alias.Alias} -> {name} {alias.CocId}\n";
}
await RespondAsync(msg);
}
}
8 changes: 6 additions & 2 deletions Bot/Discord/MemberConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ internal sealed class MemberConverter : TypeConverter
public override Task<TypeConverterResult> ReadAsync(IInteractionContext context, IApplicationCommandInteractionDataOption option, IServiceProvider services)
{
string input = (string)option.Value;
// Console.WriteLine($"\"{input}\"");
Member? member = Db.GetMember(input);
if (member == null)
{
Expand All @@ -22,8 +23,11 @@ public override Task<TypeConverterResult> ReadAsync(IInteractionContext context,
}
}
return member == null
? Task.FromResult(TypeConverterResult.FromError(InteractionCommandError.ConvertFailed,
$"Invalid `{option.Name}`: To specify a clan member, enter his name/ID with #"))
? Task.FromResult(TypeConverterResult.FromError(
InteractionCommandError.ConvertFailed,
@$"Invalid `{option.Name}`. To specify a clan member:
* Enter his name (非速本主義Arkyo), alias (Arkyo) or ID with # (#28QL0CJV2)
* Mention his Discord (@Dim) __which will refer to his main__"))
: Task.FromResult(TypeConverterResult.FromSuccess(member));
}
}
15 changes: 15 additions & 0 deletions Bot/Sql/Alias.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using SQLite;

namespace Hyperstellar.Sql;

[Table("Alias")]
public sealed class CocMemberAlias(string alias, string cocId)
{
[PrimaryKey, NotNull]
public string Alias { get; set; } = alias;

[NotNull]
public string CocId { get; set; } = cocId;

public CocMemberAlias() : this("", "") { }
}
9 changes: 9 additions & 0 deletions Bot/Sql/Db.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,15 @@ internal static bool AddAdmin(ulong id)
return count == 1;
}

internal static IEnumerable<CocMemberAlias> GetAliases() => s_db.Table<CocMemberAlias>();

internal static bool AddAlias(string alias, Member member)
{
CocMemberAlias cocMemberAlias = new(alias, member.CocId);
int count = s_db.Insert(cocMemberAlias);
return count == 1;
}

internal static async Task InitAsync()
{
s_db.BeginTransaction();
Expand Down

0 comments on commit 86c3c06

Please sign in to comment.