Add exception handling when making new guild state
This commit is contained in:
parent
ee94cfd961
commit
749ba169c0
1 changed files with 38 additions and 11 deletions
|
@ -1,4 +1,5 @@
|
||||||
using Newtonsoft.Json;
|
using Discord.WebSocket;
|
||||||
|
using Newtonsoft.Json;
|
||||||
using Newtonsoft.Json.Linq;
|
using Newtonsoft.Json.Linq;
|
||||||
using Npgsql;
|
using Npgsql;
|
||||||
using System;
|
using System;
|
||||||
|
@ -27,12 +28,29 @@ namespace Kerobot.Services.GuildState
|
||||||
_defaultGuildJson = PreloadDefaultGuildJson();
|
_defaultGuildJson = PreloadDefaultGuildJson();
|
||||||
|
|
||||||
kb.DiscordClient.GuildAvailable += DiscordClient_GuildAvailable;
|
kb.DiscordClient.GuildAvailable += DiscordClient_GuildAvailable;
|
||||||
|
kb.DiscordClient.JoinedGuild += DiscordClient_JoinedGuild;
|
||||||
kb.DiscordClient.LeftGuild += DiscordClient_LeftGuild;
|
kb.DiscordClient.LeftGuild += DiscordClient_LeftGuild;
|
||||||
|
|
||||||
// TODO periodic task for refreshing stale configuration
|
// TODO periodic task for refreshing stale configuration
|
||||||
}
|
}
|
||||||
|
|
||||||
private async Task DiscordClient_GuildAvailable(Discord.WebSocket.SocketGuild arg)
|
private async Task DiscordClient_GuildAvailable(SocketGuild arg) => await InitializeGuild(arg);
|
||||||
|
private async Task DiscordClient_JoinedGuild(SocketGuild arg) => await InitializeGuild(arg);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Unloads in-memory guild information.
|
||||||
|
/// </summary>
|
||||||
|
private Task DiscordClient_LeftGuild(SocketGuild arg)
|
||||||
|
{
|
||||||
|
// TODO what is GuildUnavailable? Should we listen for that too?
|
||||||
|
lock (_storageLock) _storage.Remove(arg.Id);
|
||||||
|
return Task.CompletedTask;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Initializes guild in-memory and database structures, then attempts to load configuration.
|
||||||
|
/// </summary>
|
||||||
|
private async Task InitializeGuild(SocketGuild arg)
|
||||||
{
|
{
|
||||||
// Get this done before any other thing.
|
// Get this done before any other thing.
|
||||||
await CreateSchema(arg.Id);
|
await CreateSchema(arg.Id);
|
||||||
|
@ -49,7 +67,7 @@ namespace Kerobot.Services.GuildState
|
||||||
catch (NpgsqlException ex)
|
catch (NpgsqlException ex)
|
||||||
{
|
{
|
||||||
await Log("Database error on CreateDatabaseTablesAsync:\n"
|
await Log("Database error on CreateDatabaseTablesAsync:\n"
|
||||||
+ $"-- Service: {svc.Name}\n-- Guild: {arg.Id}\n-- Error: {ex.Message}" , true);
|
+ $"-- Service: {svc.Name}\n-- Guild: {arg.Id}\n-- Error: {ex.Message}", true);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -67,12 +85,6 @@ namespace Kerobot.Services.GuildState
|
||||||
$"Configuration successfully refreshed for guild ID {arg.Id}.");
|
$"Configuration successfully refreshed for guild ID {arg.Id}.");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
private Task DiscordClient_LeftGuild(Discord.WebSocket.SocketGuild arg)
|
|
||||||
{
|
|
||||||
// Unload guild information.
|
|
||||||
lock (_storageLock) _storage.Remove(arg.Id);
|
|
||||||
return Task.CompletedTask;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// See <see cref="ModuleBase.GetGuildState{T}(ulong)"/>.
|
/// See <see cref="ModuleBase.GetGuildState{T}(ulong)"/>.
|
||||||
|
@ -138,7 +150,22 @@ namespace Kerobot.Services.GuildState
|
||||||
var tn = t.Name;
|
var tn = t.Name;
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
var state = await mod.CreateGuildStateAsync(guildConf[tn]); // can be null
|
object state;
|
||||||
|
try
|
||||||
|
{
|
||||||
|
state = await mod.CreateGuildStateAsync(guildConf[tn]); // can be null
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
Log("Encountered unhandled exception during guild state initialization:\n" +
|
||||||
|
$"Module: {tn}\n" +
|
||||||
|
$"Guild: {guildId} ({Kerobot.DiscordClient.GetGuild(guildId)?.Name ?? "unknown name"})\n" +
|
||||||
|
$"```\n{ex.ToString()}\n```", true).Wait();
|
||||||
|
Kerobot.GuildLogAsync(guildId, GuildLogSource,
|
||||||
|
"An internal error occurred when attempting to load new configuration. " +
|
||||||
|
"The bot owner has been notified.").Wait();
|
||||||
|
return false;
|
||||||
|
}
|
||||||
newStates.Add(t, new StateInfo(state, jstrHash));
|
newStates.Add(t, new StateInfo(state, jstrHash));
|
||||||
}
|
}
|
||||||
catch (ModuleLoadException ex)
|
catch (ModuleLoadException ex)
|
||||||
|
|
Loading…
Reference in a new issue