示例#1
0
        public async Task ListCustReact(int page = 1)
        {
            if (page < 1 || page > 1000)
            {
                return;
            }
            CustomReaction[] customReactions;
            if (Context.Guild == null)
            {
                customReactions = GlobalReactions.Where(cr => cr != null).ToArray();
            }
            else
            {
                customReactions = GuildReactions.GetOrAdd(Context.Guild.Id, Array.Empty <CustomReaction>()).Where(cr => cr != null).ToArray();
            }

            if (customReactions == null || !customReactions.Any())
            {
                await Context.Channel.SendErrorAsync("No custom reactions found").ConfigureAwait(false);
            }
            else
            {
                var lastPage = customReactions.Length / 20;
                await Context.Channel.SendPaginatedConfirmAsync(page, curPage =>
                                                                new EmbedBuilder().WithOkColor()
                                                                .WithTitle("Custom reactions")
                                                                .WithDescription(string.Join("\n", customReactions.OrderBy(cr => cr.Trigger)
                                                                                             .Skip((curPage - 1) * 20)
                                                                                             .Take(20)
                                                                                             .Select(cr => $"`#{cr.Id}`  `Trigger:` {cr.Trigger}"))), lastPage)
                .ConfigureAwait(false);
            }
        }
示例#2
0
        public async Task ListCustReact(All x)
        {
            CustomReaction[] customReactions;
            if (Context.Guild == null)
            {
                customReactions = GlobalReactions.Where(cr => cr != null).ToArray();
            }
            else
            {
                customReactions = GuildReactions.GetOrAdd(Context.Guild.Id, new CustomReaction[] { }).Where(cr => cr != null).ToArray();
            }

            if (customReactions == null || !customReactions.Any())
            {
                await ReplyErrorLocalized("no_found").ConfigureAwait(false);

                return;
            }

            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
            {
                await((IGuildUser)Context.User).SendFileAsync(txtStream, "customreactions.txt", GetText("list_all")).ConfigureAwait(false);
            }
        }
示例#3
0
        public async Task ShowCustReact(int id)
        {
            CustomReaction[] customReactions;
            if (Context.Guild == null)
            {
                customReactions = GlobalReactions;
            }
            else
            {
                customReactions = GuildReactions.GetOrAdd(Context.Guild.Id, new CustomReaction[] { });
            }

            var found = customReactions.FirstOrDefault(cr => cr?.Id == id);

            if (found == null)
            {
                await ReplyErrorLocalized("no_found_id").ConfigureAwait(false);

                return;
            }
            else
            {
                await Context.Channel.EmbedAsync(new EmbedBuilder().WithOkColor()
                                                 .WithDescription($"#{id}")
                                                 .AddField(efb => efb.WithName(GetText("trigger")).WithValue(found.Trigger))
                                                 .AddField(efb => efb.WithName(GetText("response")).WithValue(found.Response + "\n```css\n" + found.Response + "```"))
                                                 ).ConfigureAwait(false);
            }
        }
        public async Task ListCustReact(int page = 1)
        {
            if (page < 1 || page > 1000)
            {
                return;
            }
            ConcurrentHashSet <CustomReaction> customReactions;

            if (Context.Guild == null)
            {
                customReactions = GlobalReactions;
            }
            else
            {
                customReactions = GuildReactions.GetOrAdd(Context.Guild.Id, new ConcurrentHashSet <CustomReaction>());
            }

            if (customReactions == null || !customReactions.Any())
            {
                await Context.Channel.SendErrorAsync("No custom reactions found").ConfigureAwait(false);
            }
            else
            {
                await Context.Channel.SendConfirmAsync(
                    $"Page {page} of custom reactions:",
                    string.Join("\n", customReactions.OrderBy(cr => cr.Trigger)
                                .Skip((page - 1) * 20)
                                .Take(20)
                                .Select(cr => $"`#{cr.Id}`  `Trigger:` {cr.Trigger}")))
                .ConfigureAwait(false);
            }
        }
示例#5
0
        public async Task ShowCustReact(IUserMessage imsg, int id)
        {
            var channel = imsg.Channel as ITextChannel;

            ConcurrentHashSet <CustomReaction> customReactions;

            if (channel == null)
            {
                customReactions = GlobalReactions;
            }
            else
            {
                customReactions = GuildReactions.GetOrAdd(channel.Guild.Id, new ConcurrentHashSet <CustomReaction>());
            }

            var found = customReactions.FirstOrDefault(cr => cr.Id == id);

            if (found == null)
            {
                await imsg.Channel.SendErrorAsync("No custom reaction found with that id.").ConfigureAwait(false);
            }
            else
            {
                await imsg.Channel.EmbedAsync(new EmbedBuilder().WithColor(NadekoBot.OkColor)
                                              .WithDescription($"#{id}")
                                              .AddField(efb => efb.WithName("Trigger").WithValue(found.Trigger))
                                              .AddField(efb => efb.WithName("Response").WithValue(found.Response + "\n```css\n" + found.Response + "```"))
                                              .Build()).ConfigureAwait(false);
            }
        }
示例#6
0
        public async Task ListCustReactG(IUserMessage imsg, int page = 1)
        {
            var channel = imsg.Channel as ITextChannel;

            if (page < 1 || page > 10000)
            {
                return;
            }
            ConcurrentHashSet <CustomReaction> customReactions;

            if (channel == null)
            {
                customReactions = GlobalReactions;
            }
            else
            {
                customReactions = GuildReactions.GetOrAdd(channel.Guild.Id, new ConcurrentHashSet <CustomReaction>());
            }

            if (customReactions == null || !customReactions.Any())
            {
                await imsg.Channel.SendErrorAsync("No custom reactions found").ConfigureAwait(false);
            }
            else
            {
                await imsg.Channel.SendConfirmAsync($"Page {page} of custom reactions (grouped):",
                                                    string.Join("\r\n", customReactions
                                                                .GroupBy(cr => cr.Trigger)
                                                                .OrderBy(cr => cr.Key)
                                                                .Skip((page - 1) * 20)
                                                                .Take(20)
                                                                .Select(cr => $"**{cr.Key.Trim().ToLowerInvariant()}** `x{cr.Count()}`")))
                .ConfigureAwait(false);
            }
        }
示例#7
0
        public async Task AddCustReact(string key, [Remainder] string message)
        {
            var channel = Context.Channel as ITextChannel;

            if (string.IsNullOrWhiteSpace(message) || string.IsNullOrWhiteSpace(key))
            {
                return;
            }

            key = key.ToLowerInvariant();

            if ((channel == null && !NadekoBot.Credentials.IsOwner(Context.User)) || (channel != null && !((IGuildUser)Context.User).GuildPermissions.Administrator))
            {
                await ReplyErrorLocalized("insuff_perms").ConfigureAwait(false);

                return;
            }

            var cr = new CustomReaction()
            {
                GuildId  = (long?)channel?.Guild.Id,
                IsRegex  = false,
                Trigger  = key,
                Response = message,
            };

            using (var uow = DbHandler.UnitOfWork())
            {
                uow.CustomReactions.Add(cr);

                await uow.CompleteAsync().ConfigureAwait(false);
            }

            if (channel == null)
            {
                Array.Resize(ref _globalReactions, _globalReactions.Length + 1);
                _globalReactions[_globalReactions.Length - 1] = cr;
            }
            else
            {
                GuildReactions.AddOrUpdate(Context.Guild.Id,
                                           new CustomReaction[] { cr },
                                           (k, old) =>
                {
                    Array.Resize(ref old, old.Length + 1);
                    old[old.Length - 1] = cr;
                    return(old);
                });
            }

            await Context.Channel.EmbedAsync(new EmbedBuilder().WithOkColor()
                                             .WithTitle(GetText("new_cust_react"))
                                             .WithDescription($"#{cr.Id}")
                                             .AddField(efb => efb.WithName(GetText("trigger")).WithValue(key))
                                             .AddField(efb => efb.WithName(GetText("response")).WithValue(message))
                                             ).ConfigureAwait(false);
        }
示例#8
0
        public async Task DelCustReact(int id)
        {
            if ((Context.Guild == null && !NadekoBot.Credentials.IsOwner(Context.User)) || (Context.Guild != null && !((IGuildUser)Context.User).GuildPermissions.Administrator))
            {
                await ReplyErrorLocalized("insuff_perms").ConfigureAwait(false);

                return;
            }

            var            success = false;
            CustomReaction toDelete;

            using (var uow = DbHandler.UnitOfWork())
            {
                toDelete = uow.CustomReactions.Get(id);
                if (toDelete == null) //not found
                {
                    success = false;
                }
                else
                {
                    if ((toDelete.GuildId == null || toDelete.GuildId == 0) && Context.Guild == null)
                    {
                        uow.CustomReactions.Remove(toDelete);
                        //todo i can dramatically improve performance of this, if Ids are ordered.
                        _globalReactions = GlobalReactions.Where(cr => cr?.Id != toDelete.Id).ToArray();
                        success          = true;
                    }
                    else if ((toDelete.GuildId != null && toDelete.GuildId != 0) && (long)Context.Guild.Id == toDelete.GuildId)
                    {
                        uow.CustomReactions.Remove(toDelete);
                        GuildReactions.AddOrUpdate(Context.Guild.Id, new CustomReaction[] { }, (key, old) =>
                        {
                            return(old.Where(cr => cr?.Id != toDelete.Id).ToArray());
                        });
                        success = true;
                    }
                    if (success)
                    {
                        await uow.CompleteAsync().ConfigureAwait(false);
                    }
                }
            }

            if (success)
            {
                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)));
            }
            else
            {
                await ReplyErrorLocalized("no_found_id").ConfigureAwait(false);
            }
        }
示例#9
0
        public async Task AddCustReact(string key, [Remainder] string message)
        {
            var channel = Context.Channel as ITextChannel;

            if (string.IsNullOrWhiteSpace(message) || string.IsNullOrWhiteSpace(key))
            {
                return;
            }

            key = key.ToLowerInvariant();

            if ((channel == null && !NadekoBot.Credentials.IsOwner(Context.User)) || (channel != null && !((IGuildUser)Context.User).GuildPermissions.Administrator))
            {
                try { await Context.Channel.SendErrorAsync("Insufficient permissions. Requires Bot ownership for global custom reactions, and Administrator for guild custom reactions."); } catch { }
                return;
            }

            var cr = new CustomReaction()
            {
                GuildId  = channel?.Guild.Id,
                IsRegex  = false,
                Trigger  = key,
                Response = message,
            };

            using (var uow = DbHandler.UnitOfWork())
            {
                uow.CustomReactions.Add(cr);

                await uow.CompleteAsync().ConfigureAwait(false);
            }

            if (channel == null)
            {
                Array.Resize(ref _globalReactions, _globalReactions.Length + 1);
                _globalReactions[_globalReactions.Length - 1] = cr;
            }
            else
            {
                var reactions = GuildReactions.AddOrUpdate(Context.Guild.Id,
                                                           Array.Empty <CustomReaction>(),
                                                           (k, old) =>
                {
                    Array.Resize(ref old, old.Length + 1);
                    old[old.Length - 1] = cr;
                    return(old);
                });
            }

            await Context.Channel.EmbedAsync(new EmbedBuilder().WithOkColor()
                                             .WithTitle("New Custom Reaction")
                                             .WithDescription($"#{cr.Id}")
                                             .AddField(efb => efb.WithName("Trigger").WithValue(key))
                                             .AddField(efb => efb.WithName("Response").WithValue(message))
                                             ).ConfigureAwait(false);
        }
示例#10
0
        public CustomReaction TryGetCustomReaction(IUserMessage umsg)
        {
            if (!(umsg.Channel is SocketTextChannel channel))
            {
                return(null);
            }

            var content = umsg.Content.Trim().ToLowerInvariant();

            if (GuildReactions.TryGetValue(channel.Guild.Id, out var reactions) && reactions != null && reactions.Any())
            {
                var rs = reactions.Where(cr => {
                    if (cr == null)
                    {
                        return(false);
                    }

                    var hasTarget = cr.Response.ToLowerInvariant().Contains("%target%");
                    var trigger   = cr.TriggerWithContext(umsg, _client).Trim().ToLowerInvariant();
                    return(cr.ContainsAnywhere && content.GetWordPosition(trigger) != WordPosition.None ||
                           hasTarget && content.StartsWith(trigger + " ") ||
                           _bc.BotConfig.CustomReactionsStartWith && content.StartsWith(trigger + " ") ||
                           content == trigger);
                }).ToArray();

                if (rs.Length != 0)
                {
                    var reaction = rs[new NadekoRandom().Next(0, rs.Length)];
                    if (reaction != null)
                    {
                        return(reaction.Response == "-" ? null : reaction);
                    }
                }
            }

            var grs = GlobalReactions.Where(cr =>
            {
                if (cr == null)
                {
                    return(false);
                }
                var hasTarget = cr.Response.ToLowerInvariant().Contains("%target%");
                var trigger   = cr.TriggerWithContext(umsg, _client).Trim().ToLowerInvariant();
                return(hasTarget && content.StartsWith(trigger + " ") || _bc.BotConfig.CustomReactionsStartWith && content.StartsWith(trigger + " ") || content == trigger);
            }).ToArray();

            if (grs.Length == 0)
            {
                return(null);
            }
            var greaction = grs[new NadekoRandom().Next(0, grs.Length)];

            return(greaction);
        }
示例#11
0
        public CustomReaction TryGetCustomReaction(IUserMessage umsg)
        {
            var channel = umsg.Channel as SocketTextChannel;

            if (channel == null)
            {
                return(null);
            }

            var content = umsg.Content.Trim().ToLowerInvariant();

            if (GuildReactions.TryGetValue(channel.Guild.Id, out CustomReaction[] reactions))
示例#12
0
        public async Task CrAd(int id)
        {
            if ((Context.Guild == null && !NadekoBot.Credentials.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 = GlobalReactions;
            }
            else
            {
                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.AutoDeleteTrigger = !reaction.AutoDeleteTrigger;

                using (var uow = DbHandler.UnitOfWork())
                {
                    uow.CustomReactions.Get(id).AutoDeleteTrigger = setValue;
                    uow.Complete();
                }

                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);
            }
        }
        private Task Bot_JoinedGuild(GuildConfig gc)
        {
            var _ = Task.Run(() =>
            {
                using (var uow = _db.UnitOfWork)
                {
                    var crs = uow.CustomReactions.For(gc.GuildId);
                    GuildReactions.AddOrUpdate(gc.GuildId, crs, (key, old) => crs);
                }
            });

            return(Task.CompletedTask);
        }
示例#14
0
        public async Task DelCustReact(int id)
        {
            if ((Context.Guild == null && !NadekoBot.Credentials.IsOwner(Context.User)) || (Context.Guild != null && !((IGuildUser)Context.User).GuildPermissions.Administrator))
            {
                try { await Context.Channel.SendErrorAsync("Insufficient permissions. Requires Bot ownership for global custom reactions, and Administrator for guild custom reactions."); } catch { }
                return;
            }

            var            success = false;
            CustomReaction toDelete;

            using (var uow = DbHandler.UnitOfWork())
            {
                toDelete = uow.CustomReactions.Get(id);
                if (toDelete == null) //not found
                {
                    return;
                }

                if ((toDelete.GuildId == null || toDelete.GuildId == 0) && Context.Guild == null)
                {
                    uow.CustomReactions.Remove(toDelete);
                    //todo i can dramatically improve performance of this, if Ids are ordered.
                    _globalReactions = GlobalReactions.Where(cr => cr?.Id != toDelete.Id).ToArray();
                    success          = true;
                }
                else if ((toDelete.GuildId != null && toDelete.GuildId != 0) && Context.Guild.Id == toDelete.GuildId)
                {
                    uow.CustomReactions.Remove(toDelete);
                    GuildReactions.AddOrUpdate(Context.Guild.Id, new CustomReaction[] { }, (key, old) =>
                    {
                        return(old.Where(cr => cr?.Id != toDelete.Id).ToArray());
                    });
                    success = true;
                }
                if (success)
                {
                    await uow.CompleteAsync().ConfigureAwait(false);
                }
            }

            if (success)
            {
                await Context.Channel.SendConfirmAsync("Deleted custom reaction", toDelete.ToString()).ConfigureAwait(false);
            }
            else
            {
                await Context.Channel.SendErrorAsync("Failed to find that custom reaction.").ConfigureAwait(false);
            }
        }
示例#15
0
        public async Task AddCustReact(IUserMessage imsg, string key, [Remainder] string message)
        {
            var channel = imsg.Channel as ITextChannel;

            if (string.IsNullOrWhiteSpace(message) || string.IsNullOrWhiteSpace(key))
            {
                return;
            }

            key = key.ToLowerInvariant();

            if ((channel == null && !NadekoBot.Credentials.IsOwner(imsg.Author)) || (channel != null && !((IGuildUser)imsg.Author).GuildPermissions.Administrator))
            {
                try { await imsg.Channel.SendErrorAsync("Insufficient permissions. Requires Bot ownership for global custom reactions, and Administrator for guild custom reactions."); } catch { }
                return;
            }

            var cr = new CustomReaction()
            {
                GuildId  = channel?.Guild.Id,
                IsRegex  = false,
                Trigger  = key,
                Response = message,
            };

            using (var uow = DbHandler.UnitOfWork())
            {
                uow.CustomReactions.Add(cr);

                await uow.CompleteAsync().ConfigureAwait(false);
            }

            if (channel == null)
            {
                GlobalReactions.Add(cr);
            }
            else
            {
                var reactions = GuildReactions.GetOrAdd(channel.Guild.Id, new ConcurrentHashSet <CustomReaction>());
                reactions.Add(cr);
            }

            await imsg.Channel.EmbedAsync(new EmbedBuilder().WithColor(NadekoBot.OkColor)
                                          .WithTitle("New Custom Reaction")
                                          .WithDescription($"#{cr.Id}")
                                          .AddField(efb => efb.WithName("Trigger").WithValue(key))
                                          .AddField(efb => efb.WithName("Response").WithValue(message))
                                          .Build()).ConfigureAwait(false);
        }
示例#16
0
        public async Task DelCustReact(IUserMessage imsg, int id)
        {
            var channel = imsg.Channel as ITextChannel;

            if ((channel == null && !NadekoBot.Credentials.IsOwner(imsg.Author)) || (channel != null && !((IGuildUser)imsg.Author).GuildPermissions.Administrator))
            {
                try { await imsg.Channel.SendErrorAsync("Insufficient permissions. Requires Bot ownership for global custom reactions, and Administrator for guild custom reactions."); } catch { }
                return;
            }

            var            success = false;
            CustomReaction toDelete;

            using (var uow = DbHandler.UnitOfWork())
            {
                toDelete = uow.CustomReactions.Get(id);
                if (toDelete == null) //not found
                {
                    return;
                }

                if ((toDelete.GuildId == null || toDelete.GuildId == 0) && channel == null)
                {
                    uow.CustomReactions.Remove(toDelete);
                    GlobalReactions.RemoveWhere(cr => cr.Id == toDelete.Id);
                    success = true;
                }
                else if ((toDelete.GuildId != null && toDelete.GuildId != 0) && channel?.Guild.Id == toDelete.GuildId)
                {
                    uow.CustomReactions.Remove(toDelete);
                    GuildReactions.GetOrAdd(channel.Guild.Id, new ConcurrentHashSet <CustomReaction>()).RemoveWhere(cr => cr.Id == toDelete.Id);
                    success = true;
                }
                if (success)
                {
                    await uow.CompleteAsync().ConfigureAwait(false);
                }
            }

            if (success)
            {
                await imsg.Channel.SendConfirmAsync("Deleted custom reaction", toDelete.ToString()).ConfigureAwait(false);
            }
            else
            {
                await imsg.Channel.SendErrorAsync("Failed to find that custom reaction.").ConfigureAwait(false);
            }
        }
示例#17
0
        public static async Task <bool> TryExecuteCustomReaction(IUserMessage umsg)
        {
            var channel = umsg.Channel as ITextChannel;

            if (channel == null)
            {
                return(false);
            }

            var content = umsg.Content.Trim().ToLowerInvariant();
            ConcurrentHashSet <CustomReaction> reactions;

            GuildReactions.TryGetValue(channel.Guild.Id, out reactions);
            if (reactions != null && reactions.Any())
            {
                var reaction = reactions.Where(cr =>
                {
                    var hasTarget = cr.Response.ToLowerInvariant().Contains("%target%");
                    var trigger   = cr.TriggerWithContext(umsg).Trim().ToLowerInvariant();
                    return((hasTarget && content.StartsWith(trigger + " ")) || content == trigger);
                }).Shuffle().FirstOrDefault();
                if (reaction != null)
                {
                    if (reaction.Response != "-")
                    {
                        try { await channel.SendMessageAsync(reaction.ResponseWithContext(umsg)).ConfigureAwait(false); } catch { }
                    }

                    ReactionStats.AddOrUpdate(reaction.Trigger, 1, (k, old) => ++ old);
                    return(true);
                }
            }
            var greaction = GlobalReactions.Where(cr =>
            {
                var hasTarget = cr.Response.ToLowerInvariant().Contains("%target%");
                var trigger   = cr.TriggerWithContext(umsg).Trim().ToLowerInvariant();
                return((hasTarget && content.StartsWith(trigger + " ")) || content == trigger);
            }).Shuffle().FirstOrDefault();

            if (greaction != null)
            {
                try { await channel.SendMessageAsync(greaction.ResponseWithContext(umsg)).ConfigureAwait(false); } catch { }
                ReactionStats.AddOrUpdate(greaction.Trigger, 1, (k, old) => ++ old);
                return(true);
            }
            return(false);
        }
示例#18
0
        public async Task ListCustReact(int page = 1)
        {
            if (page < 1 || page > 1000)
            {
                return;
            }
            CustomReaction[] customReactions;
            if (Context.Guild == null)
            {
                customReactions = GlobalReactions.Where(cr => cr != null).ToArray();
            }
            else
            {
                customReactions = GuildReactions.GetOrAdd(Context.Guild.Id, Array.Empty <CustomReaction>()).Where(cr => cr != null).ToArray();
            }

            if (customReactions == null || !customReactions.Any())
            {
                await ReplyErrorLocalized("no_found").ConfigureAwait(false);

                return;
            }

            var lastPage = customReactions.Length / 20;
            await Context.Channel.SendPaginatedConfirmAsync(page, curPage =>
                                                            new EmbedBuilder().WithOkColor()
                                                            .WithTitle(GetText("name"))
                                                            .WithDescription(string.Join("\n", customReactions.OrderBy(cr => cr.Trigger)
                                                                                         .Skip((curPage - 1) * 20)
                                                                                         .Take(20)
                                                                                         .Select(cr =>
            {
                var str = $"`#{cr.Id}` {cr.Trigger}";
                if (cr.AutoDeleteTrigger)
                {
                    str = "🗑" + str;
                }
                if (cr.DmResponse)
                {
                    str = "📪" + str;
                }
                return(str);
            }))), lastPage)
            .ConfigureAwait(false);
        }
示例#19
0
        public async Task ListCustReactG(int page = 1)
        {
            if (page < 1 || page > 10000)
            {
                return;
            }
            ConcurrentHashSet <CustomReaction> customReactions;

            if (Context.Guild == null)
            {
                customReactions = GlobalReactions;
            }
            else
            {
                customReactions = GuildReactions.GetOrAdd(Context.Guild.Id, new ConcurrentHashSet <CustomReaction>());
            }

            if (customReactions == null || !customReactions.Any())
            {
                await Context.Channel.SendErrorAsync("No custom reactions found").ConfigureAwait(false);
            }
            else
            {
                var ordered = customReactions
                              .GroupBy(cr => cr.Trigger)
                              .OrderBy(cr => cr.Key)
                              .ToList();

                var lastPage = ordered.Count / 20;
                await Context.Channel.SendPaginatedConfirmAsync(page, (curPage) =>
                                                                new EmbedBuilder().WithOkColor()
                                                                .WithTitle($"Custom Reactions (grouped)")
                                                                .WithDescription(string.Join("\r\n", ordered
                                                                                             .Skip((curPage - 1) * 20)
                                                                                             .Take(20)
                                                                                             .Select(cr => $"**{cr.Key.Trim().ToLowerInvariant()}** `x{cr.Count()}`"))), lastPage)
                .ConfigureAwait(false);
            }
        }
示例#20
0
        public async Task ListCustReact(IUserMessage imsg, All x)
        {
            var channel = imsg.Channel as ITextChannel;

            ConcurrentHashSet <CustomReaction> customReactions;

            if (channel == null)
            {
                customReactions = GlobalReactions;
            }
            else
            {
                customReactions = GuildReactions.GetOrAdd(channel.Guild.Id, new ConcurrentHashSet <CustomReaction>());
            }

            if (customReactions == null || !customReactions.Any())
            {
                await imsg.Channel.SendErrorAsync("No custom reactions found").ConfigureAwait(false);
            }
            else
            {
                var txtStream = await customReactions.GroupBy(cr => cr.Trigger)
                                .OrderBy(cr => cr.Key)
                                .Select(cr => new { Trigger = cr.Key, Responses = cr.Select(y => y.Response).ToList() })
                                .ToJson()
                                .ToStream()
                                .ConfigureAwait(false);

                if (channel == null) // its a private one, just send back
                {
                    await imsg.Channel.SendFileAsync(txtStream, "customreactions.txt", "List of all custom reactions").ConfigureAwait(false);
                }
                else
                {
                    await((IGuildUser)imsg.Author).SendFileAsync(txtStream, "customreactions.txt", "List of all custom reactions").ConfigureAwait(false);
                }
            }
        }
示例#21
0
        public async Task ListCustReactG(int page = 1)
        {
            if (page < 1 || page > 10000)
            {
                return;
            }
            CustomReaction[] customReactions;
            if (Context.Guild == null)
            {
                customReactions = GlobalReactions.Where(cr => cr != null).ToArray();
            }
            else
            {
                customReactions = GuildReactions.GetOrAdd(Context.Guild.Id, new CustomReaction[] { }).Where(cr => cr != null).ToArray();
            }

            if (customReactions == null || !customReactions.Any())
            {
                await ReplyErrorLocalized("no_found").ConfigureAwait(false);
            }
            else
            {
                var ordered = customReactions
                              .GroupBy(cr => cr.Trigger)
                              .OrderBy(cr => cr.Key)
                              .ToList();

                var lastPage = ordered.Count / 20;
                await Context.Channel.SendPaginatedConfirmAsync(page, (curPage) =>
                                                                new EmbedBuilder().WithOkColor()
                                                                .WithTitle(GetText("name"))
                                                                .WithDescription(string.Join("\r\n", ordered
                                                                                             .Skip((curPage - 1) * 20)
                                                                                             .Take(20)
                                                                                             .Select(cr => $"**{cr.Key.Trim().ToLowerInvariant()}** `x{cr.Count()}`"))), lastPage)
                .ConfigureAwait(false);
            }
        }
 private Task _client_LeftGuild(SocketGuild arg)
 {
     GuildReactions.TryRemove(arg.Id, out _);
     return(Task.CompletedTask);
 }
示例#23
0
        public static CustomReaction TryGetCustomReaction(IUserMessage umsg)
        {
            var channel = umsg.Channel as SocketTextChannel;

            if (channel == null)
            {
                return(null);
            }

            var content = umsg.Content.Trim().ToLowerInvariant();

            CustomReaction[] reactions;

            GuildReactions.TryGetValue(channel.Guild.Id, out reactions);
            if (reactions != null && reactions.Any())
            {
                var rs = reactions.Where(cr =>
                {
                    if (cr == null)
                    {
                        return(false);
                    }

                    var hasTarget = cr.Response.ToLowerInvariant().Contains("%target%");
                    var trigger   = cr.TriggerWithContext(umsg).Trim().ToLowerInvariant();
                    return((hasTarget && content.StartsWith(trigger + " ")) || content == trigger);
                }).ToArray();

                if (rs.Length != 0)
                {
                    var reaction = rs[new NadekoRandom().Next(0, rs.Length)];
                    if (reaction != null)
                    {
                        if (reaction.Response == "-")
                        {
                            return(null);
                        }
                        return(reaction);
                    }
                }
            }

            var grs = GlobalReactions.Where(cr =>
            {
                if (cr == null)
                {
                    return(false);
                }
                var hasTarget = cr.Response.ToLowerInvariant().Contains("%target%");
                var trigger   = cr.TriggerWithContext(umsg).Trim().ToLowerInvariant();
                return((hasTarget && content.StartsWith(trigger + " ")) || content == trigger);
            }).ToArray();

            if (grs.Length == 0)
            {
                return(null);
            }
            var greaction = grs[new NadekoRandom().Next(0, grs.Length)];

            return(greaction);
        }
示例#24
0
        public static async Task <bool> TryExecuteCustomReaction(SocketUserMessage umsg)
        {
            var channel = umsg.Channel as SocketTextChannel;

            if (channel == null)
            {
                return(false);
            }

            var content = umsg.Content.Trim().ToLowerInvariant();

            CustomReaction[] reactions;

            GuildReactions.TryGetValue(channel.Guild.Id, out reactions);
            if (reactions != null && reactions.Any())
            {
                var rs = reactions.Where(cr =>
                {
                    if (cr == null)
                    {
                        return(false);
                    }

                    var hasTarget = cr.Response.ToLowerInvariant().Contains("%target%");
                    var trigger   = cr.TriggerWithContext(umsg).Trim().ToLowerInvariant();
                    return((hasTarget && content.StartsWith(trigger + " ")) || content == trigger);
                }).ToArray();

                if (rs.Length != 0)
                {
                    var reaction = rs[new NadekoRandom().Next(0, rs.Length)];
                    if (reaction != null)
                    {
                        if (reaction.Response != "-")
                        {
                            CREmbed crembed;
                            if (CREmbed.TryParse(reaction.Response, out crembed))
                            {
                                try { await channel.EmbedAsync(crembed.ToEmbed(), crembed.PlainText ?? "").ConfigureAwait(false); }
                                catch (Exception ex)
                                {
                                    _log.Warn("Sending CREmbed failed");
                                    _log.Warn(ex);
                                }
                            }
                            else
                            {
                                try { await channel.SendMessageAsync(reaction.ResponseWithContext(umsg)).ConfigureAwait(false); } catch { }
                            }
                        }

                        ReactionStats.AddOrUpdate(reaction.Trigger, 1, (k, old) => ++ old);
                        return(true);
                    }
                }
            }

            var grs = GlobalReactions.Where(cr =>
            {
                if (cr == null)
                {
                    return(false);
                }
                var hasTarget = cr.Response.ToLowerInvariant().Contains("%target%");
                var trigger   = cr.TriggerWithContext(umsg).Trim().ToLowerInvariant();
                return((hasTarget && content.StartsWith(trigger + " ")) || content == trigger);
            }).ToArray();

            if (grs.Length == 0)
            {
                return(false);
            }
            var greaction = grs[new NadekoRandom().Next(0, grs.Length)];

            if (greaction != null)
            {
                CREmbed crembed;
                if (CREmbed.TryParse(greaction.Response, out crembed))
                {
                    try { await channel.EmbedAsync(crembed.ToEmbed(), crembed.PlainText ?? "").ConfigureAwait(false); }
                    catch (Exception ex)
                    {
                        _log.Warn("Sending CREmbed failed");
                        _log.Warn(ex);
                    }
                }
                else
                {
                    try { await channel.SendMessageAsync(greaction.ResponseWithContext(umsg)).ConfigureAwait(false); } catch { }
                }
                ReactionStats.AddOrUpdate(greaction.Trigger, 1, (k, old) => ++ old);
                return(true);
            }
            return(false);
        }