RegexBot/RegexBot-Modules/VoiceRoleSync/ModuleConfig.cs
Noi 08afc224ed Port VoiceRoleSync from legacy
Since it was recently rewritten, the code was copied over nearly as-is from
the legacy branch without any other major changes considered.
2022-07-05 21:53:00 -07:00

42 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 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;
}
}
}