Example #1
0
        public async ValueTask <AdminCommandResult> ModifySuggestionAsync(int id, [Remainder] string reason = null)
        {
            var suggestion = await Context.Database.Suggestions.FindAsync(id);

            if (suggestion?.GuildId != Context.Guild.Id)
            {
                return(CommandErrorLocalized("suggestion_notfound"));
            }

            if (!(await Context.Database.GetLoggingChannelAsync(Context.Guild.Id, LogType.SuggestionArchive) is { }
                  archiveChannel))
            {
                return(CommandErrorLocalized("suggestion_noarchive"));
            }

            IUserMessage message = null;
            int          upvotes = 0, downvotes = 0;

            if (await Context.Database.GetLoggingChannelAsync(Context.Guild.Id, LogType.Suggestion) is { }
                suggestionChannel)
            {
                try
                {
                    message = (IUserMessage)await suggestionChannel.GetMessageAsync(suggestion.MessageId);

                    var upvote = (await Context.Database.SpecialEmojis.FindAsync(Context.Guild.Id.RawValue, EmojiType.Upvote))?.Emoji ??
                                 EmojiTools.Upvote;
                    var downvote = (await Context.Database.SpecialEmojis.FindAsync(Context.Guild.Id.RawValue, EmojiType.Downvote))?.Emoji ??
                                   EmojiTools.Downvote;

                    upvotes   = Math.Max((await message.GetReactionsAsync(upvote, int.MaxValue)).Count - 1, 0);
                    downvotes = Math.Max((await message.GetReactionsAsync(downvote, int.MaxValue)).Count - 1, 0);
                }
                catch { /* ignored */ }
            }

            var author = await Context.Client.GetOrDownloadUserAsync(suggestion.UserId);

            var builder = new LocalEmbedBuilder()
                          .WithAuthor(Context.Localize(Context.Alias.Equals("approve")
                        ? "suggestion_approved_title"
                        : "suggestion_denied_title", suggestion.Id, author.Tag, upvotes, downvotes),
                                      author.GetAvatarUrl())
                          .WithDescription(suggestion.Text)
                          .WithFooter(Context.Localize(Context.Alias.Equals("approve")
                        ? "suggestion_approved_footer"
                        : "suggestion_denied_footer", Context.User.Tag),
                                      Context.User.GetAvatarUrl());

            if (Context.Alias.Equals("approve"))
            {
                builder.WithSuccessColor();
            }
            else
            {
                builder.WithErrorColor();
            }

            if (!string.IsNullOrWhiteSpace(reason))
            {
                builder.AddField(Context.Localize("title_reason"), reason);
            }

            if (suggestion.Format != ImageFormat.Default)
            {
                await archiveChannel.SendMessageAsync(new LocalAttachment(suggestion.Image, $"attachment.{suggestion.Format.ToString().ToLower()}"),
                                                      embed : builder.WithImageUrl($"attachment://attachment.{suggestion.Format.ToString().ToLower()}").Build());
            }
            else
            {
                await archiveChannel.SendMessageAsync(embed : builder.Build());
            }

            _ = message?.DeleteAsync();

            Context.Database.Suggestions.Remove(suggestion);
            await Context.Database.SaveChangesAsync();

            return(CommandSuccessLocalized(Context.Alias.Equals("approve")
                ? "suggestion_approved"
                : "suggestion_denied"));
        }