discord: add /wiki command.

This commit is contained in:
evazion
2021-03-12 22:44:57 -06:00
parent 698be2d0e4
commit f219fc09ec
3 changed files with 86 additions and 2 deletions

View File

@@ -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

View File

@@ -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/)

View File

@@ -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