discord: add /wiki command.
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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/)
|
||||
|
||||
83
app/logical/discord_slash_command/wiki_command.rb
Normal file
83
app/logical/discord_slash_command/wiki_command.rb
Normal 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
|
||||
Reference in New Issue
Block a user