discord: add /time command.

This commit is contained in:
evazion
2021-03-18 21:44:28 -05:00
parent 1fd23c344a
commit f75b1ddb4a
2 changed files with 58 additions and 0 deletions

View File

@@ -5,6 +5,7 @@ class DiscordSlashCommand
count: DiscordSlashCommand::CountCommand,
posts: DiscordSlashCommand::PostsCommand,
random: DiscordSlashCommand::RandomCommand,
time: DiscordSlashCommand::TimeCommand,
wiki: DiscordSlashCommand::WikiCommand,
}

View File

@@ -0,0 +1,57 @@
class DiscordSlashCommand
class TimeCommand < DiscordSlashCommand
def name
"time"
end
def description
"Show the current time around the world"
end
def options
[{
name: "name",
description: "The name of the country to show",
required: false,
type: ApplicationCommandOptionType::String
}]
end
def call
name = params[:name]
if name.present?
msg = times_for_country(name)
msg = "Timezone not found: #{name}" if msg.blank?
else
msg = <<~EOS
`US (west): #{time("US/Pacific")}`
`US (east): #{time("US/Eastern")}`
`Europe: #{time("Europe/Berlin")}`
`Japan: #{time("Asia/Tokyo")}`
`Australia: #{time("Australia/Sydney")}`
EOS
end
respond_with(msg)
end
def times_for_country(country_name)
country = TZInfo::Country.all.find do |country|
country.name.downcase == country_name.downcase
end
return nil if country.nil?
zones = country.zones.group_by(&:abbr).transform_values(&:first).values
zones.map do |zone|
"`#{zone.friendly_identifier}: #{time(zone.identifier)}`"
end.join("\n")
end
# format: Thu, Nov 02 2017 6:11 PM CDT
def time(zone, format: "%a, %b %d %Y %l:%M %p %Z")
Time.use_zone(zone) { Time.current.strftime(format) }
end
end
end