Add support for moderator list in GuildStateService

Completely forgot.
This commit is contained in:
Noikoio 2019-04-24 13:41:10 -07:00
parent 4de1e25230
commit 77b6aa891b
4 changed files with 47 additions and 7 deletions

View file

@ -1,4 +1,5 @@
using Discord.WebSocket; using Discord.WebSocket;
using Kerobot.Common;
using Newtonsoft.Json.Linq; using Newtonsoft.Json.Linq;
using System; using System;
using System.Diagnostics; using System.Diagnostics;
@ -134,6 +135,15 @@ namespace Kerobot
if (result == null) return new BanKickResult(null, false, true); if (result == null) return new BanKickResult(null, false, true);
return await KickAsync(guild, source, result.UserID, reason, dmMsg); return await KickAsync(guild, source, result.UserID, reason, dmMsg);
} }
/// <summary>
/// Returns the list of moderators defined in the current guild configuration.
/// </summary>
/// <returns>
/// An <see cref="EntityList"/> with corresponding moderator configuration data.
/// In case none exists, an empty list will be returned.
/// </returns>
protected EntityList GetModerators(ulong guild) => Kerobot.GetModerators(guild);
} }
/// <summary> /// <summary>

View file

@ -2,6 +2,7 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.Threading.Tasks; using System.Threading.Tasks;
using Discord.WebSocket; using Discord.WebSocket;
using Kerobot.Common;
using Newtonsoft.Json; using Newtonsoft.Json;
using Newtonsoft.Json.Linq; using Newtonsoft.Json.Linq;
@ -14,13 +15,14 @@ namespace Kerobot.Services.GuildState
class GuildStateService : Service class GuildStateService : Service
{ {
private readonly object _storageLock = new object(); private readonly object _storageLock = new object();
private readonly Dictionary<ulong, Dictionary<Type, StateInfo>> _storage; private readonly Dictionary<ulong, EntityList> _moderators;
private readonly Dictionary<ulong, Dictionary<Type, StateInfo>> _states;
const string GuildLogSource = "Configuration loader"; const string GuildLogSource = "Configuration loader";
public GuildStateService(Kerobot kb) : base(kb) public GuildStateService(Kerobot kb) : base(kb)
{ {
_storage = new Dictionary<ulong, Dictionary<Type, StateInfo>>(); _states = new Dictionary<ulong, Dictionary<Type, StateInfo>>();
CreateDatabaseTablesAsync().Wait(); CreateDatabaseTablesAsync().Wait();
kb.DiscordClient.GuildAvailable += DiscordClient_GuildAvailable; kb.DiscordClient.GuildAvailable += DiscordClient_GuildAvailable;
@ -39,7 +41,7 @@ namespace Kerobot.Services.GuildState
private Task DiscordClient_LeftGuild(SocketGuild arg) private Task DiscordClient_LeftGuild(SocketGuild arg)
{ {
// TODO what is GuildUnavailable? Should we listen for that too? // TODO what is GuildUnavailable? Should we listen for that too?
lock (_storageLock) _storage.Remove(arg.Id); lock (_storageLock) _states.Remove(arg.Id);
return Task.CompletedTask; return Task.CompletedTask;
} }
@ -62,6 +64,7 @@ namespace Kerobot.Services.GuildState
} }
} }
#region Data output
/// <summary> /// <summary>
/// See <see cref="ModuleBase.GetGuildState{T}(ulong)"/>. /// See <see cref="ModuleBase.GetGuildState{T}(ulong)"/>.
/// </summary> /// </summary>
@ -69,7 +72,7 @@ namespace Kerobot.Services.GuildState
{ {
lock (_storageLock) lock (_storageLock)
{ {
if (_storage.TryGetValue(guildId, out var tl)) if (_states.TryGetValue(guildId, out var tl))
{ {
if (tl.TryGetValue(t, out var val)) if (tl.TryGetValue(t, out var val))
{ {
@ -81,6 +84,19 @@ namespace Kerobot.Services.GuildState
} }
} }
/// <summary>
/// See <see cref="ModuleBase.GetModerators(ulong)"/>.
/// </summary>
public EntityList RetrieveGuildModerators(ulong guildId)
{
lock (_storageLock)
{
if (_moderators.TryGetValue(guildId, out var mods)) return mods;
else return new EntityList();
}
}
#endregion
/// <summary> /// <summary>
/// Guild-specific configuration begins processing here. /// Guild-specific configuration begins processing here.
/// Configuration is loaded from database, and appropriate sections dispatched to their /// Configuration is loaded from database, and appropriate sections dispatched to their
@ -120,6 +136,10 @@ namespace Kerobot.Services.GuildState
// TODO Guild-specific service options? If implemented, this is where to load them. // TODO Guild-specific service options? If implemented, this is where to load them.
// Load moderator list
var mods = new EntityList(guildConf["Moderators"], true);
// Create guild state objects for all existing modules
var newStates = new Dictionary<Type, StateInfo>(); var newStates = new Dictionary<Type, StateInfo>();
foreach (var mod in Kerobot.Modules) foreach (var mod in Kerobot.Modules)
{ {
@ -152,7 +172,11 @@ namespace Kerobot.Services.GuildState
return false; return false;
} }
} }
lock (_storageLock) _storage[guildId] = newStates; lock (_storageLock)
{
_moderators[guildId] = mods;
_states[guildId] = newStates;
}
return true; return true;
} }

View file

@ -1,4 +1,5 @@
using Kerobot.Services.GuildState; using Kerobot.Common;
using Kerobot.Services.GuildState;
using System; using System;
namespace Kerobot namespace Kerobot
@ -12,5 +13,10 @@ namespace Kerobot
/// </summary> /// </summary>
internal T GetGuildState<T>(ulong guild, Type type) internal T GetGuildState<T>(ulong guild, Type type)
=> _svcGuildState.RetrieveGuildStateObject<T>(guild, type); => _svcGuildState.RetrieveGuildStateObject<T>(guild, type);
/// <summary>
/// See <see cref="ModuleBase.GetModerators(ulong)"/>.
/// </summary>
internal EntityList GetModerators(ulong guild) => _svcGuildState.RetrieveGuildModerators(guild);
} }
} }

View file

@ -4,7 +4,7 @@ using System;
namespace Kerobot.Services.GuildState namespace Kerobot.Services.GuildState
{ {
/// <summary> /// <summary>
/// Contains the guild state object and other useful metadata in regards to it. /// Contains a guild state object and other useful metadata in regards to it.
/// </summary> /// </summary>
class StateInfo : IDisposable class StateInfo : IDisposable
{ {