public async ValueTask <AdminCommandResult> EditMessageAsync(CachedTextChannel channel, ulong messageId,
                                                                     [Remainder] string text)
        {
            if (!(await channel.GetMessageAsync(messageId) is RestUserMessage message))
            {
                return(CommandErrorLocalized("utility_edit_nomessage"));
            }

            if (message.Author.Id != Context.Client.CurrentUser.Id)
            {
                return(CommandErrorLocalized("utility_edit_nonowner"));
            }

            text = await text.FormatPlaceHoldersAsync(Context, random : Random);

            await message.ModifyAsync(x =>
            {
                if (JsonEmbed.TryParse(text, out var embed))
                {
                    x.Content = embed.Text;
                    x.Embed   = embed.ToLocalEmbed();
                    return;
                }

                x.Content = text;
            });

            await Context.Message.AddReactionAsync(EmojiTools.Checkmark);

            return(CommandSuccess());
        }
Ejemplo n.º 2
0
        public async Task HandleAsync(MemberLeftEventArgs args)
        {
            using var ctx = new AdminDatabaseContext(_provider);
            var guild = await ctx.GetOrCreateGuildAsync(args.Guild.Id);

            if (string.IsNullOrWhiteSpace(guild.Goodbye))
            {
                return;
            }

            var goodbye = await guild.Goodbye.FormatPlaceHoldersAsync(
                AdminCommandContext.MockContext(guild.Language, _provider, args.User, args.Guild));

            if (!(await ctx.GetLoggingChannelAsync(args.Guild.Id, LogType.Goodbye) is { } channel))
            {
                return;
            }

            if (JsonEmbed.TryParse(goodbye, out var embed))
            {
                await channel.SendMessageAsync(embed.Text ?? string.Empty, embed : embed.ToLocalEmbed());

                return;
            }

            await channel.SendMessageAsync(goodbye);
        }
Ejemplo n.º 3
0
        public async ValueTask <AdminCommandResult> ShowTagAsync([Remainder, Lowercase] string name)
        {
            if (!(await Context.Database.Tags.FindAsync(Context.Guild.Id.RawValue, name) is { } tag))
            {
                return(CommandErrorLocalized("tag_notfound"));
            }

            tag.Uses++;
            Context.Database.Tags.Update(tag);
            await Context.Database.SaveChangesAsync();

            if (!string.IsNullOrWhiteSpace(tag.Response))
            {
                tag.Response = await tag.Response.FormatPlaceHoldersAsync(Context);
            }

            if (JsonEmbed.TryParse(tag.Response ?? string.Empty, out var embed))
            {
                return(CommandSuccess(embed.Text, embed.ToLocalEmbed(),
                                      tag.Format != ImageFormat.Default
                        ? new LocalAttachment(tag.Image, $"attachment.{tag.Format.ToString().ToLower()}")
                        : null));
            }

            return(CommandSuccess(tag.Response, attachment: tag.Format != ImageFormat.Default
                ? new LocalAttachment(tag.Image, $"attachment.{tag.Format.ToString().ToLower()}")
                : null));
        }
        public async ValueTask <AdminCommandResult> SendMessageAsync(CachedTextChannel channel, [Remainder] string text = null)
        {
            if (string.IsNullOrWhiteSpace(text) && Context.Message.Attachments.Count == 0)
            {
                return(CommandErrorLocalized("utility_send_empty"));
            }

            if (!string.IsNullOrWhiteSpace(text))
            {
                text = await text.FormatPlaceHoldersAsync(Context, random : Random);
            }

            var file     = new MemoryStream();
            var filename = string.Empty;

            if (Context.Message.Attachments.FirstOrDefault() is { } attachment)
            {
                await using var stream = await Http.GetStreamAsync(attachment.Url);

                await stream.CopyToAsync(file);

                file.Seek(0, SeekOrigin.Begin);
                filename = attachment.FileName;
            }

            if (JsonEmbed.TryParse(text, out var embed))
            {
                await channel.SendMessageAsync(
                    !string.IsNullOrWhiteSpace(filename)?new LocalAttachment(file, filename) : null, embed.Text,
                    embed : embed.ToLocalEmbed());

                return(CommandSuccess());
            }

            if (!string.IsNullOrWhiteSpace(filename))
            {
                await channel.SendMessageAsync(new LocalAttachment(file, filename), text);
            }
            else
            {
                await channel.SendMessageAsync(text);
            }
            return(CommandSuccess());
        }
Ejemplo n.º 5
0
        public async Task HandleAsync(MemberJoinedEventArgs args)
        {
            using var ctx = new AdminDatabaseContext(_provider);
            var guild = await ctx.GetOrCreateGuildAsync(args.Member.Guild.Id);

            if (string.IsNullOrWhiteSpace(guild.Greeting))
            {
                return;
            }

            var greeting = await guild.Greeting.FormatPlaceHoldersAsync(
                AdminCommandContext.MockContext(guild.Language, _provider, args.Member, args.Member.Guild));

            var hasEmbed = JsonEmbed.TryParse(greeting, out var embed);

            if (guild.DmGreeting)
            {
                if (hasEmbed)
                {
                    _ = args.Member.SendMessageAsync(embed.Text ?? string.Empty, embed: embed.ToLocalEmbed());
                }
                else
                {
                    _ = args.Member.SendMessageAsync(greeting);
                }

                return;
            }

            if (!(await ctx.GetLoggingChannelAsync(args.Member.Guild.Id, LogType.Greeting) is { } channel))
            {
                return;
            }

            if (hasEmbed)
            {
                await channel.SendMessageAsync(embed.Text ?? string.Empty, embed : embed.ToLocalEmbed());

                return;
            }

            await channel.SendMessageAsync(greeting);
        }
Ejemplo n.º 6
0
                public async ValueTask <AdminCommandResult> TriggerAsync()
                {
                    var guild = await Context.Database.GetOrCreateGuildAsync(Context.Guild.Id);

                    if (string.IsNullOrWhiteSpace(guild.Greeting))
                    {
                        return(CommandErrorLocalized("guild_greeting_empty"));
                    }

                    var channel = await Context.Database.GetLoggingChannelAsync(Context.Guild.Id,
                                                                                LogType.Greeting);

                    var greeting = await guild.Greeting.FormatPlaceHoldersAsync(
                        AdminCommandContext.MockContext(Context.Language, Provider, Context.User, Context.Guild), Context.User.Mention, Random);

                    JsonEmbed embed;

                    if (!guild.DmGreeting)
                    {
                        if (channel is null)
                        {
                            return(CommandErrorLocalized("guild_greeting_nochannel"));
                        }

                        IUserMessage message;
                        if (JsonEmbed.TryParse(greeting, out embed))
                        {
                            message = await channel.SendMessageAsync(embed.Text ?? string.Empty, embed : embed.ToLocalEmbed());
                        }
                        else
                        {
                            message = await channel.SendMessageAsync(greeting);
                        }

                        if (guild.GreetingDuration.HasValue)
                        {
                            _ = Task.Run(async() =>
                                         await Task.Delay(guild.GreetingDuration.Value)
                                         .ContinueWith(_ => message.DeleteAsync()));
                        }

                        await Context.Message.AddReactionAsync(EmojiTools.Checkmark);

                        return(CommandSuccess());
                    }

                    if (JsonEmbed.TryParse(greeting, out embed))
                    {
                        try
                        {
                            await Context.User.SendMessageAsync(embed.Text ?? string.Empty,
                                                                embed : embed.ToLocalEmbed());

                            await Context.Message.AddReactionAsync(EmojiTools.Checkmark);
                        }
                        catch (DiscordHttpException ex) when(ex.JsonErrorCode ==
                                                             JsonErrorCode.CannotSendMessagesToThisUser)
                        {
                            await Context.Message.AddReactionAsync(EmojiTools.X);
                        }
                    }
                    else
                    {
                        try
                        {
                            await Context.User.SendMessageAsync(greeting);

                            await Context.Message.AddReactionAsync(EmojiTools.Checkmark);
                        }
                        catch (DiscordHttpException ex) when(ex.JsonErrorCode ==
                                                             JsonErrorCode.CannotSendMessagesToThisUser)
                        {
                            await Context.Message.AddReactionAsync(EmojiTools.X);
                        }
                    }

                    return(CommandSuccess());
                }