72 lines
3.0 KiB
C#
72 lines
3.0 KiB
C#
using System;
|
|
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 ILogger _logger = Program.Logger;
|
|
private static DiscordSocketClient _client;
|
|
|
|
public static void Initialize(IServiceProvider services)
|
|
{
|
|
_client = services.GetRequiredService<DiscordSocketClient>();
|
|
|
|
foreach (IConfigurationSection serverConfig in Program.AppSettingsHandler.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()
|
|
{
|
|
try
|
|
{
|
|
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;
|
|
}
|
|
|
|
if (!guild.GetUser(_client.CurrentUser.Id).GetPermissions(channel).ManageChannel) return;
|
|
|
|
string description = LodestoneManager
|
|
.GetFreeCompanyById(serverConfig.GetValue<string>("Lodestone:FreeCompanyId")).FreeCompany
|
|
.Slogan;
|
|
|
|
await channel.ModifyAsync(x => { x.Topic = description; });
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.Error($"TimerHandler.UpdateChannelSloganDescription: Exception [{ex}] thrown, <[{ex.Message}]>.");
|
|
}
|
|
}
|
|
|
|
Timer.RunTimer(UpdateChannelSloganDescription, new TimeSpan(TimeSpan.TicksPerMinute * refreshMinutes));
|
|
}
|
|
}
|
|
}
|
|
} |