Ejemplo n.º 1
0
        public async Task CrDm(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.DmResponse = !reaction.DmResponse;

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

                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);
            }
        }
Ejemplo n.º 2
0
        public async Task CrAd(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.AutoDeleteTrigger = !reaction.AutoDeleteTrigger;

                using (var uow = _db.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);
            }
        }
Ejemplo n.º 3
0
 public static ExportedExpr FromModel(CustomReaction cr)
 => new ExportedExpr()
 {
     Res   = cr.Response,
     Ad    = cr.AutoDeleteTrigger,
     At    = cr.AllowTarget,
     Ca    = cr.ContainsAnywhere,
     Dm    = cr.DmResponse,
     React = string.IsNullOrWhiteSpace(cr.Reactions)
             ? null
             : cr.GetReactions(),
 };
Ejemplo n.º 4
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 && !_creds.IsOwner(Context.User)) || (channel != null && !((IGuildUser)Context.User).GuildPermissions.Administrator))
            {
                await ReplyErrorLocalized("insuff_perms").ConfigureAwait(false);
                return;
            }

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

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

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

            if (channel == null)
            {
                await _service.AddGcr(cr).ConfigureAwait(false);
            }
            else
            {
                _service.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.Length > 1024 ? GetText("redacted_too_long") : message))
                ).ConfigureAwait(false);
        }
Ejemplo n.º 5
0
        public static async Task <IUserMessage> Send(this CustomReaction cr, IUserMessage context)
        {
            var channel = cr.DmResponse ? await context.Author.CreateDMChannelAsync() : context.Channel;

            CustomReactions.ReactionStats.AddOrUpdate(cr.Trigger, 1, (k, old) => ++ old);

            CREmbed crembed;

            if (CREmbed.TryParse(cr.Response, out crembed))
            {
                return(await channel.EmbedAsync(crembed.ToEmbed(), crembed.PlainText ?? ""));
            }
            return(await channel.SendMessageAsync(cr.ResponseWithContext(context).SanitizeMentions()));
        }
Ejemplo n.º 6
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);
        }
        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;

                await _service.SetCrDmAsync(reaction.Id, setValue).ConfigureAwait(false);

                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);
            }
        }
Ejemplo n.º 8
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.SendMessageAsync("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.SendMessageAsync($"`Added new custom reaction {cr.Id}:`\n\t`Trigger:` {key}\n\t`Response:` {message}").ConfigureAwait(false);
        }
Ejemplo n.º 9
0
        public Task PublishEditedGcr(CustomReaction cr)
        {
            // don't publish changes of server-specific crs
            // as other shards no longer have them, nor need them
            if (cr.GuildId != 0 && cr.GuildId != null)
            {
                return(Task.CompletedTask);
            }

            var sub  = _cache.Redis.GetSubscriber();
            var data = new
            {
                Id  = cr.Id,
                Res = cr.Response,
                Ad  = cr.AutoDeleteTrigger,
                Dm  = cr.DmResponse,
                Ca  = cr.ContainsAnywhere
            };

            return(sub.PublishAsync(_client.CurrentUser.Id + "_gcr.edited", JsonConvert.SerializeObject(data)));
        }
Ejemplo n.º 10
0
        public static async Task <IUserMessage> Send(this CustomReaction cr, IUserMessage ctx, DiscordSocketClient client, bool sanitize)
        {
            var channel = cr.DmResponse ? await ctx.Author.GetOrCreateDMChannelAsync().ConfigureAwait(false) : ctx.Channel;

            if (CREmbed.TryParse(cr.Response, out CREmbed crembed))
            {
                var trigger        = cr.Trigger.ResolveTriggerString(ctx, client);
                var substringIndex = trigger.Length;
                if (cr.ContainsAnywhere)
                {
                    var pos = ctx.Content.AsSpan().GetWordPosition(trigger);
                    if (pos == WordPosition.Start)
                    {
                        substringIndex += 1;
                    }
                    else if (pos == WordPosition.End)
                    {
                        substringIndex = ctx.Content.Length;
                    }
                    else if (pos == WordPosition.Middle)
                    {
                        substringIndex += ctx.Content.IndexOf(trigger, StringComparison.InvariantCulture);
                    }
                }

                var canMentionEveryone = (ctx.Author as IGuildUser)?.GuildPermissions.MentionEveryone ?? true;

                var rep = new ReplacementBuilder()
                          .WithDefault(ctx.Author, ctx.Channel, (ctx.Channel as ITextChannel)?.Guild as SocketGuild, client)
                          .WithOverride("%target%", () => canMentionEveryone
                        ? ctx.Content.Substring(substringIndex).Trim()
                        : ctx.Content.Substring(substringIndex).Trim().SanitizeMentions(true))
                          .Build();

                rep.Replace(crembed);

                return(await channel.EmbedAsync(crembed, sanitize).ConfigureAwait(false));
            }
            return(await channel.SendMessageAsync((await cr.ResponseWithContextAsync(ctx, client, cr.ContainsAnywhere).ConfigureAwait(false)).SanitizeMentions(sanitize)).ConfigureAwait(false));
        }
Ejemplo n.º 11
0
        public static async Task <IUserMessage> Send(this CustomReaction cr, IUserMessage ctx, DiscordSocketClient client, CustomReactionsService crs)
        {
            var channel = cr.DmResponse ? await ctx.Author.CreateDMChannelAsync() : ctx.Channel;

            crs.ReactionStats.AddOrUpdate(cr.Trigger, 1, (k, old) => ++ old);

            if (!CREmbed.TryParse(cr.Response, out var crembed))
            {
                return(await channel.SendMessageAsync(cr.ResponseWithContext(ctx, client, cr.ContainsAnywhere).SanitizeMentions()));
            }
            var trigger        = cr.ResolveTriggerString(ctx, client);
            var substringIndex = trigger.Length;

            if (cr.ContainsAnywhere)
            {
                var pos = ctx.Content.GetWordPosition(trigger);
                if (pos == WordPosition.Start)
                {
                    substringIndex += 1;
                }
                else if (pos == WordPosition.End)
                {
                    substringIndex = ctx.Content.Length;
                }
                else if (pos == WordPosition.Middle)
                {
                    substringIndex += ctx.Content.IndexOf(trigger, StringComparison.Ordinal);
                }
            }

            var rep = new ReplacementBuilder()
                      .WithDefault(ctx.Author, ctx.Channel, (ctx.Channel as ITextChannel)?.Guild, client)
                      .WithOverride("%target%", () => ctx.Content.Substring(substringIndex).Trim())
                      .Build();

            rep.Replace(crembed);

            return(await channel.EmbedAsync(crembed.ToEmbedBuilder(), crembed.PlainText?.SanitizeMentions() ?? ""));
        }
Ejemplo n.º 12
0
        public Task AddGcr(CustomReaction cr)
        {
            var sub = _cache.Redis.GetSubscriber();

            return(sub.PublishAsync(_client.CurrentUser.Id + "_gcr.added", JsonConvert.SerializeObject(cr)));
        }
Ejemplo n.º 13
0
 public static Task <string> ResponseWithContextAsync(this CustomReaction cr, IUserMessage ctx, DiscordSocketClient client, bool containsAnywhere)
 => cr.Response.ResolveResponseStringAsync(ctx, client, cr.Trigger.ResolveTriggerString(ctx, client), containsAnywhere);
Ejemplo n.º 14
0
 public static string TriggerWithContext(this CustomReaction cr, IUserMessage ctx, DiscordSocketClient client)
 => cr.Trigger.ResolveTriggerString(ctx, client);
Ejemplo n.º 15
0
 public static string TriggerWithContext(this CustomReaction cr, IUserMessage ctx)
 => cr.Trigger.ResolveTriggerString(ctx);
Ejemplo n.º 16
0
 public static string ResponseWithContext(this CustomReaction cr, IUserMessage ctx)
 => cr.Response.ResolveResponseString(ctx, cr.Trigger.ResolveTriggerString(ctx));
Ejemplo n.º 17
0
 private static string ResolveTriggerString(this CustomReaction cr, IUserMessage ctx, DiscordSocketClient client)
 => new ReplacementBuilder().WithUser(ctx.Author).WithClient(client).Build().Replace(cr.Trigger);