NadekoBot/NadekoBot.Core/Modules/Administration/Services/VcRoleService.cs

120 lines
4.3 KiB
C#
Raw Normal View History

2017-07-17 19:42:36 +00:00
using System;
2017-05-27 08:19:27 +00:00
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
2017-07-17 19:42:36 +00:00
using Discord;
using Discord.WebSocket;
using NadekoBot.Core.Services;
using NadekoBot.Core.Services.Database.Models;
2017-07-17 19:42:36 +00:00
using NLog;
2017-05-27 08:19:27 +00:00
2017-07-17 19:42:36 +00:00
namespace NadekoBot.Modules.Administration.Services
2017-05-27 08:19:27 +00:00
{
public class VcRoleService : INService
2017-05-27 08:19:27 +00:00
{
private readonly Logger _log;
private readonly DbService _db;
private readonly DiscordSocketClient _client;
2017-05-27 08:19:27 +00:00
public ConcurrentDictionary<ulong, ConcurrentDictionary<ulong, IRole>> VcRoles { get; }
public VcRoleService(DiscordSocketClient client, NadekoBot bot, DbService db)
2017-05-27 08:19:27 +00:00
{
_log = LogManager.GetCurrentClassLogger();
_db = db;
_client = client;
2017-05-27 08:19:27 +00:00
_client.UserVoiceStateUpdated += ClientOnUserVoiceStateUpdated;
2017-05-27 08:19:27 +00:00
VcRoles = new ConcurrentDictionary<ulong, ConcurrentDictionary<ulong, IRole>>();
var missingRoles = new List<VcRoleInfo>();
foreach (var gconf in bot.AllGuildConfigs)
2017-05-27 08:19:27 +00:00
{
var g = _client.GetGuild(gconf.GuildId);
2017-05-27 08:19:27 +00:00
if (g == null)
continue;
2017-05-27 08:19:27 +00:00
var infos = new ConcurrentDictionary<ulong, IRole>();
VcRoles.TryAdd(gconf.GuildId, infos);
foreach (var ri in gconf.VcRoleInfos)
{
var role = g.GetRole(ri.RoleId);
if (role == null)
{
missingRoles.Add(ri);
continue;
}
2017-05-27 08:19:27 +00:00
infos.TryAdd(ri.VoiceChannelId, role);
}
}
if(missingRoles.Any())
using (var uow = _db.UnitOfWork)
{
_log.Warn($"Removing {missingRoles.Count} missing roles from {nameof(VcRoleService)}");
uow._context.RemoveRange(missingRoles);
uow.Complete();
}
2017-05-27 08:19:27 +00:00
}
private Task ClientOnUserVoiceStateUpdated(SocketUser usr, SocketVoiceState oldState,
SocketVoiceState newState)
{
var gusr = usr as SocketGuildUser;
if (gusr == null)
return Task.CompletedTask;
var oldVc = oldState.VoiceChannel;
var newVc = newState.VoiceChannel;
var _ = Task.Run(async () =>
{
try
{
if (oldVc != newVc)
{
ulong guildId;
guildId = newVc?.Guild.Id ?? oldVc.Guild.Id;
if (VcRoles.TryGetValue(guildId, out ConcurrentDictionary<ulong, IRole> guildVcRoles))
{
//remove old
if (oldVc != null && guildVcRoles.TryGetValue(oldVc.Id, out IRole role))
{
try
{
await gusr.RemoveRoleAsync(role).ConfigureAwait(false);
}
catch
2017-05-27 08:19:27 +00:00
{
try
{
await Task.Delay(500).ConfigureAwait(false);
await gusr.RemoveRoleAsync(role).ConfigureAwait(false);
}
catch { }
2017-05-27 08:19:27 +00:00
}
}
//add new
if (newVc != null && guildVcRoles.TryGetValue(newVc.Id, out role))
{
if (!gusr.Roles.Contains(role))
{
await Task.Delay(500).ConfigureAwait(false);
2017-05-27 08:19:27 +00:00
await gusr.AddRoleAsync(role).ConfigureAwait(false);
}
2017-05-27 08:19:27 +00:00
}
}
}
}
catch (Exception ex)
{
_log.Warn(ex);
}
});
return Task.CompletedTask;
}
}
}