discord: add function to register all commands.

* Add a `DiscordSlashCommand.register_slash_commands!` method to register
  all slash commands with the Discord API.
* Allow registering global commands.
* Refactor slash commands to use class attributes for the command
  name, description, and options.
This commit is contained in:
evazion
2021-03-18 22:50:44 -05:00
parent f75b1ddb4a
commit d5903b61c4
7 changed files with 94 additions and 127 deletions

View File

@@ -1,21 +1,13 @@
class DiscordSlashCommand
class CountCommand < DiscordSlashCommand
def name
"count"
end
def description
"Do a tag search and return the number of results"
end
def options
[{
name: "tags",
description: "The tags to search",
required: true,
type: ApplicationCommandOptionType::String
}]
end
self.name = "count"
self.description = "Do a tag search and return the number of results"
self.options = [{
name: "tags",
description: "The tags to search",
required: true,
type: ApplicationCommandOptionType::String
}]
def call
tags = params[:tags]

View File

@@ -2,28 +2,21 @@ class DiscordSlashCommand
class PostsCommand < DiscordSlashCommand
extend Memoist
def name
"posts"
end
self.name = "posts"
self.description = "Do a tag search"
def description
"Do a tag search"
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
self.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
}
]
def call
tags = params[:tags]

View File

@@ -1,27 +1,19 @@
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
self.name = "random"
self.description = "Show a random post"
self.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
}
]
def call
tags = params[:tags]

View File

@@ -1,21 +1,13 @@
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
self.name = "time"
self.description = "Show the current time around the world"
self.options = [{
name: "name",
description: "The name of the country to show",
required: false,
type: ApplicationCommandOptionType::String
}]
def call
name = params[:name]

View File

@@ -2,24 +2,16 @@ 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
self.name = "wiki"
self.description = "Show a wiki page"
self.options = [
{
name: "name",
description: "The name of the wiki page",
required: true,
type: ApplicationCommandOptionType::String
},
]
def call
if wiki_page.nil?