Add initial support for the `/count <tags>` and `/posts <tags>` slash commands. Slash commands are basically like webhooks; we register a command, and when anybody types that command in Discord, Discord sends us a HTTP request that we respond to. * https://discord.com/developers/docs/interactions/slash-commands * https://support.discord.com/hc/en-us/articles/1500000368501-Slash-Commands-FAQ
52 lines
1.2 KiB
Ruby
52 lines
1.2 KiB
Ruby
class DiscordApiClient
|
|
extend Memoist
|
|
|
|
BASE_URL = "https://discord.com/api/v8"
|
|
|
|
attr_reader :application_id, :guild_id, :bot_token, :http
|
|
|
|
def initialize(application_id: Danbooru.config.discord_application_client_id, guild_id: Danbooru.config.discord_guild_id, bot_token: Danbooru.config.discord_bot_token, http: Danbooru::Http.new)
|
|
@application_id = application_id
|
|
@guild_id = guild_id
|
|
@bot_token = bot_token
|
|
@http = http
|
|
end
|
|
|
|
def register_slash_command(name:, description:, options: [])
|
|
json = {
|
|
name: name,
|
|
description: description,
|
|
options: options
|
|
}
|
|
|
|
post("/applications/#{application_id}/guilds/#{guild_id}/commands", json)
|
|
end
|
|
|
|
def get_channel(channel_id)
|
|
get("/channels/#{channel_id}")
|
|
end
|
|
|
|
def me
|
|
get("/users/@me")
|
|
end
|
|
|
|
def get(url)
|
|
client.get("#{BASE_URL}/#{url}").parse
|
|
end
|
|
|
|
def post(url, data)
|
|
client.post("#{BASE_URL}/#{url}", json: data).parse
|
|
end
|
|
|
|
def client
|
|
headers = {
|
|
"User-Agent": "#{Danbooru.config.canonical_app_name} (#{Danbooru.config.source_code_url}, 1.0)",
|
|
"Authorization": "Bot #{bot_token}"
|
|
}
|
|
|
|
http.headers(headers)
|
|
end
|
|
|
|
memoize :client
|
|
end
|