2017-07-14 03:00:30 +00:00
|
|
|
|
using System;
|
2017-07-17 19:42:36 +00:00
|
|
|
|
using System.Collections.Concurrent;
|
2017-07-14 03:00:30 +00:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using Discord;
|
|
|
|
|
using Discord.WebSocket;
|
2017-07-17 19:42:36 +00:00
|
|
|
|
using NadekoBot.Extensions;
|
2017-10-13 04:14:54 +00:00
|
|
|
|
using NadekoBot.Core.Services;
|
|
|
|
|
using NadekoBot.Core.Services.Database.Models;
|
2017-07-14 03:00:30 +00:00
|
|
|
|
using NLog;
|
2017-07-19 08:38:14 +00:00
|
|
|
|
using NadekoBot.Modules.Utility.Extensions;
|
|
|
|
|
using NadekoBot.Common.TypeReaders;
|
|
|
|
|
using NadekoBot.Modules.Utility.Common;
|
|
|
|
|
using NadekoBot.Modules.Utility.Common.Exceptions;
|
|
|
|
|
using Discord.Net;
|
2017-07-14 03:00:30 +00:00
|
|
|
|
|
2017-07-17 19:42:36 +00:00
|
|
|
|
namespace NadekoBot.Modules.Utility.Services
|
2017-07-14 03:00:30 +00:00
|
|
|
|
{
|
2017-10-04 22:51:12 +00:00
|
|
|
|
public class StreamRoleService : INService, IUnloadableService
|
2017-07-14 03:00:30 +00:00
|
|
|
|
{
|
|
|
|
|
private readonly DbService _db;
|
2017-10-04 22:51:12 +00:00
|
|
|
|
private readonly DiscordSocketClient _client;
|
2017-07-14 03:00:30 +00:00
|
|
|
|
private readonly ConcurrentDictionary<ulong, StreamRoleSettings> guildSettings;
|
|
|
|
|
private readonly Logger _log;
|
|
|
|
|
|
2017-10-13 00:21:39 +00:00
|
|
|
|
public StreamRoleService(DiscordSocketClient client, DbService db, NadekoBot bot)
|
2017-07-14 03:00:30 +00:00
|
|
|
|
{
|
|
|
|
|
this._log = LogManager.GetCurrentClassLogger();
|
2017-10-04 22:51:12 +00:00
|
|
|
|
this._db = db;
|
|
|
|
|
this._client = client;
|
2017-07-14 03:00:30 +00:00
|
|
|
|
|
2017-10-13 00:21:39 +00:00
|
|
|
|
guildSettings = bot.AllGuildConfigs
|
|
|
|
|
.ToDictionary(x => x.GuildId, x => x.StreamRole)
|
2017-07-19 08:38:14 +00:00
|
|
|
|
.Where(x => x.Value != null && x.Value.Enabled)
|
|
|
|
|
.ToConcurrent();
|
2017-07-14 03:00:30 +00:00
|
|
|
|
|
2017-10-04 22:51:12 +00:00
|
|
|
|
_client.GuildMemberUpdated += Client_GuildMemberUpdated;
|
2017-07-14 03:00:30 +00:00
|
|
|
|
|
|
|
|
|
var _ = Task.Run(async () =>
|
|
|
|
|
{
|
2017-07-21 04:56:21 +00:00
|
|
|
|
try
|
2017-07-14 03:00:30 +00:00
|
|
|
|
{
|
2017-07-21 04:56:21 +00:00
|
|
|
|
await Task.WhenAll(client.Guilds.Select(g => RescanUsers(g))).ConfigureAwait(false);
|
2017-07-14 03:00:30 +00:00
|
|
|
|
}
|
2017-07-21 04:56:21 +00:00
|
|
|
|
catch
|
2017-07-14 03:00:30 +00:00
|
|
|
|
{
|
2017-07-21 04:56:21 +00:00
|
|
|
|
// ignored
|
2017-07-19 08:38:14 +00:00
|
|
|
|
}
|
2017-07-14 03:00:30 +00:00
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-04 22:51:12 +00:00
|
|
|
|
public Task Unload()
|
|
|
|
|
{
|
|
|
|
|
_client.GuildMemberUpdated -= Client_GuildMemberUpdated;
|
|
|
|
|
return Task.CompletedTask;
|
|
|
|
|
}
|
|
|
|
|
|
2017-07-21 04:56:21 +00:00
|
|
|
|
private Task Client_GuildMemberUpdated(SocketGuildUser before, SocketGuildUser after)
|
2017-07-14 03:00:30 +00:00
|
|
|
|
{
|
2017-07-21 04:56:21 +00:00
|
|
|
|
var _ = Task.Run(async () =>
|
2017-07-19 08:38:14 +00:00
|
|
|
|
{
|
2017-07-21 04:56:21 +00:00
|
|
|
|
//if user wasn't streaming or didn't have a game status at all
|
|
|
|
|
if (guildSettings.TryGetValue(after.Guild.Id, out var setting))
|
2017-07-19 08:38:14 +00:00
|
|
|
|
{
|
2017-07-21 04:56:21 +00:00
|
|
|
|
await RescanUser(after, setting).ConfigureAwait(false);
|
2017-07-19 08:38:14 +00:00
|
|
|
|
}
|
2017-07-21 04:56:21 +00:00
|
|
|
|
});
|
2017-07-19 08:38:14 +00:00
|
|
|
|
|
2017-07-21 04:56:21 +00:00
|
|
|
|
return Task.CompletedTask;
|
|
|
|
|
}
|
2017-07-19 08:38:14 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Adds or removes a user from a blacklist or a whitelist in the specified guild.
|
|
|
|
|
/// </summary>
|
2017-07-21 04:56:21 +00:00
|
|
|
|
/// <param name="guild">Guild</param>
|
2017-07-19 08:38:14 +00:00
|
|
|
|
/// <param name="action">Add or rem action</param>
|
|
|
|
|
/// <param name="userId">User's Id</param>
|
|
|
|
|
/// <param name="userName">User's name#discrim</param>
|
|
|
|
|
/// <returns>Whether the operation was successful</returns>
|
2017-07-21 04:56:21 +00:00
|
|
|
|
public async Task<bool> ApplyListAction(StreamRoleListType listType, IGuild guild, AddRemove action, ulong userId, string userName)
|
2017-07-19 08:38:14 +00:00
|
|
|
|
{
|
|
|
|
|
userName.ThrowIfNull(nameof(userName));
|
|
|
|
|
|
|
|
|
|
bool success;
|
2017-07-14 03:00:30 +00:00
|
|
|
|
using (var uow = _db.UnitOfWork)
|
|
|
|
|
{
|
2017-07-21 04:56:21 +00:00
|
|
|
|
var streamRoleSettings = uow.GuildConfigs.GetStreamRoleSettings(guild.Id);
|
2017-07-14 03:00:30 +00:00
|
|
|
|
|
2017-07-19 08:38:14 +00:00
|
|
|
|
if (listType == StreamRoleListType.Whitelist)
|
|
|
|
|
{
|
|
|
|
|
var userObj = new StreamRoleWhitelistedUser()
|
2017-07-14 03:00:30 +00:00
|
|
|
|
{
|
2017-07-19 08:38:14 +00:00
|
|
|
|
UserId = userId,
|
|
|
|
|
Username = userName,
|
2017-07-14 03:00:30 +00:00
|
|
|
|
};
|
2017-07-19 08:38:14 +00:00
|
|
|
|
|
|
|
|
|
if (action == AddRemove.Rem)
|
|
|
|
|
success = streamRoleSettings.Whitelist.Remove(userObj);
|
|
|
|
|
else
|
|
|
|
|
success = streamRoleSettings.Whitelist.Add(userObj);
|
|
|
|
|
}
|
2017-07-14 03:00:30 +00:00
|
|
|
|
else
|
|
|
|
|
{
|
2017-07-19 08:38:14 +00:00
|
|
|
|
var userObj = new StreamRoleBlacklistedUser()
|
|
|
|
|
{
|
|
|
|
|
UserId = userId,
|
|
|
|
|
Username = userName,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
if (action == AddRemove.Rem)
|
|
|
|
|
success = streamRoleSettings.Blacklist.Remove(userObj);
|
|
|
|
|
else
|
|
|
|
|
success = streamRoleSettings.Blacklist.Add(userObj);
|
2017-07-14 03:00:30 +00:00
|
|
|
|
}
|
2017-07-19 08:38:14 +00:00
|
|
|
|
|
|
|
|
|
await uow.CompleteAsync().ConfigureAwait(false);
|
2017-07-21 05:10:17 +00:00
|
|
|
|
UpdateCache(guild.Id, streamRoleSettings);
|
2017-07-19 08:38:14 +00:00
|
|
|
|
}
|
2017-07-21 04:56:21 +00:00
|
|
|
|
if (success)
|
|
|
|
|
{
|
|
|
|
|
await RescanUsers(guild).ConfigureAwait(false);
|
|
|
|
|
}
|
2017-07-19 08:38:14 +00:00
|
|
|
|
return success;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Sets keyword on a guild and updates the cache.
|
|
|
|
|
/// </summary>
|
2017-07-21 04:56:21 +00:00
|
|
|
|
/// <param name="guild">Guild Id</param>
|
2017-07-19 08:38:14 +00:00
|
|
|
|
/// <param name="keyword">Keyword to set</param>
|
|
|
|
|
/// <returns>The keyword set</returns>
|
2017-07-21 04:56:21 +00:00
|
|
|
|
public async Task<string> SetKeyword(IGuild guild, string keyword)
|
2017-07-19 08:38:14 +00:00
|
|
|
|
{
|
|
|
|
|
keyword = keyword?.Trim()?.ToLowerInvariant();
|
|
|
|
|
|
|
|
|
|
using (var uow = _db.UnitOfWork)
|
|
|
|
|
{
|
2017-07-21 04:56:21 +00:00
|
|
|
|
var streamRoleSettings = uow.GuildConfigs.GetStreamRoleSettings(guild.Id);
|
2017-07-19 08:38:14 +00:00
|
|
|
|
|
|
|
|
|
streamRoleSettings.Keyword = keyword;
|
2017-07-21 04:56:21 +00:00
|
|
|
|
UpdateCache(guild.Id, streamRoleSettings);
|
2017-07-14 03:00:30 +00:00
|
|
|
|
uow.Complete();
|
2017-07-19 08:38:14 +00:00
|
|
|
|
}
|
|
|
|
|
|
2017-07-21 04:56:21 +00:00
|
|
|
|
await RescanUsers(guild).ConfigureAwait(false);
|
|
|
|
|
return keyword;
|
2017-07-19 08:38:14 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the currently set keyword on a guild.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="guildId">Guild Id</param>
|
|
|
|
|
/// <returns>The keyword set</returns>
|
|
|
|
|
public string GetKeyword(ulong guildId)
|
|
|
|
|
{
|
|
|
|
|
if (guildSettings.TryGetValue(guildId, out var outSetting))
|
|
|
|
|
return outSetting.Keyword;
|
|
|
|
|
|
|
|
|
|
StreamRoleSettings setting;
|
|
|
|
|
using (var uow = _db.UnitOfWork)
|
|
|
|
|
{
|
|
|
|
|
setting = uow.GuildConfigs.GetStreamRoleSettings(guildId);
|
2017-07-14 03:00:30 +00:00
|
|
|
|
}
|
|
|
|
|
|
2017-07-19 08:38:14 +00:00
|
|
|
|
UpdateCache(guildId, setting);
|
|
|
|
|
|
|
|
|
|
return setting.Keyword;
|
2017-07-14 03:00:30 +00:00
|
|
|
|
}
|
|
|
|
|
|
2017-07-19 08:38:14 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Sets the role to monitor, and a role to which to add to
|
|
|
|
|
/// the user who starts streaming in the monitored role.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="fromRole">Role to monitor</param>
|
|
|
|
|
/// <param name="addRole">Role to add to the user</param>
|
|
|
|
|
public async Task SetStreamRole(IRole fromRole, IRole addRole)
|
|
|
|
|
{
|
|
|
|
|
fromRole.ThrowIfNull(nameof(fromRole));
|
|
|
|
|
addRole.ThrowIfNull(nameof(addRole));
|
|
|
|
|
|
|
|
|
|
StreamRoleSettings setting;
|
|
|
|
|
using (var uow = _db.UnitOfWork)
|
|
|
|
|
{
|
|
|
|
|
var streamRoleSettings = uow.GuildConfigs.GetStreamRoleSettings(fromRole.Guild.Id);
|
|
|
|
|
|
|
|
|
|
streamRoleSettings.Enabled = true;
|
|
|
|
|
streamRoleSettings.AddRoleId = addRole.Id;
|
|
|
|
|
streamRoleSettings.FromRoleId = fromRole.Id;
|
|
|
|
|
|
|
|
|
|
setting = streamRoleSettings;
|
|
|
|
|
await uow.CompleteAsync().ConfigureAwait(false);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
UpdateCache(fromRole.Guild.Id, setting);
|
|
|
|
|
|
2017-07-21 04:56:21 +00:00
|
|
|
|
foreach (var usr in await fromRole.GetMembersAsync())
|
2017-07-19 08:38:14 +00:00
|
|
|
|
{
|
2017-07-21 04:56:21 +00:00
|
|
|
|
if (usr is IGuildUser x)
|
|
|
|
|
await RescanUser(x, setting, addRole).ConfigureAwait(false);
|
2017-07-19 08:38:14 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Stops the stream role feature on the specified guild.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="guildId">Guild's Id</param>
|
2017-07-21 04:56:21 +00:00
|
|
|
|
public async Task StopStreamRole(IGuild guild, bool cleanup = false)
|
2017-07-14 03:00:30 +00:00
|
|
|
|
{
|
|
|
|
|
using (var uow = _db.UnitOfWork)
|
|
|
|
|
{
|
2017-07-21 04:56:21 +00:00
|
|
|
|
var streamRoleSettings = uow.GuildConfigs.GetStreamRoleSettings(guild.Id);
|
2017-07-19 08:38:14 +00:00
|
|
|
|
streamRoleSettings.Enabled = false;
|
2017-07-21 04:56:21 +00:00
|
|
|
|
await uow.CompleteAsync().ConfigureAwait(false);
|
2017-07-14 03:00:30 +00:00
|
|
|
|
}
|
|
|
|
|
|
2017-07-21 04:56:21 +00:00
|
|
|
|
if (guildSettings.TryRemove(guild.Id, out var setting) && cleanup)
|
|
|
|
|
await RescanUsers(guild).ConfigureAwait(false);
|
|
|
|
|
}
|
2017-09-10 01:52:34 +00:00
|
|
|
|
|
2017-07-21 04:56:21 +00:00
|
|
|
|
private async Task RescanUser(IGuildUser user, StreamRoleSettings setting, IRole addRole = null)
|
|
|
|
|
{
|
|
|
|
|
if (user.Game.HasValue &&
|
|
|
|
|
user.Game.Value.StreamType != StreamType.NotStreaming
|
|
|
|
|
&& setting.Enabled
|
|
|
|
|
&& !setting.Blacklist.Any(x => x.UserId == user.Id)
|
|
|
|
|
&& user.RoleIds.Contains(setting.FromRoleId)
|
|
|
|
|
&& (string.IsNullOrWhiteSpace(setting.Keyword)
|
2017-07-21 05:10:17 +00:00
|
|
|
|
|| user.Game.Value.Name.ToLowerInvariant().Contains(setting.Keyword.ToLowerInvariant())
|
2017-07-21 04:56:21 +00:00
|
|
|
|
|| setting.Whitelist.Any(x => x.UserId == user.Id)))
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2017-07-21 05:10:17 +00:00
|
|
|
|
addRole = addRole ?? user.Guild.GetRole(setting.AddRoleId);
|
|
|
|
|
if (addRole == null)
|
|
|
|
|
throw new StreamRoleNotFoundException();
|
|
|
|
|
|
2017-07-21 04:56:21 +00:00
|
|
|
|
//check if he doesn't have addrole already, to avoid errors
|
|
|
|
|
if (!user.RoleIds.Contains(setting.AddRoleId))
|
|
|
|
|
await user.AddRoleAsync(addRole).ConfigureAwait(false);
|
|
|
|
|
_log.Info("Added stream role to user {0} in {1} server", user.ToString(), user.Guild.ToString());
|
|
|
|
|
}
|
|
|
|
|
catch (HttpException ex) when (ex.HttpCode == System.Net.HttpStatusCode.Forbidden)
|
|
|
|
|
{
|
|
|
|
|
await StopStreamRole(user.Guild).ConfigureAwait(false);
|
|
|
|
|
_log.Warn("Error adding stream role(s). Forcibly disabling stream role feature.");
|
|
|
|
|
_log.Error(ex);
|
|
|
|
|
throw new StreamRolePermissionException();
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
_log.Warn("Failed adding stream role.");
|
|
|
|
|
_log.Error(ex);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
//check if user is in the addrole
|
|
|
|
|
if (user.RoleIds.Contains(setting.AddRoleId))
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
addRole = addRole ?? user.Guild.GetRole(setting.AddRoleId);
|
|
|
|
|
if (addRole == null)
|
|
|
|
|
throw new StreamRoleNotFoundException();
|
|
|
|
|
|
|
|
|
|
await user.RemoveRoleAsync(addRole).ConfigureAwait(false);
|
2017-10-10 16:24:36 +00:00
|
|
|
|
_log.Info("Removed stream role from the user {0} in {1} server", user.ToString(), user.Guild.ToString());
|
2017-07-21 04:56:21 +00:00
|
|
|
|
}
|
|
|
|
|
catch (HttpException ex) when (ex.HttpCode == System.Net.HttpStatusCode.Forbidden)
|
|
|
|
|
{
|
|
|
|
|
await StopStreamRole(user.Guild).ConfigureAwait(false);
|
|
|
|
|
_log.Warn("Error removing stream role(s). Forcibly disabling stream role feature.");
|
|
|
|
|
_log.Error(ex);
|
|
|
|
|
throw new StreamRolePermissionException();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private async Task RescanUsers(IGuild guild)
|
|
|
|
|
{
|
|
|
|
|
if (!guildSettings.TryGetValue(guild.Id, out var setting))
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
var addRole = guild.GetRole(setting.AddRoleId);
|
|
|
|
|
if (addRole == null)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
if (setting.Enabled)
|
|
|
|
|
{
|
|
|
|
|
var users = await guild.GetUsersAsync(CacheMode.CacheOnly).ConfigureAwait(false);
|
|
|
|
|
foreach (var usr in users.Where(x => x.RoleIds.Contains(setting.FromRoleId) || x.RoleIds.Contains(addRole.Id)))
|
|
|
|
|
{
|
|
|
|
|
if(usr is IGuildUser x)
|
|
|
|
|
await RescanUser(x, setting, addRole).ConfigureAwait(false);
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-07-14 03:00:30 +00:00
|
|
|
|
}
|
2017-07-19 08:38:14 +00:00
|
|
|
|
|
|
|
|
|
private void UpdateCache(ulong guildId, StreamRoleSettings setting)
|
|
|
|
|
{
|
|
|
|
|
guildSettings.AddOrUpdate(guildId, (key) => setting, (key, old) => setting);
|
|
|
|
|
}
|
2017-07-14 03:00:30 +00:00
|
|
|
|
}
|
|
|
|
|
}
|