discord: add /random command.

This commit is contained in:
evazion
2021-03-11 21:23:20 -06:00
parent b79bd8407f
commit 698be2d0e4
8 changed files with 146 additions and 88 deletions

View File

@@ -0,0 +1,67 @@
class DiscordSlashCommand
class PostEmbed
attr_reader :post, :command
def initialize(post, command)
@post = post
@command = command
end
def to_h
{
title: post.dtext_shortlink,
url: Routes.url_for(post),
timestamp: post.created_at.iso8601,
color: embed_color,
footer: embed_footer,
image: {
width: post.image_width,
height: post.image_height,
url: embed_image,
},
}
end
def embed_image
if is_censored?
nil
elsif post.file_ext.match?(/jpe?g|png|gif/)
post.file_url
else
post.preview_file_url
end
end
def embed_color
if post.is_flagged?
0xC41C19
elsif post.is_pending?
0x0000FF
elsif post.parent_id.present?
0xC0C000
elsif post.has_active_children?
0x00FF00
elsif post.is_deleted?
0xFFFFFF
else
nil
end
end
def embed_footer
dimensions = "#{post.image_width}x#{post.image_height}"
file_size = post.file_size.to_s(:human_size, precision: 4)
text = "Rating: #{post.rating.upcase} | #{dimensions} (#{file_size} #{post.file_ext})"
{ text: text }
end
def is_censored?
post.rating != "s" && !is_nsfw_channel?
end
def is_nsfw_channel?
command.channel.fetch("nsfw")
end
end
end

View File

@@ -11,83 +11,26 @@ class DiscordSlashCommand
end
def options
[{
name: "tags",
description: "The tags to search",
required: true,
type: ApplicationCommandOptionType::String
}]
[
{
name: "tags",
description: "The tags to search",
type: ApplicationCommandOptionType::String
},
{
name: "limit",
description: "The number of posts to show (max 10)",
type: ApplicationCommandOptionType::Integer
}
]
end
def call
tags = params[:tags]
query = PostQueryBuilder.new(tags, User.anonymous).normalized_query
limit = params.fetch(:limit, 3).clamp(1, 10)
posts = Post.user_tag_match(tags, User.anonymous).limit(limit)
limit = query.find_metatag(:limit) || 3
limit = limit.to_i.clamp(1, 10)
posts = query.build.paginate(1, limit: limit)
embeds = posts.map { |post| post_embed(post) }
respond_with(embeds: embeds)
respond_with(posts: posts)
end
def post_embed(post)
{
title: post.dtext_shortlink,
url: Routes.url_for(post),
timestamp: post.created_at.iso8601,
color: post_embed_color(post),
footer: post_embed_footer(post),
image: {
width: post.image_width,
height: post.image_height,
url: post_embed_image(post),
},
}
end
def post_embed_image(post, blur: 50)
if is_censored?(post)
nil
elsif post.file_ext.match?(/jpe?g|png|gif/)
post.file_url
else
post.preview_file_url
end
end
def post_embed_color(post)
if post.is_flagged?
0xC41C19
elsif post.is_pending?
0x0000FF
elsif post.parent_id.present?
0xC0C000
elsif post.has_active_children?
0x00FF00
elsif post.is_deleted?
0xFFFFFF
else
nil
end
end
def post_embed_footer(post)
dimensions = "#{post.image_width}x#{post.image_height}"
file_size = post.file_size.to_s(:human_size, precision: 4)
text = "Rating: #{post.rating.upcase} | #{dimensions} (#{file_size} #{post.file_ext})"
{ text: text }
end
def is_censored?(post)
post.rating != "s" && !is_nsfw_channel?
end
def is_nsfw_channel?
discord.get_channel(data[:channel_id]).fetch("nsfw")
end
memoize :is_nsfw_channel?
end
end

View File

@@ -0,0 +1,34 @@
class DiscordSlashCommand
class RandomCommand < DiscordSlashCommand
def name
"random"
end
def description
"Show a random post"
end
def options
[
{
name: "tags",
description: "The tags to search",
type: ApplicationCommandOptionType::String
},
{
name: "limit",
description: "The number of posts to show (max 10)",
type: ApplicationCommandOptionType::Integer
}
]
end
def call
tags = params[:tags]
limit = params.fetch(:limit, 1).clamp(1, 10)
posts = Post.user_tag_match(tags, User.anonymous).random(limit)
respond_with(posts: posts)
end
end
end