Add global timer functionality from config
This commit is contained in:
parent
5595993080
commit
0ab5a9f6c3
@ -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
|
// we get the CommandHandler class here and call the InitializeAsync method to start things up for the CommandHandler service
|
||||||
await services.GetRequiredService<CommandHandler>().InitializeAsync();
|
await services.GetRequiredService<CommandHandler>().InitializeAsync();
|
||||||
|
TimerHandler.Initialize(services);
|
||||||
|
|
||||||
await Task.Delay(-1);
|
await Task.Delay(-1);
|
||||||
}
|
}
|
||||||
|
|||||||
62
ChaosBot/Discord/Services/TimerHandler.cs
Normal file
62
ChaosBot/Discord/Services/TimerHandler.cs
Normal file
@ -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<DiscordSocketClient>();
|
||||||
|
|
||||||
|
foreach (IConfigurationSection serverConfig in Program.Cfg.GetSection("Servers").GetChildren())
|
||||||
|
{
|
||||||
|
long? lodestoneChannelSloganDescriptionId = serverConfig.GetValue<long?>("Lodestone:SloganDescription:Channel", null);
|
||||||
|
int refreshMinutes = serverConfig.GetValue<int>("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<string>("Lodestone:FreeCompanyId")).FreeCompany.Slogan;;
|
||||||
|
await channel.ModifyAsync(x =>
|
||||||
|
{
|
||||||
|
x.Topic = description;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
Timer.RunTimer(UpdateChannelSloganDescription, new TimeSpan(TimeSpan.TicksPerMinute * refreshMinutes));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue
Block a user