2017-05-24 04:43:00 +00:00
|
|
|
|
using Discord;
|
|
|
|
|
using Discord.WebSocket;
|
2017-05-25 02:24:43 +00:00
|
|
|
|
using NadekoBot.DataStructures.ModuleBehaviors;
|
2017-05-24 04:43:00 +00:00
|
|
|
|
using NadekoBot.Services.Database.Models;
|
|
|
|
|
using NLog;
|
|
|
|
|
using System.Collections.Concurrent;
|
|
|
|
|
using System.Diagnostics;
|
|
|
|
|
using System.Linq;
|
2017-05-25 02:24:43 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Threading.Tasks;
|
2017-05-30 00:51:56 +00:00
|
|
|
|
using NadekoBot.Services.Permissions;
|
2017-05-30 04:54:59 +00:00
|
|
|
|
using NadekoBot.Extensions;
|
2017-06-22 21:59:54 +00:00
|
|
|
|
using NadekoBot.Services.Database;
|
2017-05-24 04:43:00 +00:00
|
|
|
|
|
2017-05-25 02:24:43 +00:00
|
|
|
|
namespace NadekoBot.Services.CustomReactions
|
2017-05-24 04:43:00 +00:00
|
|
|
|
{
|
2017-05-29 04:13:22 +00:00
|
|
|
|
public class CustomReactionsService : IEarlyBlockingExecutor
|
2017-05-24 04:43:00 +00:00
|
|
|
|
{
|
2017-05-25 02:24:43 +00:00
|
|
|
|
public CustomReaction[] GlobalReactions = new CustomReaction[] { };
|
2017-05-24 04:43:00 +00:00
|
|
|
|
public ConcurrentDictionary<ulong, CustomReaction[]> GuildReactions { get; } = new ConcurrentDictionary<ulong, CustomReaction[]>();
|
|
|
|
|
|
|
|
|
|
public ConcurrentDictionary<string, uint> ReactionStats { get; } = new ConcurrentDictionary<string, uint>();
|
|
|
|
|
|
|
|
|
|
private readonly Logger _log;
|
2017-05-29 04:13:22 +00:00
|
|
|
|
private readonly DbService _db;
|
2017-06-19 13:42:10 +00:00
|
|
|
|
private readonly DiscordSocketClient _client;
|
2017-05-30 04:54:59 +00:00
|
|
|
|
private readonly PermissionService _perms;
|
2017-06-01 03:12:00 +00:00
|
|
|
|
private readonly CommandHandler _cmd;
|
2017-06-16 15:47:02 +00:00
|
|
|
|
private readonly BotConfig _bc;
|
2017-05-24 04:43:00 +00:00
|
|
|
|
|
2017-06-01 03:12:00 +00:00
|
|
|
|
public CustomReactionsService(PermissionService perms, DbService db,
|
2017-06-22 21:59:54 +00:00
|
|
|
|
DiscordSocketClient client, CommandHandler cmd, BotConfig bc, IUnitOfWork uow)
|
2017-05-24 04:43:00 +00:00
|
|
|
|
{
|
|
|
|
|
_log = LogManager.GetCurrentClassLogger();
|
|
|
|
|
_db = db;
|
2017-05-25 02:24:43 +00:00
|
|
|
|
_client = client;
|
2017-05-30 00:51:56 +00:00
|
|
|
|
_perms = perms;
|
2017-06-01 03:12:00 +00:00
|
|
|
|
_cmd = cmd;
|
2017-06-16 15:47:02 +00:00
|
|
|
|
_bc = bc;
|
2017-06-22 21:59:54 +00:00
|
|
|
|
|
|
|
|
|
var items = uow.CustomReactions.GetAll();
|
|
|
|
|
GuildReactions = new ConcurrentDictionary<ulong, CustomReaction[]>(items.Where(g => g.GuildId != null && g.GuildId != 0).GroupBy(k => k.GuildId.Value).ToDictionary(g => g.Key, g => g.ToArray()));
|
|
|
|
|
GlobalReactions = items.Where(g => g.GuildId == null || g.GuildId == 0).ToArray();
|
2017-05-24 04:43:00 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void ClearStats() => ReactionStats.Clear();
|
|
|
|
|
|
|
|
|
|
public CustomReaction TryGetCustomReaction(IUserMessage umsg)
|
|
|
|
|
{
|
|
|
|
|
var channel = umsg.Channel as SocketTextChannel;
|
|
|
|
|
if (channel == null)
|
|
|
|
|
return null;
|
|
|
|
|
|
|
|
|
|
var content = umsg.Content.Trim().ToLowerInvariant();
|
|
|
|
|
|
2017-05-30 00:51:56 +00:00
|
|
|
|
if (GuildReactions.TryGetValue(channel.Guild.Id, out CustomReaction[] reactions))
|
|
|
|
|
if (reactions != null && reactions.Any())
|
2017-05-24 04:43:00 +00:00
|
|
|
|
{
|
2017-05-30 00:51:56 +00:00
|
|
|
|
var rs = reactions.Where(cr =>
|
|
|
|
|
{
|
|
|
|
|
if (cr == null)
|
|
|
|
|
return false;
|
2017-05-24 04:43:00 +00:00
|
|
|
|
|
2017-05-30 00:51:56 +00:00
|
|
|
|
var hasTarget = cr.Response.ToLowerInvariant().Contains("%target%");
|
|
|
|
|
var trigger = cr.TriggerWithContext(umsg, _client).Trim().ToLowerInvariant();
|
2017-06-16 18:19:29 +00:00
|
|
|
|
return ((hasTarget && content.StartsWith(trigger + " ")) || (_bc.CustomReactionsStartWith && content.StartsWith(trigger + " ")) || content == trigger);
|
2017-05-30 00:51:56 +00:00
|
|
|
|
}).ToArray();
|
2017-05-24 04:43:00 +00:00
|
|
|
|
|
2017-05-30 00:51:56 +00:00
|
|
|
|
if (rs.Length != 0)
|
2017-05-24 04:43:00 +00:00
|
|
|
|
{
|
2017-05-30 00:51:56 +00:00
|
|
|
|
var reaction = rs[new NadekoRandom().Next(0, rs.Length)];
|
|
|
|
|
if (reaction != null)
|
|
|
|
|
{
|
|
|
|
|
if (reaction.Response == "-")
|
|
|
|
|
return null;
|
|
|
|
|
return reaction;
|
|
|
|
|
}
|
2017-05-24 04:43:00 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var grs = GlobalReactions.Where(cr =>
|
|
|
|
|
{
|
|
|
|
|
if (cr == null)
|
|
|
|
|
return false;
|
|
|
|
|
var hasTarget = cr.Response.ToLowerInvariant().Contains("%target%");
|
2017-05-25 02:24:43 +00:00
|
|
|
|
var trigger = cr.TriggerWithContext(umsg, _client).Trim().ToLowerInvariant();
|
2017-06-16 18:19:29 +00:00
|
|
|
|
return ((hasTarget && content.StartsWith(trigger + " ")) || (_bc.CustomReactionsStartWith && content.StartsWith(trigger + " ")) || content == trigger);
|
2017-05-24 04:43:00 +00:00
|
|
|
|
}).ToArray();
|
|
|
|
|
if (grs.Length == 0)
|
|
|
|
|
return null;
|
|
|
|
|
var greaction = grs[new NadekoRandom().Next(0, grs.Length)];
|
|
|
|
|
|
|
|
|
|
return greaction;
|
|
|
|
|
}
|
2017-05-25 02:24:43 +00:00
|
|
|
|
|
2017-06-19 13:42:10 +00:00
|
|
|
|
public async Task<bool> TryExecuteEarly(DiscordSocketClient client, IGuild guild, IUserMessage msg)
|
2017-05-25 02:24:43 +00:00
|
|
|
|
{
|
|
|
|
|
// maybe this message is a custom reaction
|
|
|
|
|
var cr = await Task.Run(() => TryGetCustomReaction(msg)).ConfigureAwait(false);
|
2017-05-29 04:13:22 +00:00
|
|
|
|
if (cr != null)
|
2017-05-25 02:24:43 +00:00
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2017-05-30 00:51:56 +00:00
|
|
|
|
if (guild is SocketGuild sg)
|
|
|
|
|
{
|
2017-05-30 04:54:59 +00:00
|
|
|
|
var pc = _perms.GetCache(guild.Id);
|
2017-05-30 00:51:56 +00:00
|
|
|
|
if (!pc.Permissions.CheckPermissions(msg, cr.Trigger, "ActualCustomReactions",
|
|
|
|
|
out int index))
|
|
|
|
|
{
|
2017-05-30 04:54:59 +00:00
|
|
|
|
if (pc.Verbose)
|
|
|
|
|
{
|
2017-06-01 03:12:00 +00:00
|
|
|
|
var returnMsg = $"Permission number #{index + 1} **{pc.Permissions[index].GetCommand(_cmd.GetPrefix(guild), sg)}** is preventing this action.";
|
2017-05-30 04:54:59 +00:00
|
|
|
|
try { await msg.Channel.SendErrorAsync(returnMsg).ConfigureAwait(false); } catch { }
|
|
|
|
|
_log.Info(returnMsg);
|
|
|
|
|
}
|
2017-05-30 00:51:56 +00:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-05-25 02:24:43 +00:00
|
|
|
|
await cr.Send(msg, _client, this).ConfigureAwait(false);
|
|
|
|
|
|
|
|
|
|
if (cr.AutoDeleteTrigger)
|
|
|
|
|
{
|
|
|
|
|
try { await msg.DeleteAsync().ConfigureAwait(false); } catch { }
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
_log.Warn("Sending CREmbed failed");
|
|
|
|
|
_log.Warn(ex);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2017-05-24 04:43:00 +00:00
|
|
|
|
}
|
|
|
|
|
}
|