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-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-05-27 17:42:23 +00:00
|
|
|
|
|
|
|
|
|
//guildid/roleid
|
|
|
|
|
public ConcurrentDictionary<ulong, ulong> AutoAssignedRoles { get; }
|
|
|
|
|
|
2017-10-13 00:21:39 +00:00
|
|
|
|
public AutoAssignRoleService(DiscordSocketClient client, NadekoBot bot)
|
2017-05-27 17:42:23 +00:00
|
|
|
|
{
|
|
|
|
|
_log = LogManager.GetCurrentClassLogger();
|
|
|
|
|
_client = client;
|
|
|
|
|
|
|
|
|
|
AutoAssignedRoles = new ConcurrentDictionary<ulong, ulong>(
|
2017-10-13 00:21:39 +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-06-05 22:46:58 +00:00
|
|
|
|
_client.UserJoined += (user) =>
|
2017-05-27 17:42:23 +00:00
|
|
|
|
{
|
2017-06-05 22:46:58 +00:00
|
|
|
|
var _ = Task.Run(async () =>
|
2017-05-27 17:42:23 +00:00
|
|
|
|
{
|
2017-06-05 22:46:58 +00:00
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
AutoAssignedRoles.TryGetValue(user.Guild.Id, out ulong roleId);
|
2017-05-27 17:42:23 +00:00
|
|
|
|
|
2017-06-05 22:46:58 +00:00
|
|
|
|
if (roleId == 0)
|
|
|
|
|
return;
|
2017-05-27 17:42:23 +00:00
|
|
|
|
|
2017-06-05 22:46:58 +00:00
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex) { _log.Warn(ex); }
|
|
|
|
|
});
|
|
|
|
|
return Task.CompletedTask;
|
2017-05-27 17:42:23 +00:00
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|