2016-11-15 11:01:02 +00:00
|
|
|
|
using System.Linq;
|
2016-10-08 02:25:12 +00:00
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using Discord.Commands;
|
2017-10-13 04:14:54 +00:00
|
|
|
|
using NadekoBot.Core.Services;
|
|
|
|
|
using NadekoBot.Core.Services.Database.Models;
|
2016-10-08 02:25:12 +00:00
|
|
|
|
using Discord;
|
|
|
|
|
using NadekoBot.Extensions;
|
2016-12-31 12:21:18 +00:00
|
|
|
|
using Discord.WebSocket;
|
2017-01-29 03:30:21 +00:00
|
|
|
|
using System;
|
2017-07-17 19:42:36 +00:00
|
|
|
|
using NadekoBot.Common.Attributes;
|
2017-07-17 02:37:51 +00:00
|
|
|
|
using NadekoBot.Modules.CustomReactions.Services;
|
2016-10-08 02:25:12 +00:00
|
|
|
|
|
|
|
|
|
namespace NadekoBot.Modules.CustomReactions
|
2016-11-15 11:01:02 +00:00
|
|
|
|
{
|
2017-07-15 16:34:34 +00:00
|
|
|
|
public class CustomReactions : NadekoTopLevelModule<CustomReactionsService>
|
2016-10-08 02:25:12 +00:00
|
|
|
|
{
|
2017-05-25 02:24:43 +00:00
|
|
|
|
private readonly IBotCredentials _creds;
|
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-25 02:24:43 +00:00
|
|
|
|
|
2017-07-15 16:34:34 +00:00
|
|
|
|
public CustomReactions(IBotCredentials creds, DbService db,
|
2017-06-19 13:42:10 +00:00
|
|
|
|
DiscordSocketClient client)
|
2017-05-25 02:24:43 +00:00
|
|
|
|
{
|
|
|
|
|
_creds = creds;
|
|
|
|
|
_db = db;
|
|
|
|
|
_client = client;
|
|
|
|
|
}
|
|
|
|
|
|
2016-10-08 02:25:12 +00:00
|
|
|
|
[NadekoCommand, Usage, Description, Aliases]
|
2016-12-17 00:16:14 +00:00
|
|
|
|
public async Task AddCustReact(string key, [Remainder] string message)
|
2016-10-08 02:25:12 +00:00
|
|
|
|
{
|
2016-12-17 00:16:14 +00:00
|
|
|
|
var channel = Context.Channel as ITextChannel;
|
2016-10-08 02:25:12 +00:00
|
|
|
|
if (string.IsNullOrWhiteSpace(message) || string.IsNullOrWhiteSpace(key))
|
|
|
|
|
return;
|
|
|
|
|
|
2016-10-10 03:04:50 +00:00
|
|
|
|
key = key.ToLowerInvariant();
|
|
|
|
|
|
2017-05-25 02:24:43 +00:00
|
|
|
|
if ((channel == null && !_creds.IsOwner(Context.User)) || (channel != null && !((IGuildUser)Context.User).GuildPermissions.Administrator))
|
2016-10-08 02:25:12 +00:00
|
|
|
|
{
|
2017-02-13 21:11:36 +00:00
|
|
|
|
await ReplyErrorLocalized("insuff_perms").ConfigureAwait(false);
|
2016-10-08 02:25:12 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var cr = new CustomReaction()
|
|
|
|
|
{
|
|
|
|
|
GuildId = channel?.Guild.Id,
|
|
|
|
|
IsRegex = false,
|
2016-10-10 03:04:50 +00:00
|
|
|
|
Trigger = key,
|
2016-10-08 02:25:12 +00:00
|
|
|
|
Response = message,
|
|
|
|
|
};
|
|
|
|
|
|
2017-05-24 04:43:00 +00:00
|
|
|
|
using (var uow = _db.UnitOfWork)
|
2016-10-08 02:25:12 +00:00
|
|
|
|
{
|
|
|
|
|
uow.CustomReactions.Add(cr);
|
|
|
|
|
|
|
|
|
|
await uow.CompleteAsync().ConfigureAwait(false);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (channel == null)
|
|
|
|
|
{
|
2017-09-13 01:12:40 +00:00
|
|
|
|
await _service.AddGcr(cr).ConfigureAwait(false);
|
2016-10-08 02:25:12 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2017-07-15 16:34:34 +00:00
|
|
|
|
_service.GuildReactions.AddOrUpdate(Context.Guild.Id,
|
2017-02-13 21:11:36 +00:00
|
|
|
|
new CustomReaction[] { cr },
|
2017-01-29 03:30:21 +00:00
|
|
|
|
(k, old) =>
|
|
|
|
|
{
|
|
|
|
|
Array.Resize(ref old, old.Length + 1);
|
|
|
|
|
old[old.Length - 1] = cr;
|
|
|
|
|
return old;
|
|
|
|
|
});
|
2016-10-08 02:25:12 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-12-31 10:55:12 +00:00
|
|
|
|
await Context.Channel.EmbedAsync(new EmbedBuilder().WithOkColor()
|
2017-02-13 21:11:36 +00:00
|
|
|
|
.WithTitle(GetText("new_cust_react"))
|
2016-12-11 16:18:25 +00:00
|
|
|
|
.WithDescription($"#{cr.Id}")
|
2017-02-13 21:11:36 +00:00
|
|
|
|
.AddField(efb => efb.WithName(GetText("trigger")).WithValue(key))
|
2017-07-08 12:20:06 +00:00
|
|
|
|
.AddField(efb => efb.WithName(GetText("response")).WithValue(message.Length > 1024 ? GetText("redacted_too_long") : message))
|
2016-12-16 18:43:57 +00:00
|
|
|
|
).ConfigureAwait(false);
|
2016-10-08 02:25:12 +00:00
|
|
|
|
}
|
|
|
|
|
|
2017-09-27 14:22:42 +00:00
|
|
|
|
[NadekoCommand, Usage, Description, Aliases]
|
|
|
|
|
public async Task EditCustReact(int id, [Remainder] string message)
|
|
|
|
|
{
|
|
|
|
|
var channel = Context.Channel as ITextChannel;
|
|
|
|
|
if (string.IsNullOrWhiteSpace(message) || id < 0)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
if ((channel == null && !_creds.IsOwner(Context.User)) || (channel != null && !((IGuildUser)Context.User).GuildPermissions.Administrator))
|
|
|
|
|
{
|
|
|
|
|
await ReplyErrorLocalized("insuff_perms").ConfigureAwait(false);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
CustomReaction cr;
|
|
|
|
|
using (var uow = _db.UnitOfWork)
|
|
|
|
|
{
|
|
|
|
|
cr = uow.CustomReactions.Get(id);
|
|
|
|
|
|
|
|
|
|
if (cr != null)
|
|
|
|
|
{
|
|
|
|
|
cr.Response = message;
|
|
|
|
|
await uow.CompleteAsync().ConfigureAwait(false);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (cr != null)
|
|
|
|
|
{
|
|
|
|
|
if (channel == null)
|
|
|
|
|
{
|
|
|
|
|
await _service.EditGcr(id, message).ConfigureAwait(false);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
if (_service.GuildReactions.TryGetValue(Context.Guild.Id, out var crs))
|
|
|
|
|
{
|
|
|
|
|
var oldCr = crs.FirstOrDefault(x => x.Id == id);
|
|
|
|
|
if (oldCr != null)
|
|
|
|
|
oldCr.Response = message;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
await Context.Channel.EmbedAsync(new EmbedBuilder().WithOkColor()
|
|
|
|
|
.WithTitle(GetText("edited_cust_react"))
|
|
|
|
|
.WithDescription($"#{cr.Id}")
|
|
|
|
|
.AddField(efb => efb.WithName(GetText("trigger")).WithValue(cr.Trigger))
|
|
|
|
|
.AddField(efb => efb.WithName(GetText("response")).WithValue(message.Length > 1024 ? GetText("redacted_too_long") : message))
|
|
|
|
|
).ConfigureAwait(false);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
await ReplyErrorLocalized("edit_fail").ConfigureAwait(false);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-10-08 02:25:12 +00:00
|
|
|
|
[NadekoCommand, Usage, Description, Aliases]
|
2017-07-15 03:04:16 +00:00
|
|
|
|
[Priority(1)]
|
2016-12-17 00:16:14 +00:00
|
|
|
|
public async Task ListCustReact(int page = 1)
|
2016-10-08 02:25:12 +00:00
|
|
|
|
{
|
2017-06-14 15:19:27 +00:00
|
|
|
|
if (--page < 0 || page > 999)
|
2016-10-08 02:25:12 +00:00
|
|
|
|
return;
|
2017-01-29 03:30:21 +00:00
|
|
|
|
CustomReaction[] customReactions;
|
2016-12-17 00:16:14 +00:00
|
|
|
|
if (Context.Guild == null)
|
2017-07-15 16:34:34 +00:00
|
|
|
|
customReactions = _service.GlobalReactions.Where(cr => cr != null).ToArray();
|
2016-10-08 02:25:12 +00:00
|
|
|
|
else
|
2017-07-15 16:34:34 +00:00
|
|
|
|
customReactions = _service.GuildReactions.GetOrAdd(Context.Guild.Id, Array.Empty<CustomReaction>()).Where(cr => cr != null).ToArray();
|
2016-10-08 02:25:12 +00:00
|
|
|
|
|
|
|
|
|
if (customReactions == null || !customReactions.Any())
|
2017-01-11 16:15:59 +00:00
|
|
|
|
{
|
2017-02-13 21:11:36 +00:00
|
|
|
|
await ReplyErrorLocalized("no_found").ConfigureAwait(false);
|
|
|
|
|
return;
|
2017-01-11 16:15:59 +00:00
|
|
|
|
}
|
2017-02-13 21:11:36 +00:00
|
|
|
|
|
2017-05-25 02:24:43 +00:00
|
|
|
|
await Context.Channel.SendPaginatedConfirmAsync(_client, page, curPage =>
|
2017-02-13 21:11:36 +00:00
|
|
|
|
new EmbedBuilder().WithOkColor()
|
|
|
|
|
.WithTitle(GetText("name"))
|
|
|
|
|
.WithDescription(string.Join("\n", customReactions.OrderBy(cr => cr.Trigger)
|
2017-06-14 15:19:27 +00:00
|
|
|
|
.Skip(curPage * 20)
|
2017-02-13 21:11:36 +00:00
|
|
|
|
.Take(20)
|
2017-03-24 11:15:50 +00:00
|
|
|
|
.Select(cr =>
|
|
|
|
|
{
|
|
|
|
|
var str = $"`#{cr.Id}` {cr.Trigger}";
|
|
|
|
|
if (cr.AutoDeleteTrigger)
|
|
|
|
|
{
|
|
|
|
|
str = "🗑" + str;
|
|
|
|
|
}
|
|
|
|
|
if (cr.DmResponse)
|
|
|
|
|
{
|
|
|
|
|
str = "📪" + str;
|
|
|
|
|
}
|
|
|
|
|
return str;
|
2017-10-17 14:10:51 +00:00
|
|
|
|
}))), customReactions.Length, 20)
|
2017-02-13 21:11:36 +00:00
|
|
|
|
.ConfigureAwait(false);
|
2016-11-12 19:58:33 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public enum All
|
|
|
|
|
{
|
|
|
|
|
All
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[NadekoCommand, Usage, Description, Aliases]
|
2017-07-15 03:04:16 +00:00
|
|
|
|
[Priority(0)]
|
2016-12-17 00:16:14 +00:00
|
|
|
|
public async Task ListCustReact(All x)
|
2016-11-12 19:58:33 +00:00
|
|
|
|
{
|
2017-01-29 03:30:21 +00:00
|
|
|
|
CustomReaction[] customReactions;
|
2016-12-17 00:16:14 +00:00
|
|
|
|
if (Context.Guild == null)
|
2017-07-15 16:34:34 +00:00
|
|
|
|
customReactions = _service.GlobalReactions.Where(cr => cr != null).ToArray();
|
2016-11-12 19:58:33 +00:00
|
|
|
|
else
|
2017-07-15 16:34:34 +00:00
|
|
|
|
customReactions = _service.GuildReactions.GetOrAdd(Context.Guild.Id, new CustomReaction[]{ }).Where(cr => cr != null).ToArray();
|
2016-11-12 19:58:33 +00:00
|
|
|
|
|
|
|
|
|
if (customReactions == null || !customReactions.Any())
|
|
|
|
|
{
|
2017-02-13 21:11:36 +00:00
|
|
|
|
await ReplyErrorLocalized("no_found").ConfigureAwait(false);
|
|
|
|
|
return;
|
2016-11-12 19:58:33 +00:00
|
|
|
|
}
|
2017-02-13 21:11:36 +00:00
|
|
|
|
|
|
|
|
|
var txtStream = await customReactions.GroupBy(cr => cr.Trigger)
|
|
|
|
|
.OrderBy(cr => cr.Key)
|
|
|
|
|
.Select(cr => new { Trigger = cr.Key, Responses = cr.Select(y => new { id = y.Id, text = y.Response }).ToList() })
|
|
|
|
|
.ToJson()
|
|
|
|
|
.ToStream()
|
|
|
|
|
.ConfigureAwait(false);
|
|
|
|
|
|
|
|
|
|
if (Context.Guild == null) // its a private one, just send back
|
|
|
|
|
await Context.Channel.SendFileAsync(txtStream, "customreactions.txt", GetText("list_all")).ConfigureAwait(false);
|
|
|
|
|
else
|
2017-09-10 01:52:34 +00:00
|
|
|
|
await ((IGuildUser)Context.User).SendFileAsync(txtStream, "customreactions.txt", GetText("list_all"), false).ConfigureAwait(false);
|
2016-11-12 19:58:33 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[NadekoCommand, Usage, Description, Aliases]
|
2016-12-17 00:16:14 +00:00
|
|
|
|
public async Task ListCustReactG(int page = 1)
|
2016-11-12 19:58:33 +00:00
|
|
|
|
{
|
2017-06-14 15:19:27 +00:00
|
|
|
|
if (--page < 0 || page > 9999)
|
2016-11-12 19:58:33 +00:00
|
|
|
|
return;
|
2017-01-29 03:30:21 +00:00
|
|
|
|
CustomReaction[] customReactions;
|
2016-12-17 00:16:14 +00:00
|
|
|
|
if (Context.Guild == null)
|
2017-07-15 16:34:34 +00:00
|
|
|
|
customReactions = _service.GlobalReactions.Where(cr => cr != null).ToArray();
|
2016-11-12 19:58:33 +00:00
|
|
|
|
else
|
2017-07-15 16:34:34 +00:00
|
|
|
|
customReactions = _service.GuildReactions.GetOrAdd(Context.Guild.Id, new CustomReaction[]{ }).Where(cr => cr != null).ToArray();
|
2016-11-12 19:58:33 +00:00
|
|
|
|
|
|
|
|
|
if (customReactions == null || !customReactions.Any())
|
2017-02-13 21:11:36 +00:00
|
|
|
|
{
|
|
|
|
|
await ReplyErrorLocalized("no_found").ConfigureAwait(false);
|
|
|
|
|
}
|
2016-11-12 19:58:33 +00:00
|
|
|
|
else
|
2017-01-11 16:15:59 +00:00
|
|
|
|
{
|
|
|
|
|
var ordered = customReactions
|
|
|
|
|
.GroupBy(cr => cr.Trigger)
|
|
|
|
|
.OrderBy(cr => cr.Key)
|
|
|
|
|
.ToList();
|
2017-10-17 14:10:51 +00:00
|
|
|
|
|
2017-05-25 02:24:43 +00:00
|
|
|
|
await Context.Channel.SendPaginatedConfirmAsync(_client, page, (curPage) =>
|
2017-01-11 16:15:59 +00:00
|
|
|
|
new EmbedBuilder().WithOkColor()
|
2017-02-13 21:11:36 +00:00
|
|
|
|
.WithTitle(GetText("name"))
|
2017-01-11 16:15:59 +00:00
|
|
|
|
.WithDescription(string.Join("\r\n", ordered
|
2017-06-14 15:19:27 +00:00
|
|
|
|
.Skip(curPage * 20)
|
2017-01-11 16:15:59 +00:00
|
|
|
|
.Take(20)
|
2017-10-17 14:10:51 +00:00
|
|
|
|
.Select(cr => $"**{cr.Key.Trim().ToLowerInvariant()}** `x{cr.Count()}`"))),
|
|
|
|
|
ordered.Count, 20).ConfigureAwait(false);
|
2017-01-11 16:15:59 +00:00
|
|
|
|
}
|
2016-10-08 02:25:12 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-10-14 08:50:04 +00:00
|
|
|
|
[NadekoCommand, Usage, Description, Aliases]
|
2016-12-21 08:33:47 +00:00
|
|
|
|
public async Task ShowCustReact(int id)
|
2016-10-14 08:50:04 +00:00
|
|
|
|
{
|
2017-01-29 03:30:21 +00:00
|
|
|
|
CustomReaction[] customReactions;
|
2016-12-17 00:16:14 +00:00
|
|
|
|
if (Context.Guild == null)
|
2017-07-15 16:34:34 +00:00
|
|
|
|
customReactions = _service.GlobalReactions;
|
2016-10-14 08:50:04 +00:00
|
|
|
|
else
|
2017-07-15 16:34:34 +00:00
|
|
|
|
customReactions = _service.GuildReactions.GetOrAdd(Context.Guild.Id, new CustomReaction[]{ });
|
2016-10-14 08:50:04 +00:00
|
|
|
|
|
2017-01-29 03:30:21 +00:00
|
|
|
|
var found = customReactions.FirstOrDefault(cr => cr?.Id == id);
|
2016-10-14 08:50:04 +00:00
|
|
|
|
|
|
|
|
|
if (found == null)
|
2017-02-13 21:11:36 +00:00
|
|
|
|
{
|
|
|
|
|
await ReplyErrorLocalized("no_found_id").ConfigureAwait(false);
|
|
|
|
|
return;
|
|
|
|
|
}
|
2016-10-14 08:50:04 +00:00
|
|
|
|
else
|
|
|
|
|
{
|
2016-12-31 10:55:12 +00:00
|
|
|
|
await Context.Channel.EmbedAsync(new EmbedBuilder().WithOkColor()
|
2016-12-11 16:18:25 +00:00
|
|
|
|
.WithDescription($"#{id}")
|
2017-02-13 21:11:36 +00:00
|
|
|
|
.AddField(efb => efb.WithName(GetText("trigger")).WithValue(found.Trigger))
|
|
|
|
|
.AddField(efb => efb.WithName(GetText("response")).WithValue(found.Response + "\n```css\n" + found.Response + "```"))
|
2016-12-16 18:43:57 +00:00
|
|
|
|
).ConfigureAwait(false);
|
2016-10-14 08:50:04 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-10-08 02:25:12 +00:00
|
|
|
|
[NadekoCommand, Usage, Description, Aliases]
|
2016-12-21 08:33:47 +00:00
|
|
|
|
public async Task DelCustReact(int id)
|
2016-10-08 02:25:12 +00:00
|
|
|
|
{
|
2017-05-25 02:24:43 +00:00
|
|
|
|
if ((Context.Guild == null && !_creds.IsOwner(Context.User)) || (Context.Guild != null && !((IGuildUser)Context.User).GuildPermissions.Administrator))
|
2016-10-08 02:25:12 +00:00
|
|
|
|
{
|
2017-02-13 21:11:36 +00:00
|
|
|
|
await ReplyErrorLocalized("insuff_perms").ConfigureAwait(false);
|
2016-10-08 02:25:12 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var success = false;
|
|
|
|
|
CustomReaction toDelete;
|
2017-05-24 04:43:00 +00:00
|
|
|
|
using (var uow = _db.UnitOfWork)
|
2016-10-08 02:25:12 +00:00
|
|
|
|
{
|
|
|
|
|
toDelete = uow.CustomReactions.Get(id);
|
|
|
|
|
if (toDelete == null) //not found
|
2017-02-13 21:11:36 +00:00
|
|
|
|
success = false;
|
|
|
|
|
else
|
2016-10-08 02:25:12 +00:00
|
|
|
|
{
|
2017-02-13 21:11:36 +00:00
|
|
|
|
if ((toDelete.GuildId == null || toDelete.GuildId == 0) && Context.Guild == null)
|
2017-01-29 03:30:21 +00:00
|
|
|
|
{
|
2017-02-13 21:11:36 +00:00
|
|
|
|
uow.CustomReactions.Remove(toDelete);
|
2017-09-13 01:12:40 +00:00
|
|
|
|
await _service.DelGcr(toDelete.Id);
|
2017-02-13 21:11:36 +00:00
|
|
|
|
success = true;
|
|
|
|
|
}
|
|
|
|
|
else if ((toDelete.GuildId != null && toDelete.GuildId != 0) && Context.Guild.Id == toDelete.GuildId)
|
|
|
|
|
{
|
|
|
|
|
uow.CustomReactions.Remove(toDelete);
|
2017-07-15 16:34:34 +00:00
|
|
|
|
_service.GuildReactions.AddOrUpdate(Context.Guild.Id, new CustomReaction[] { }, (key, old) =>
|
2017-02-13 21:11:36 +00:00
|
|
|
|
{
|
|
|
|
|
return old.Where(cr => cr?.Id != toDelete.Id).ToArray();
|
|
|
|
|
});
|
|
|
|
|
success = true;
|
|
|
|
|
}
|
|
|
|
|
if (success)
|
|
|
|
|
await uow.CompleteAsync().ConfigureAwait(false);
|
2016-10-08 02:25:12 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (success)
|
2017-02-13 21:11:36 +00:00
|
|
|
|
{
|
|
|
|
|
await Context.Channel.EmbedAsync(new EmbedBuilder().WithOkColor()
|
|
|
|
|
.WithTitle(GetText("deleted"))
|
|
|
|
|
.WithDescription("#" + toDelete.Id)
|
|
|
|
|
.AddField(efb => efb.WithName(GetText("trigger")).WithValue(toDelete.Trigger))
|
|
|
|
|
.AddField(efb => efb.WithName(GetText("response")).WithValue(toDelete.Response)));
|
|
|
|
|
}
|
2016-10-08 02:25:12 +00:00
|
|
|
|
else
|
2017-02-13 21:11:36 +00:00
|
|
|
|
{
|
|
|
|
|
await ReplyErrorLocalized("no_found_id").ConfigureAwait(false);
|
|
|
|
|
}
|
2016-10-08 02:25:12 +00:00
|
|
|
|
}
|
2016-12-15 04:22:12 +00:00
|
|
|
|
|
2017-03-18 19:23:26 +00:00
|
|
|
|
[NadekoCommand, Usage, Description, Aliases]
|
2017-07-22 16:12:08 +00:00
|
|
|
|
public async Task CrCa(int id)
|
2017-03-18 19:23:26 +00:00
|
|
|
|
{
|
2017-07-23 07:52:45 +00:00
|
|
|
|
if ((Context.Guild == null && !_creds.IsOwner(Context.User)) ||
|
2017-03-18 19:23:26 +00:00
|
|
|
|
(Context.Guild != null && !((IGuildUser)Context.User).GuildPermissions.Administrator))
|
|
|
|
|
{
|
|
|
|
|
await ReplyErrorLocalized("insuff_perms").ConfigureAwait(false);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
CustomReaction[] reactions = new CustomReaction[0];
|
|
|
|
|
|
|
|
|
|
if (Context.Guild == null)
|
2017-07-15 16:34:34 +00:00
|
|
|
|
reactions = _service.GlobalReactions;
|
2017-03-18 19:23:26 +00:00
|
|
|
|
else
|
|
|
|
|
{
|
2017-07-15 16:34:34 +00:00
|
|
|
|
_service.GuildReactions.TryGetValue(Context.Guild.Id, out reactions);
|
2017-03-18 19:23:26 +00:00
|
|
|
|
}
|
|
|
|
|
if (reactions.Any())
|
|
|
|
|
{
|
|
|
|
|
var reaction = reactions.FirstOrDefault(x => x.Id == id);
|
|
|
|
|
|
|
|
|
|
if (reaction == null)
|
|
|
|
|
{
|
|
|
|
|
await ReplyErrorLocalized("no_found_id").ConfigureAwait(false);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2017-07-22 16:12:08 +00:00
|
|
|
|
var setValue = reaction.ContainsAnywhere = !reaction.ContainsAnywhere;
|
2017-09-24 06:56:36 +00:00
|
|
|
|
|
|
|
|
|
await _service.SetCrCaAsync(reaction.Id, setValue).ConfigureAwait(false);
|
2017-03-18 19:23:26 +00:00
|
|
|
|
|
|
|
|
|
if (setValue)
|
|
|
|
|
{
|
2017-07-22 16:12:08 +00:00
|
|
|
|
await ReplyConfirmLocalized("crca_enabled", Format.Code(reaction.Id.ToString())).ConfigureAwait(false);
|
2017-03-18 19:23:26 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2017-07-22 16:12:08 +00:00
|
|
|
|
await ReplyConfirmLocalized("crca_disabled", Format.Code(reaction.Id.ToString())).ConfigureAwait(false);
|
2017-03-18 19:23:26 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
await ReplyErrorLocalized("no_found").ConfigureAwait(false);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-07-23 07:52:45 +00:00
|
|
|
|
[NadekoCommand, Usage, Description, Aliases]
|
|
|
|
|
public async Task CrDm(int id)
|
|
|
|
|
{
|
|
|
|
|
if ((Context.Guild == null && !_creds.IsOwner(Context.User)) ||
|
|
|
|
|
(Context.Guild != null && !((IGuildUser)Context.User).GuildPermissions.Administrator))
|
|
|
|
|
{
|
|
|
|
|
await ReplyErrorLocalized("insuff_perms").ConfigureAwait(false);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
CustomReaction[] reactions = new CustomReaction[0];
|
|
|
|
|
|
|
|
|
|
if (Context.Guild == null)
|
|
|
|
|
reactions = _service.GlobalReactions;
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
_service.GuildReactions.TryGetValue(Context.Guild.Id, out reactions);
|
|
|
|
|
}
|
|
|
|
|
if (reactions.Any())
|
|
|
|
|
{
|
|
|
|
|
var reaction = reactions.FirstOrDefault(x => x.Id == id);
|
|
|
|
|
|
|
|
|
|
if (reaction == null)
|
|
|
|
|
{
|
|
|
|
|
await ReplyErrorLocalized("no_found_id").ConfigureAwait(false);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var setValue = reaction.DmResponse = !reaction.DmResponse;
|
|
|
|
|
|
2017-09-24 06:56:36 +00:00
|
|
|
|
await _service.SetCrDmAsync(reaction.Id, setValue).ConfigureAwait(false);
|
2017-07-23 07:52:45 +00:00
|
|
|
|
|
|
|
|
|
if (setValue)
|
|
|
|
|
{
|
|
|
|
|
await ReplyConfirmLocalized("crdm_enabled", Format.Code(reaction.Id.ToString())).ConfigureAwait(false);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
await ReplyConfirmLocalized("crdm_disabled", Format.Code(reaction.Id.ToString())).ConfigureAwait(false);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
await ReplyErrorLocalized("no_found").ConfigureAwait(false);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-03-18 19:23:26 +00:00
|
|
|
|
[NadekoCommand, Usage, Description, Aliases]
|
|
|
|
|
public async Task CrAd(int id)
|
|
|
|
|
{
|
2017-05-25 02:24:43 +00:00
|
|
|
|
if ((Context.Guild == null && !_creds.IsOwner(Context.User)) ||
|
2017-03-18 19:23:26 +00:00
|
|
|
|
(Context.Guild != null && !((IGuildUser)Context.User).GuildPermissions.Administrator))
|
|
|
|
|
{
|
|
|
|
|
await ReplyErrorLocalized("insuff_perms").ConfigureAwait(false);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
CustomReaction[] reactions = new CustomReaction[0];
|
|
|
|
|
|
|
|
|
|
if (Context.Guild == null)
|
2017-07-15 16:34:34 +00:00
|
|
|
|
reactions = _service.GlobalReactions;
|
2017-03-18 19:23:26 +00:00
|
|
|
|
else
|
|
|
|
|
{
|
2017-07-15 16:34:34 +00:00
|
|
|
|
_service.GuildReactions.TryGetValue(Context.Guild.Id, out reactions);
|
2017-03-18 19:23:26 +00:00
|
|
|
|
}
|
|
|
|
|
if (reactions.Any())
|
|
|
|
|
{
|
|
|
|
|
var reaction = reactions.FirstOrDefault(x => x.Id == id);
|
|
|
|
|
|
|
|
|
|
if (reaction == null)
|
|
|
|
|
{
|
|
|
|
|
await ReplyErrorLocalized("no_found_id").ConfigureAwait(false);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var setValue = reaction.AutoDeleteTrigger = !reaction.AutoDeleteTrigger;
|
2017-09-24 06:56:36 +00:00
|
|
|
|
|
|
|
|
|
await _service.SetCrAdAsync(reaction.Id, setValue).ConfigureAwait(false);
|
2017-03-18 19:23:26 +00:00
|
|
|
|
|
|
|
|
|
if (setValue)
|
|
|
|
|
{
|
|
|
|
|
await ReplyConfirmLocalized("crad_enabled", Format.Code(reaction.Id.ToString())).ConfigureAwait(false);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
await ReplyConfirmLocalized("crad_disabled", Format.Code(reaction.Id.ToString())).ConfigureAwait(false);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
await ReplyErrorLocalized("no_found").ConfigureAwait(false);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-12-15 04:22:12 +00:00
|
|
|
|
[NadekoCommand, Usage, Description, Aliases]
|
2017-01-02 07:07:09 +00:00
|
|
|
|
[OwnerOnly]
|
2016-12-21 08:33:47 +00:00
|
|
|
|
public async Task CrStatsClear(string trigger = null)
|
2016-12-15 04:22:12 +00:00
|
|
|
|
{
|
|
|
|
|
if (string.IsNullOrWhiteSpace(trigger))
|
|
|
|
|
{
|
2017-07-15 16:34:34 +00:00
|
|
|
|
_service.ClearStats();
|
2017-02-13 21:11:36 +00:00
|
|
|
|
await ReplyConfirmLocalized("all_stats_cleared").ConfigureAwait(false);
|
2016-12-15 04:22:12 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2017-07-25 16:31:30 +00:00
|
|
|
|
if (_service.ReactionStats.TryRemove(trigger, out _))
|
2016-12-15 04:22:12 +00:00
|
|
|
|
{
|
2017-02-13 21:11:36 +00:00
|
|
|
|
await ReplyErrorLocalized("stats_cleared", Format.Bold(trigger)).ConfigureAwait(false);
|
2016-12-15 04:22:12 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2017-02-13 21:11:36 +00:00
|
|
|
|
await ReplyErrorLocalized("stats_not_found").ConfigureAwait(false);
|
2016-12-15 04:22:12 +00:00
|
|
|
|
}
|
2016-12-26 05:16:34 +00:00
|
|
|
|
}
|
2016-12-15 04:22:12 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[NadekoCommand, Usage, Description, Aliases]
|
2016-12-21 08:33:47 +00:00
|
|
|
|
public async Task CrStats(int page = 1)
|
2016-12-15 04:22:12 +00:00
|
|
|
|
{
|
2017-06-14 15:19:27 +00:00
|
|
|
|
if (--page < 0)
|
2016-12-15 04:22:12 +00:00
|
|
|
|
return;
|
2017-07-15 16:34:34 +00:00
|
|
|
|
var ordered = _service.ReactionStats.OrderByDescending(x => x.Value).ToArray();
|
2017-01-29 03:30:21 +00:00
|
|
|
|
if (!ordered.Any())
|
|
|
|
|
return;
|
2017-05-25 02:24:43 +00:00
|
|
|
|
await Context.Channel.SendPaginatedConfirmAsync(_client, page,
|
2017-06-14 15:19:27 +00:00
|
|
|
|
(curPage) => ordered.Skip(curPage * 9)
|
2017-01-11 16:15:59 +00:00
|
|
|
|
.Take(9)
|
2017-02-13 21:11:36 +00:00
|
|
|
|
.Aggregate(new EmbedBuilder().WithOkColor().WithTitle(GetText("stats")),
|
2017-10-17 14:10:51 +00:00
|
|
|
|
(agg, cur) => agg.AddField(efb => efb.WithName(cur.Key).WithValue(cur.Value.ToString()).WithIsInline(true))),
|
|
|
|
|
ordered.Length, 9)
|
2017-01-11 16:15:59 +00:00
|
|
|
|
.ConfigureAwait(false);
|
2016-12-15 04:22:12 +00:00
|
|
|
|
}
|
2016-10-08 02:25:12 +00:00
|
|
|
|
}
|
2016-12-26 05:16:34 +00:00
|
|
|
|
}
|