From 8341807d927755a7ccd3599c6055e37b11d2e5a6 Mon Sep 17 00:00:00 2001 From: Ben Date: Wed, 26 Jun 2024 11:27:19 +0200 Subject: [PATCH] Disables role rewards (not configured in real server) --- Framework/DiscordRewards/RewardRepo.cs | 82 +++++++++++++------------- Tools/BiblioTech/CommandHandler.cs | 14 +++-- Tools/BiblioTech/Program.cs | 2 +- Tools/BiblioTech/Rewards/RoleDriver.cs | 68 +++++++++++++-------- 4 files changed, 92 insertions(+), 74 deletions(-) diff --git a/Framework/DiscordRewards/RewardRepo.cs b/Framework/DiscordRewards/RewardRepo.cs index 28f6173..1c97caf 100644 --- a/Framework/DiscordRewards/RewardRepo.cs +++ b/Framework/DiscordRewards/RewardRepo.cs @@ -1,53 +1,53 @@ -using Utils; - -namespace DiscordRewards +namespace DiscordRewards { public class RewardRepo { private static string Tag => RewardConfig.UsernameTag; - public RewardConfig[] Rewards { get; } = new RewardConfig[] - { - // Filled any slot - new RewardConfig(1187039439558541498, $"{Tag} successfully filled their first slot!", new CheckConfig - { - Type = CheckType.HostFilledSlot - }), + public RewardConfig[] Rewards { get; } = new RewardConfig[0]; - // Finished any slot - new RewardConfig(1202286165630390339, $"{Tag} successfully finished their first slot!", new CheckConfig - { - Type = CheckType.HostFinishedSlot - }), + // Example configuration, from test server: + //{ + // // Filled any slot + // new RewardConfig(1187039439558541498, $"{Tag} successfully filled their first slot!", new CheckConfig + // { + // Type = CheckType.HostFilledSlot + // }), - // Finished a sizable slot - new RewardConfig(1202286218738405418, $"{Tag} finished their first 1GB-24h slot! (10mb/5mins for test)", new CheckConfig - { - Type = CheckType.HostFinishedSlot, - MinSlotSize = 10.MB(), - MinDuration = TimeSpan.FromMinutes(5.0), - }), + // // Finished any slot + // new RewardConfig(1202286165630390339, $"{Tag} successfully finished their first slot!", new CheckConfig + // { + // Type = CheckType.HostFinishedSlot + // }), - // Posted any contract - new RewardConfig(1202286258370383913, $"{Tag} posted their first contract!", new CheckConfig - { - Type = CheckType.ClientPostedContract - }), + // // Finished a sizable slot + // new RewardConfig(1202286218738405418, $"{Tag} finished their first 1GB-24h slot! (10mb/5mins for test)", new CheckConfig + // { + // Type = CheckType.HostFinishedSlot, + // MinSlotSize = 10.MB(), + // MinDuration = TimeSpan.FromMinutes(5.0), + // }), - // Started any contract - new RewardConfig(1202286330873126992, $"A contract created by {Tag} reached Started state for the first time!", new CheckConfig - { - Type = CheckType.ClientStartedContract - }), + // // Posted any contract + // new RewardConfig(1202286258370383913, $"{Tag} posted their first contract!", new CheckConfig + // { + // Type = CheckType.ClientPostedContract + // }), - // Started a sizable contract - new RewardConfig(1202286381670608909, $"A large contract created by {Tag} reached Started state for the first time! (10mb/5mins for test)", new CheckConfig - { - Type = CheckType.ClientStartedContract, - MinNumberOfHosts = 4, - MinSlotSize = 10.MB(), - MinDuration = TimeSpan.FromMinutes(5.0), - }) - }; + // // Started any contract + // new RewardConfig(1202286330873126992, $"A contract created by {Tag} reached Started state for the first time!", new CheckConfig + // { + // Type = CheckType.ClientStartedContract + // }), + + // // Started a sizable contract + // new RewardConfig(1202286381670608909, $"A large contract created by {Tag} reached Started state for the first time! (10mb/5mins for test)", new CheckConfig + // { + // Type = CheckType.ClientStartedContract, + // MinNumberOfHosts = 4, + // MinSlotSize = 10.MB(), + // MinDuration = TimeSpan.FromMinutes(5.0), + // }) + //}; } } diff --git a/Tools/BiblioTech/CommandHandler.cs b/Tools/BiblioTech/CommandHandler.cs index c7263c5..4f6d7b8 100644 --- a/Tools/BiblioTech/CommandHandler.cs +++ b/Tools/BiblioTech/CommandHandler.cs @@ -3,6 +3,7 @@ using Discord.WebSocket; using Discord; using Newtonsoft.Json; using BiblioTech.Rewards; +using Logging; namespace BiblioTech { @@ -10,12 +11,13 @@ namespace BiblioTech { private readonly DiscordSocketClient client; private readonly BaseCommand[] commands; + private readonly ILog log; - public CommandHandler(DiscordSocketClient client, params BaseCommand[] commands) + public CommandHandler(ILog log, DiscordSocketClient client, params BaseCommand[] commands) { this.client = client; this.commands = commands; - + this.log = log; client.Ready += Client_Ready; client.SlashCommandExecuted += SlashCommandHandler; } @@ -24,12 +26,12 @@ namespace BiblioTech { var guild = client.Guilds.Single(g => g.Id == Program.Config.ServerId); Program.AdminChecker.SetGuild(guild); - Program.Log.Log($"Initializing for guild: '{guild.Name}'"); + log.Log($"Initializing for guild: '{guild.Name}'"); var adminChannels = guild.TextChannels.Where(Program.AdminChecker.IsAdminChannel).ToArray(); if (adminChannels == null || !adminChannels.Any()) throw new Exception("No admin message channel"); Program.AdminChecker.SetAdminChannel(adminChannels.First()); - Program.RoleDriver = new RoleDriver(client); + Program.RoleDriver = new RoleDriver(client, log); var builders = commands.Select(c => { @@ -44,7 +46,7 @@ namespace BiblioTech builder.AddOption(option.Build()); } - Program.Log.Log(msg); + log.Log(msg); return builder; }); @@ -58,7 +60,7 @@ namespace BiblioTech catch (HttpException exception) { var json = JsonConvert.SerializeObject(exception.Errors, Formatting.Indented); - Program.Log.Error(json); + log.Error(json); } } diff --git a/Tools/BiblioTech/Program.cs b/Tools/BiblioTech/Program.cs index 6ec7dae..39bd58b 100644 --- a/Tools/BiblioTech/Program.cs +++ b/Tools/BiblioTech/Program.cs @@ -73,7 +73,7 @@ namespace BiblioTech var notifyCommand = new NotifyCommand(); var associateCommand = new UserAssociateCommand(notifyCommand); var sprCommand = new SprCommand(); - var handler = new CommandHandler(client, + var handler = new CommandHandler(Log, client, new GetBalanceCommand(associateCommand), new MintCommand(associateCommand), sprCommand, diff --git a/Tools/BiblioTech/Rewards/RoleDriver.cs b/Tools/BiblioTech/Rewards/RoleDriver.cs index f0ea48f..c7173c1 100644 --- a/Tools/BiblioTech/Rewards/RoleDriver.cs +++ b/Tools/BiblioTech/Rewards/RoleDriver.cs @@ -1,6 +1,7 @@ using Discord; using Discord.WebSocket; using DiscordRewards; +using Logging; using Newtonsoft.Json; namespace BiblioTech.Rewards @@ -8,21 +9,22 @@ namespace BiblioTech.Rewards public class RoleDriver : IDiscordRoleDriver { private readonly DiscordSocketClient client; + private readonly ILog log; private readonly SocketTextChannel? rewardsChannel; private readonly SocketTextChannel? eventsChannel; private readonly RewardRepo repo = new RewardRepo(); - public RoleDriver(DiscordSocketClient client) + public RoleDriver(DiscordSocketClient client, ILog log) { this.client = client; - + this.log = log; rewardsChannel = GetChannel(Program.Config.RewardsChannelId); eventsChannel = GetChannel(Program.Config.ChainEventsChannelId); } public async Task GiveRewards(GiveRewardsCommand rewards) { - Program.Log.Log($"Processing rewards command: '{JsonConvert.SerializeObject(rewards)}'"); + log.Log($"Processing rewards command: '{JsonConvert.SerializeObject(rewards)}'"); if (rewards.Rewards.Any()) { @@ -34,15 +36,22 @@ namespace BiblioTech.Rewards private async Task ProcessRewards(GiveRewardsCommand rewards) { - var guild = GetGuild(); - // We load all role and user information first, - // so we don't ask the server for the same info multiple times. - var context = new RewardContext( - await LoadAllUsers(guild), - LookUpAllRoles(guild, rewards), - rewardsChannel); + try + { + var guild = GetGuild(); + // We load all role and user information first, + // so we don't ask the server for the same info multiple times. + var context = new RewardContext( + await LoadAllUsers(guild), + LookUpAllRoles(guild, rewards), + rewardsChannel); - await context.ProcessGiveRewardsCommand(LookUpUsers(rewards)); + await context.ProcessGiveRewardsCommand(LookUpUsers(rewards)); + } + catch (Exception ex) + { + log.Error("Failed to process rewards: " + ex); + } } private SocketTextChannel? GetChannel(ulong id) @@ -54,22 +63,29 @@ namespace BiblioTech.Rewards private async Task ProcessChainEvents(string[] eventsOverview) { if (eventsChannel == null || eventsOverview == null || !eventsOverview.Any()) return; - await Task.Run(async () => + try { - foreach (var e in eventsOverview) + await Task.Run(async () => { - if (!string.IsNullOrEmpty(e)) + foreach (var e in eventsOverview) { - await eventsChannel.SendMessageAsync(e); - await Task.Delay(3000); + if (!string.IsNullOrEmpty(e)) + { + await eventsChannel.SendMessageAsync(e); + await Task.Delay(3000); + } } - } - }); + }); + } + catch (Exception ex) + { + log.Error("Failed to process chain events: " + ex); + } } private async Task> LoadAllUsers(SocketGuild guild) { - Program.Log.Log("Loading all users:"); + log.Log("Loading all users.."); var result = new Dictionary(); var users = guild.GetUsersAsync(); await foreach (var ulist in users) @@ -77,8 +93,8 @@ namespace BiblioTech.Rewards foreach (var u in ulist) { result.Add(u.Id, u); - var roleIds = string.Join(",", u.RoleIds.Select(r => r.ToString()).ToArray()); - Program.Log.Log($" > {u.Id}({u.DisplayName}) has [{roleIds}]"); + //var roleIds = string.Join(",", u.RoleIds.Select(r => r.ToString()).ToArray()); + //log.Log($" > {u.Id}({u.DisplayName}) has [{roleIds}]"); } } return result; @@ -94,14 +110,14 @@ namespace BiblioTech.Rewards var rewardConfig = repo.Rewards.SingleOrDefault(rr => rr.RoleId == r.RewardId); if (rewardConfig == null) { - Program.Log.Log($"No Reward is configured for id '{r.RewardId}'."); + log.Log($"No Reward is configured for id '{r.RewardId}'."); } else { var socketRole = guild.GetRole(r.RewardId); if (socketRole == null) { - Program.Log.Log($"Guild Role by id '{r.RewardId}' not found."); + log.Log($"Guild Role by id '{r.RewardId}' not found."); } else { @@ -134,13 +150,13 @@ namespace BiblioTech.Rewards try { var userData = Program.UserRepo.GetUserDataForAddress(new GethPlugin.EthAddress(address)); - if (userData != null) Program.Log.Log($"User '{userData.Name}' was looked up."); - else Program.Log.Log($"Lookup for user was unsuccessful. EthAddress: '{address}'"); + if (userData != null) log.Log($"User '{userData.Name}' was looked up."); + else log.Log($"Lookup for user was unsuccessful. EthAddress: '{address}'"); return userData; } catch (Exception ex) { - Program.Log.Error("Error during UserData lookup: " + ex); + log.Error("Error during UserData lookup: " + ex); return null; } }