From eb2e61938e691d74a3f1eed6a61b77175eb517d9 Mon Sep 17 00:00:00 2001 From: Kumaraguru <19eucs071@skcet.ac.in> Date: Fri, 1 Nov 2024 01:59:28 +0000 Subject: [PATCH 1/2] providing user with altrustic mode role after verifying if the cid being checked is unique --- Tools/BiblioTech/Commands/CheckCidCommand.cs | 78 ++++++++++++++++++++ 1 file changed, 78 insertions(+) diff --git a/Tools/BiblioTech/Commands/CheckCidCommand.cs b/Tools/BiblioTech/Commands/CheckCidCommand.cs index 01b3b79..e6bc1f5 100644 --- a/Tools/BiblioTech/Commands/CheckCidCommand.cs +++ b/Tools/BiblioTech/Commands/CheckCidCommand.cs @@ -1,4 +1,8 @@ using BiblioTech.Options; +using System.IO; +using System.Linq; +using System.Threading.Tasks; +using Discord; namespace BiblioTech.Commands { @@ -9,10 +13,13 @@ namespace BiblioTech.Commands description: "Codex Content-Identifier", isRequired: true); private readonly CodexCidChecker checker; + private readonly CidStorage cidStorage; + private const ulong ALTRUISTIC_ROLE_ID = 1286134120379977860; public CheckCidCommand(CodexCidChecker checker) { this.checker = checker; + this.cidStorage = new CidStorage(Path.Combine(Program.Config.DataPath, "valid_cids.txt")); } public override string Name => "check"; @@ -32,7 +39,78 @@ namespace BiblioTech.Commands 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) + { + if (cidStorage.IsCidUsed(cid)) + { + await context.Followup($"{response.Message}\n\nThis CID has already been used by another user. No role will be granted."); + return; + } + + if (cidStorage.AddCid(cid, user.Id)) + { + var guildUser = context.Command.User as IGuildUser; + if (guildUser != null) + { + try + { + var role = context.Command.Guild.GetRole(ALTRUISTIC_ROLE_ID); + if (role != null) + { + await guildUser.AddRoleAsync(role); + await context.Followup($"{response.Message}\n\nCongratulations! You've been granted the Altruistic Mode role for checking a valid CID!"); + return; + } + } + catch (Exception ex) + { + await Program.AdminChecker.SendInAdminChannel($"Failed to grant Altruistic Mode role to user {Mention(user)}: {ex.Message}"); + } + } + } + } + await context.Followup(response.Message); } } + + 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 AddCid(string cid, ulong userId) + { + lock (_lock) + { + var existingEntries = File.ReadAllLines(filePath); + if (existingEntries.Any(line => line.Split(',')[0] == cid)) + { + return false; + } + + File.AppendAllText(filePath, $"{cid},{userId}{Environment.NewLine}"); + return true; + } + } + + public bool IsCidUsed(string cid) + { + lock (_lock) + { + var existingEntries = File.ReadAllLines(filePath); + return existingEntries.Any(line => line.Split(',')[0] == cid); + } + } + } } From 693880069b10245d8498492a5ec7de954e4ac390 Mon Sep 17 00:00:00 2001 From: Kumaraguru <19eucs071@skcet.ac.in> Date: Fri, 1 Nov 2024 14:43:29 +0000 Subject: [PATCH 2/2] improving codereadability and reducing redundant file operations --- Tools/BiblioTech/Commands/CheckCidCommand.cs | 84 +++++++++++--------- Tools/BiblioTech/Configuration.cs | 3 + 2 files changed, 48 insertions(+), 39 deletions(-) diff --git a/Tools/BiblioTech/Commands/CheckCidCommand.cs b/Tools/BiblioTech/Commands/CheckCidCommand.cs index e6bc1f5..cd47c7f 100644 --- a/Tools/BiblioTech/Commands/CheckCidCommand.cs +++ b/Tools/BiblioTech/Commands/CheckCidCommand.cs @@ -14,7 +14,6 @@ namespace BiblioTech.Commands isRequired: true); private readonly CodexCidChecker checker; private readonly CidStorage cidStorage; - private const ulong ALTRUISTIC_ROLE_ID = 1286134120379977860; public CheckCidCommand(CodexCidChecker checker) { @@ -42,37 +41,53 @@ namespace BiblioTech.Commands if (response.Success) { - if (cidStorage.IsCidUsed(cid)) - { - await context.Followup($"{response.Message}\n\nThis CID has already been used by another user. No role will be granted."); - return; - } - - if (cidStorage.AddCid(cid, user.Id)) - { - var guildUser = context.Command.User as IGuildUser; - if (guildUser != null) - { - try - { - var role = context.Command.Guild.GetRole(ALTRUISTIC_ROLE_ID); - if (role != null) - { - await guildUser.AddRoleAsync(role); - await context.Followup($"{response.Message}\n\nCongratulations! You've been granted the Altruistic Mode role for checking a valid CID!"); - return; - } - } - catch (Exception ex) - { - await Program.AdminChecker.SendInAdminChannel($"Failed to grant Altruistic Mode role to user {Mention(user)}: {ex.Message}"); - } - } - } + 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 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 @@ -89,7 +104,7 @@ namespace BiblioTech.Commands } } - public bool AddCid(string cid, ulong userId) + public bool TryAddCid(string cid, ulong userId) { lock (_lock) { @@ -99,18 +114,9 @@ namespace BiblioTech.Commands return false; } - File.AppendAllText(filePath, $"{cid},{userId}{Environment.NewLine}"); + File.AppendAllLines(filePath, new[] { $"{cid},{userId}" }); return true; } } - - public bool IsCidUsed(string cid) - { - lock (_lock) - { - var existingEntries = File.ReadAllLines(filePath); - return existingEntries.Any(line => line.Split(',')[0] == cid); - } - } } } diff --git a/Tools/BiblioTech/Configuration.cs b/Tools/BiblioTech/Configuration.cs index 7cacf9b..342fbb0 100644 --- a/Tools/BiblioTech/Configuration.cs +++ b/Tools/BiblioTech/Configuration.cs @@ -26,6 +26,9 @@ namespace BiblioTech [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;