From f219fc09ec0dcd47a6715f4121751e693973b690 Mon Sep 17 00:00:00 2001 From: evazion Date: Fri, 12 Mar 2021 22:44:57 -0600 Subject: [PATCH] discord: add /wiki command. --- app/logical/discord_slash_command.rb | 1 + .../discord_slash_command/post_embed.rb | 4 +- .../discord_slash_command/wiki_command.rb | 83 +++++++++++++++++++ 3 files changed, 86 insertions(+), 2 deletions(-) create mode 100644 app/logical/discord_slash_command/wiki_command.rb diff --git a/app/logical/discord_slash_command.rb b/app/logical/discord_slash_command.rb index eaf747728..de71f7df0 100644 --- a/app/logical/discord_slash_command.rb +++ b/app/logical/discord_slash_command.rb @@ -5,6 +5,7 @@ class DiscordSlashCommand count: DiscordSlashCommand::CountCommand, posts: DiscordSlashCommand::PostsCommand, random: DiscordSlashCommand::RandomCommand, + wiki: DiscordSlashCommand::WikiCommand, } # https://discord.com/developers/docs/interactions/slash-commands#interaction-interactiontype diff --git a/app/logical/discord_slash_command/post_embed.rb b/app/logical/discord_slash_command/post_embed.rb index 718e074e6..59d1065b8 100644 --- a/app/logical/discord_slash_command/post_embed.rb +++ b/app/logical/discord_slash_command/post_embed.rb @@ -17,12 +17,12 @@ class DiscordSlashCommand image: { width: post.image_width, height: post.image_height, - url: embed_image, + url: embed_image_url, }, } end - def embed_image + def embed_image_url if is_censored? nil elsif post.file_ext.match?(/jpe?g|png|gif/) diff --git a/app/logical/discord_slash_command/wiki_command.rb b/app/logical/discord_slash_command/wiki_command.rb new file mode 100644 index 000000000..5c52dc161 --- /dev/null +++ b/app/logical/discord_slash_command/wiki_command.rb @@ -0,0 +1,83 @@ +class DiscordSlashCommand + class WikiCommand < DiscordSlashCommand + extend Memoist + + def name + "wiki" + end + + def description + "Show a wiki page" + end + + def options + [ + { + name: "name", + description: "The name of the wiki page", + required: true, + type: ApplicationCommandOptionType::String + }, + ] + end + + def call + if wiki_page.nil? + respond_with("`#{params[:name]}` doesn't have a wiki.") + else + respond_with( + embeds: [{ + description: DText.to_markdown(wiki_page.body).truncate(500), + title: wiki_page.pretty_title, + url: Routes.url_for(wiki_page), + **example_embed, + }] + ) + end + end + + def wiki_page + WikiPage.titled(params[:name]).first + end + + def tag + wiki_page&.tag + end + + def example_post + return nil if tag.nil? || tag.empty? + + if tag.artist? + search = "#{tag.name} rating:safe" + elsif tag.copyright? + search = "#{tag.name} rating:safe everyone copytags:<5 -parody -crossover" + elsif tag.character? + search = "#{tag.name} rating:safe solo chartags:<5" + else # meta or general + search = "#{tag.name} rating:safe -animated -6+girls -comic" + end + + Post.system_tag_match(search).limit(500).sort_by(&:score).last + end + + def example_embed + return {} if example_post.nil? || example_image_url.nil? + + { + image: { + url: example_image_url, + }, + author: { + name: example_post.dtext_shortlink, + url: Routes.url_for(example_post), + } + } + end + + def example_image_url + DiscordSlashCommand::PostEmbed.new(example_post, self).embed_image_url + end + + memoize :wiki_page, :tag, :example_post, :example_embed, :example_image_url + end +end