2022-07-29 02:05:24 +00:00
|
|
|
using RegexBot.Common;
|
2022-07-06 04:51:44 +00:00
|
|
|
using System.Collections.ObjectModel;
|
|
|
|
|
|
|
|
namespace RegexBot.Modules.VoiceRoleSync;
|
|
|
|
class ModuleConfig {
|
2022-07-29 02:05:24 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Key = voice channel ID, Value = role ID.
|
|
|
|
/// </summary>
|
2022-07-06 04:51:44 +00:00
|
|
|
private readonly ReadOnlyDictionary<ulong, ulong> _values;
|
|
|
|
|
2022-07-21 03:34:29 +00:00
|
|
|
public int Count { get => _values.Count; }
|
|
|
|
|
2022-07-29 02:05:24 +00:00
|
|
|
public ModuleConfig(JObject config, SocketGuild g) {
|
|
|
|
// Configuration: Object with properties.
|
|
|
|
// Property name is a role entity name
|
|
|
|
// Value is a string or array of voice channel IDs.
|
2022-07-06 04:51:44 +00:00
|
|
|
var values = new Dictionary<ulong, ulong>();
|
|
|
|
|
|
|
|
foreach (var item in config.Properties()) {
|
2022-08-23 02:25:48 +00:00
|
|
|
EntityName name;
|
|
|
|
try {
|
|
|
|
name = new EntityName(item.Name, EntityType.Role);
|
|
|
|
} catch (FormatException) {
|
|
|
|
throw new ModuleLoadException($"'{item.Name}' is not specified as a role.");
|
|
|
|
}
|
2024-06-09 04:07:35 +00:00
|
|
|
var role = name.FindRoleIn(g) ?? throw new ModuleLoadException($"Unable to find role '{name}'.");
|
2022-07-29 02:05:24 +00:00
|
|
|
var channels = Utilities.LoadStringOrStringArray(item.Value);
|
|
|
|
if (channels.Count == 0) throw new ModuleLoadException($"One or more channels must be defined under '{name}'.");
|
|
|
|
foreach (var id in channels) {
|
|
|
|
if (!ulong.TryParse(id, out var channelId)) throw new ModuleLoadException("Voice channel IDs must be numeric.");
|
|
|
|
if (values.ContainsKey(channelId)) throw new ModuleLoadException($"'{channelId}' cannot be specified more than once.");
|
|
|
|
values.Add(channelId, role.Id);
|
|
|
|
}
|
2022-07-06 04:51:44 +00:00
|
|
|
}
|
2022-07-29 02:05:24 +00:00
|
|
|
_values = new(values);
|
2022-07-06 04:51:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
2022-07-29 02:05:24 +00:00
|
|
|
var roles = _values.Select(v => v.Value).Distinct();
|
|
|
|
foreach (var id in roles) {
|
|
|
|
var r = guild.GetRole(id);
|
2022-07-06 04:51:44 +00:00
|
|
|
if (r != null) yield return r;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|