From 0ab5a9f6c3765d4bc73cd651669dc5f29b4e356b Mon Sep 17 00:00:00 2001 From: Daniel-I-Am Date: Sat, 6 Jun 2020 01:44:11 +0200 Subject: [PATCH] Add global timer functionality from config --- ChaosBot/Discord/DiscordConnect.cs | 1 + ChaosBot/Discord/Services/TimerHandler.cs | 62 +++++++++++++++++++++++ 2 files changed, 63 insertions(+) create mode 100644 ChaosBot/Discord/Services/TimerHandler.cs diff --git a/ChaosBot/Discord/DiscordConnect.cs b/ChaosBot/Discord/DiscordConnect.cs index 6632236..2d38d95 100644 --- a/ChaosBot/Discord/DiscordConnect.cs +++ b/ChaosBot/Discord/DiscordConnect.cs @@ -38,6 +38,7 @@ namespace ChaosBot.Discord // we get the CommandHandler class here and call the InitializeAsync method to start things up for the CommandHandler service await services.GetRequiredService().InitializeAsync(); + TimerHandler.Initialize(services); await Task.Delay(-1); } diff --git a/ChaosBot/Discord/Services/TimerHandler.cs b/ChaosBot/Discord/Services/TimerHandler.cs new file mode 100644 index 0000000..5836b7e --- /dev/null +++ b/ChaosBot/Discord/Services/TimerHandler.cs @@ -0,0 +1,62 @@ +using System; +using System.Threading.Tasks; +using ChaosBot.Services; +using Discord; +using Discord.WebSocket; +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.DependencyInjection; +using NLog; + +namespace ChaosBot.Discord.Services +{ + public static class TimerHandler + { + private static readonly Logger _logger = Program._logger; + private static DiscordSocketClient _client; + + public static void Initialize(IServiceProvider services) + { + _client = services.GetRequiredService(); + + foreach (IConfigurationSection serverConfig in Program.Cfg.GetSection("Servers").GetChildren()) + { + long? lodestoneChannelSloganDescriptionId = serverConfig.GetValue("Lodestone:SloganDescription:Channel", null); + int refreshMinutes = serverConfig.GetValue("Lodestone:SloganDescription:RefreshMinutes", 60); + + if (lodestoneChannelSloganDescriptionId == null) continue; + + async void UpdateChannelSloganDescription() + { + SocketGuild guild = _client.GetGuild(Convert.ToUInt64(serverConfig.Key)); + if (guild == null) + { + _logger.Warn($"Guild {Convert.ToUInt64(serverConfig.Key)} not found"); + return; + } + + SocketChannel socketChannel = guild.GetChannel(Convert.ToUInt64(lodestoneChannelSloganDescriptionId)); + if (socketChannel == null) + { + _logger.Warn($"Channel {Convert.ToUInt64(lodestoneChannelSloganDescriptionId)} not found in server {guild.Name}"); + return; + } + + ITextChannel channel = socketChannel as ITextChannel; + if (channel == null) + { + _logger.Warn($"Could not cast channel {socketChannel.Id} to ITextChannel"); + return; + }; + + string description = LodestoneManager.GetFreeCompanyById(serverConfig.GetValue("Lodestone:FreeCompanyId")).FreeCompany.Slogan;; + await channel.ModifyAsync(x => + { + x.Topic = description; + }); + } + + Timer.RunTimer(UpdateChannelSloganDescription, new TimeSpan(TimeSpan.TicksPerMinute * refreshMinutes)); + } + } + } +} \ No newline at end of file