2017-07-17 19:42:36 +00:00
|
|
|
|
using System;
|
2017-05-27 17:42:23 +00:00
|
|
|
|
using System.Collections.Concurrent;
|
|
|
|
|
using System.Linq;
|
2017-06-05 22:46:58 +00:00
|
|
|
|
using System.Threading.Tasks;
|
2017-07-17 19:42:36 +00:00
|
|
|
|
using Discord.WebSocket;
|
2017-10-13 04:14:54 +00:00
|
|
|
|
using NadekoBot.Core.Services;
|
2017-07-17 19:42:36 +00:00
|
|
|
|
using NLog;
|
2017-10-30 13:38:50 +00:00
|
|
|
|
using Discord;
|
2017-05-27 17:42:23 +00:00
|
|
|
|
|
2017-07-17 19:42:36 +00:00
|
|
|
|
namespace NadekoBot.Modules.Administration.Services
|
2017-05-27 17:42:23 +00:00
|
|
|
|
{
|
2017-07-15 03:04:16 +00:00
|
|
|
|
public class AutoAssignRoleService : INService
|
2017-05-27 17:42:23 +00:00
|
|
|
|
{
|
|
|
|
|
private readonly Logger _log;
|
2017-06-19 13:42:10 +00:00
|
|
|
|
private readonly DiscordSocketClient _client;
|
2017-10-30 13:38:50 +00:00
|
|
|
|
private readonly DbService _db;
|
2017-05-27 17:42:23 +00:00
|
|
|
|
|
|
|
|
|
//guildid/roleid
|
|
|
|
|
public ConcurrentDictionary<ulong, ulong> AutoAssignedRoles { get; }
|
2017-10-30 13:38:50 +00:00
|
|
|
|
public BlockingCollection<(IGuildUser, ulong)> AutoAssignQueue { get; } = new BlockingCollection<(IGuildUser, ulong)>();
|
2017-05-27 17:42:23 +00:00
|
|
|
|
|
2017-10-30 13:38:50 +00:00
|
|
|
|
public AutoAssignRoleService(DiscordSocketClient client, NadekoBot bot, DbService db)
|
2017-05-27 17:42:23 +00:00
|
|
|
|
{
|
|
|
|
|
_log = LogManager.GetCurrentClassLogger();
|
|
|
|
|
_client = client;
|
2017-10-30 13:38:50 +00:00
|
|
|
|
_db = db;
|
2017-05-27 17:42:23 +00:00
|
|
|
|
|
|
|
|
|
AutoAssignedRoles = new ConcurrentDictionary<ulong, ulong>(
|
2017-10-30 13:38:50 +00:00
|
|
|
|
bot.AllGuildConfigs
|
|
|
|
|
.Where(x => x.AutoAssignRoleId != 0)
|
2017-05-27 17:42:23 +00:00
|
|
|
|
.ToDictionary(k => k.GuildId, v => v.AutoAssignRoleId));
|
|
|
|
|
|
2017-10-30 13:38:50 +00:00
|
|
|
|
var _queueRunner = Task.Run(async () =>
|
2017-05-27 17:42:23 +00:00
|
|
|
|
{
|
2017-10-30 13:38:50 +00:00
|
|
|
|
while (true)
|
2017-05-27 17:42:23 +00:00
|
|
|
|
{
|
2017-10-30 13:38:50 +00:00
|
|
|
|
var (user, roleId) = AutoAssignQueue.Take();
|
2017-06-05 22:46:58 +00:00
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var role = user.Guild.Roles.FirstOrDefault(r => r.Id == roleId);
|
2017-05-27 17:42:23 +00:00
|
|
|
|
|
2017-06-05 22:46:58 +00:00
|
|
|
|
if (role != null)
|
|
|
|
|
await user.AddRoleAsync(role).ConfigureAwait(false);
|
2017-10-30 13:38:50 +00:00
|
|
|
|
else
|
|
|
|
|
{
|
2017-10-30 14:14:41 +00:00
|
|
|
|
_log.Warn($"Disabled 'Auto assign role' feature on {0} server the role doesn't exist.",
|
2017-10-30 13:38:50 +00:00
|
|
|
|
roleId);
|
|
|
|
|
DisableAar(user.GuildId);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (Discord.Net.HttpException ex) when (ex.HttpCode == System.Net.HttpStatusCode.Forbidden)
|
|
|
|
|
{
|
2017-10-30 14:14:41 +00:00
|
|
|
|
_log.Warn($"Disabled 'Auto assign role' feature on {0} server because I don't have role management permissions.",
|
2017-10-30 13:38:50 +00:00
|
|
|
|
roleId);
|
|
|
|
|
DisableAar(user.GuildId);
|
2017-06-05 22:46:58 +00:00
|
|
|
|
}
|
2017-10-30 13:38:50 +00:00
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
_log.Warn(ex);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
_client.UserJoined += (user) =>
|
|
|
|
|
{
|
|
|
|
|
if (AutoAssignedRoles.TryGetValue(user.Guild.Id, out ulong roleId)
|
|
|
|
|
&& roleId != 0)
|
|
|
|
|
{
|
|
|
|
|
AutoAssignQueue.Add((user, roleId));
|
|
|
|
|
}
|
2017-06-05 22:46:58 +00:00
|
|
|
|
return Task.CompletedTask;
|
2017-05-27 17:42:23 +00:00
|
|
|
|
};
|
|
|
|
|
}
|
2017-10-30 13:38:50 +00:00
|
|
|
|
|
|
|
|
|
public void EnableAar(ulong guildId, ulong roleId)
|
|
|
|
|
{
|
|
|
|
|
using (var uow = _db.UnitOfWork)
|
|
|
|
|
{
|
|
|
|
|
var gc = uow.GuildConfigs.For(guildId, set => set);
|
|
|
|
|
gc.AutoAssignRoleId = roleId;
|
|
|
|
|
uow.Complete();
|
|
|
|
|
}
|
|
|
|
|
AutoAssignedRoles.AddOrUpdate(guildId,
|
|
|
|
|
roleId,
|
|
|
|
|
delegate { return roleId; });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void DisableAar(ulong guildId)
|
|
|
|
|
{
|
|
|
|
|
using (var uow = _db.UnitOfWork)
|
|
|
|
|
{
|
|
|
|
|
var gc = uow.GuildConfigs.For(guildId, set => set);
|
|
|
|
|
gc.AutoAssignRoleId = 0;
|
|
|
|
|
uow.Complete();
|
|
|
|
|
}
|
|
|
|
|
AutoAssignedRoles.TryRemove(guildId, out _);
|
|
|
|
|
}
|
2017-05-27 17:42:23 +00:00
|
|
|
|
}
|
|
|
|
|
}
|