Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/feature/bot-assigns-altruisticmo…
Browse files Browse the repository at this point in the history
…de-role'
  • Loading branch information
benbierens committed Nov 29, 2024
2 parents 208cd2e + 7989cdd commit 10b0136
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 0 deletions.
84 changes: 84 additions & 0 deletions Tools/BiblioTech/Commands/CheckCidCommand.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
using BiblioTech.Options;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Discord;

namespace BiblioTech.Commands
{
Expand All @@ -9,10 +13,12 @@ public class CheckCidCommand : BaseCommand
description: "Codex Content-Identifier",
isRequired: true);
private readonly CodexCidChecker checker;
private readonly CidStorage cidStorage;

public CheckCidCommand(CodexCidChecker checker)
{
this.checker = checker;
this.cidStorage = new CidStorage(Path.Combine(Program.Config.DataPath, "valid_cids.txt"));
}

public override string Name => "check";
Expand All @@ -32,7 +38,85 @@ protected override async Task Invoke(CommandContext context)

var response = await checker.PerformCheck(cid);
await Program.AdminChecker.SendInAdminChannel($"User {Mention(user)} used '/{Name}' for cid '{cid}'. Lookup-success: {response.Success}. Message: '{response.Message}' Error: '{response.Error}'");

if (response.Success)
{
await CheckAltruisticRole(context, user, cid, response.Message);
return;
}

await context.Followup(response.Message);
}

private async Task CheckAltruisticRole(CommandContext context, IUser user, string cid, string responseMessage)
{
if (cidStorage.TryAddCid(cid, user.Id))
{
if (await GiveAltruisticRole(context, user, responseMessage))
{
return;
}
}
else
{
await context.Followup($"{responseMessage}\n\nThis CID has already been used by another user. No role will be granted.");
return;
}

await context.Followup(responseMessage);
}

private async Task<bool> GiveAltruisticRole(CommandContext context, IUser user, string responseMessage)
{
var guildUser = context.Command.User as IGuildUser;
if (guildUser != null)
{
try
{
var role = context.Command.Guild.GetRole(Program.Config.AltruisticRoleId);
if (role != null)
{
await guildUser.AddRoleAsync(role);
await context.Followup($"{responseMessage}\n\nCongratulations! You've been granted the Altruistic Mode role for checking a valid CID!");
return true;
}
}
catch (Exception ex)
{
await Program.AdminChecker.SendInAdminChannel($"Failed to grant Altruistic Mode role to user {Mention(user)}: {ex.Message}");
}
}
return false;
}
}

public class CidStorage
{
private readonly string filePath;
private static readonly object _lock = new object();

public CidStorage(string filePath)
{
this.filePath = filePath;
if (!File.Exists(filePath))
{
File.WriteAllText(filePath, string.Empty);
}
}

public bool TryAddCid(string cid, ulong userId)
{
lock (_lock)
{
var existingEntries = File.ReadAllLines(filePath);
if (existingEntries.Any(line => line.Split(',')[0] == cid))
{
return false;
}

File.AppendAllLines(filePath, new[] { $"{cid},{userId}" });
return true;
}
}
}
}
3 changes: 3 additions & 0 deletions Tools/BiblioTech/Configuration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ public class Configuration
[Uniform("chain-events-channel-id", "cc", "CHAINEVENTSCHANNELID", false, "ID of the Discord server channel where chain events will be posted.")]
public ulong ChainEventsChannelId { get; set; }

[Uniform("altruistic-role-id", "ar", "ALTRUISTICROLE", true, "ID of the Discord server role for Altruistic Mode.")]
public ulong AltruisticRoleId { get; set; }

[Uniform("reward-api-port", "rp", "REWARDAPIPORT", true, "TCP listen port for the reward API.")]
public int RewardApiPort { get; set; } = 31080;

Expand Down

0 comments on commit 10b0136

Please sign in to comment.