Try-catch the timer functionality

This commit is contained in:
Daniel_I_Am 2020-06-08 14:20:42 +02:00
parent 2adc291b7d
commit a06c6d5e91
No known key found for this signature in database
GPG Key ID: 80C428FCC9743E84

View File

@ -27,6 +27,8 @@ namespace ChaosBot.Discord.Services
if (lodestoneChannelSloganDescriptionId == null) continue; if (lodestoneChannelSloganDescriptionId == null) continue;
async void UpdateChannelSloganDescription() async void UpdateChannelSloganDescription()
{
try
{ {
SocketGuild guild = _client.GetGuild(Convert.ToUInt64(serverConfig.Key)); SocketGuild guild = _client.GetGuild(Convert.ToUInt64(serverConfig.Key));
if (guild == null) if (guild == null)
@ -35,10 +37,12 @@ namespace ChaosBot.Discord.Services
return; return;
} }
SocketChannel socketChannel = guild.GetChannel(Convert.ToUInt64(lodestoneChannelSloganDescriptionId)); SocketChannel socketChannel =
guild.GetChannel(Convert.ToUInt64(lodestoneChannelSloganDescriptionId));
if (socketChannel == null) if (socketChannel == null)
{ {
_logger.Warn($"Channel {Convert.ToUInt64(lodestoneChannelSloganDescriptionId)} not found in server {guild.Name}"); _logger.Warn(
$"Channel {Convert.ToUInt64(lodestoneChannelSloganDescriptionId)} not found in server {guild.Name}");
return; return;
} }
@ -47,13 +51,20 @@ namespace ChaosBot.Discord.Services
{ {
_logger.Warn($"Could not cast channel {socketChannel.Id} to ITextChannel"); _logger.Warn($"Could not cast channel {socketChannel.Id} to ITextChannel");
return; return;
}; }
string description = LodestoneManager.GetFreeCompanyById(serverConfig.GetValue<string>("Lodestone:FreeCompanyId")).FreeCompany.Slogan;; if (!guild.GetUser(_client.CurrentUser.Id).GetPermissions(channel).ManageChannel) return;
await channel.ModifyAsync(x =>
string description = LodestoneManager
.GetFreeCompanyById(serverConfig.GetValue<string>("Lodestone:FreeCompanyId")).FreeCompany
.Slogan;
await channel.ModifyAsync(x => { x.Topic = description; });
}
catch (Exception ex)
{ {
x.Topic = description; _logger.Error($"TimerHandler.UpdateChannelSloganDescription: Exception [{ex}] thrown, <[{ex.Message}]>.");
}); }
} }
Timer.RunTimer(UpdateChannelSloganDescription, new TimeSpan(TimeSpan.TicksPerMinute * refreshMinutes)); Timer.RunTimer(UpdateChannelSloganDescription, new TimeSpan(TimeSpan.TicksPerMinute * refreshMinutes));