RegexBot/Modules/VoiceRoleSync/ModuleConfig.cs
Noi fc88ab5cf9 Update modules and module loading
Changed accessibility of included modules to 'internal'
to suppress documentation warnings.
Additionally, increase logging feedback as configuration is loaded.
2022-07-20 20:35:26 -07:00

44 lines
1.6 KiB
C#

using System.Collections.ObjectModel;
namespace RegexBot.Modules.VoiceRoleSync;
/// <summary>
/// Dictionary wrapper. Key = voice channel ID, Value = role.
/// </summary>
class ModuleConfig {
private readonly ReadOnlyDictionary<ulong, ulong> _values;
public int Count { get => _values.Count; }
public ModuleConfig(JObject config) {
// Configuration format is expected to be an object that contains other objects.
// The objects themselves should have their name be the voice channel,
// and the value be the role to be applied.
// TODO Make it accept names; currently only accepts ulongs
var values = new Dictionary<ulong, ulong>();
foreach (var item in config.Properties()) {
if (!ulong.TryParse(item.Name, out var voice)) throw new ModuleLoadException($"{item.Name} is not a voice channel ID.");
var valstr = item.Value.Value<string>();
if (!ulong.TryParse(valstr, out var role)) throw new ModuleLoadException($"{valstr} is not a role ID.");
values[voice] = role;
}
_values = new ReadOnlyDictionary<ulong, ulong>(values);
}
public SocketRole? GetAssociatedRoleFor(SocketVoiceChannel voiceChannel) {
if (voiceChannel == null) return null;
if (_values.TryGetValue(voiceChannel.Id, out var roleId)) return voiceChannel.Guild.GetRole(roleId);
return null;
}
public IEnumerable<SocketRole> GetTrackedRoles(SocketGuild guild) {
foreach (var pair in _values) {
var r = guild.GetRole(pair.Value);
if (r != null) yield return r;
}
}
}