Class: Clacky::Channel::Adapters::Discord::Adapter
- Defined in:
- lib/clacky/server/channel/adapters/discord/adapter.rb
Overview
Discord adapter (bot mode). Receives messages via the Gateway WebSocket and sends via the REST API.
Constant Summary collapse
- MAX_IMAGE_BYTES =
Clacky::Utils::FileProcessor::MAX_IMAGE_BYTES
Class Method Summary collapse
- .env_keys ⇒ Object
- .platform_config(data) ⇒ Object
- .platform_id ⇒ Object
- .set_env_data(data, config) ⇒ Object
- .test_connection(fields) ⇒ Object
Instance Method Summary collapse
-
#initialize(config) ⇒ Adapter
constructor
A new instance of Adapter.
- #send_file(chat_id, path, name: nil) ⇒ Object
- #send_text(chat_id, text, reply_to: nil) ⇒ Object
- #start(&on_message) ⇒ Object
- #stop ⇒ Object
- #supports_message_updates? ⇒ Boolean
- #update_message(chat_id, message_id, text) ⇒ Object
- #validate_config(config) ⇒ Object
Methods inherited from Base
Constructor Details
#initialize(config) ⇒ Adapter
Returns a new instance of Adapter.
53 54 55 56 57 58 59 60 61 |
# File 'lib/clacky/server/channel/adapters/discord/adapter.rb', line 53 def initialize(config) @config = config @bot_token = config[:bot_token] @api = ApiClient.new(bot_token: @bot_token) @gateway = GatewayClient.new(bot_token: @bot_token) @bot_user_id = nil @running = false @on_message = nil end |
Class Method Details
.env_keys ⇒ Object
22 23 24 |
# File 'lib/clacky/server/channel/adapters/discord/adapter.rb', line 22 def self.env_keys %w[IM_DISCORD_BOT_TOKEN] end |
.platform_config(data) ⇒ Object
26 27 28 29 30 |
# File 'lib/clacky/server/channel/adapters/discord/adapter.rb', line 26 def self.platform_config(data) { bot_token: data["IM_DISCORD_BOT_TOKEN"] } end |
.platform_id ⇒ Object
18 19 20 |
# File 'lib/clacky/server/channel/adapters/discord/adapter.rb', line 18 def self.platform_id :discord end |
.set_env_data(data, config) ⇒ Object
32 33 34 |
# File 'lib/clacky/server/channel/adapters/discord/adapter.rb', line 32 def self.set_env_data(data, config) data["IM_DISCORD_BOT_TOKEN"] = config[:bot_token] end |
.test_connection(fields) ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/clacky/server/channel/adapters/discord/adapter.rb', line 36 def self.test_connection(fields) bot_token = fields[:bot_token].to_s.strip return { ok: false, error: "bot_token is required" } if bot_token.empty? client = ApiClient.new(bot_token: bot_token) me = client.me if me["id"] { ok: true, message: "Connected as #{me["username"]}##{me["discriminator"]} (id=#{me["id"]})" } else { ok: false, error: "Empty response from /users/@me" } end rescue ApiClient::ApiError => e { ok: false, error: e. } rescue StandardError => e { ok: false, error: e. } end |
Instance Method Details
#send_file(chat_id, path, name: nil) ⇒ Object
108 109 110 111 112 113 |
# File 'lib/clacky/server/channel/adapters/discord/adapter.rb', line 108 def send_file(chat_id, path, name: nil) @api.send_file(chat_id, path, name: name) rescue ApiClient::ApiError => e Clacky::Logger.error("[DiscordAdapter] send_file failed: #{e.}") nil end |
#send_text(chat_id, text, reply_to: nil) ⇒ Object
88 89 90 91 92 93 94 |
# File 'lib/clacky/server/channel/adapters/discord/adapter.rb', line 88 def send_text(chat_id, text, reply_to: nil) res = @api.(chat_id, text, reply_to: reply_to) { message_id: res["id"] } rescue ApiClient::ApiError => e Clacky::Logger.error("[DiscordAdapter] send_text failed: #{e.}") { message_id: nil } end |
#start(&on_message) ⇒ Object
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/clacky/server/channel/adapters/discord/adapter.rb', line 63 def start(&) @running = true @on_message = begin me = @api.me @bot_user_id = me["id"] Clacky::Logger.info("[DiscordAdapter] authenticated as #{me["username"]} (id=#{@bot_user_id})") rescue ApiClient::ApiError => e Clacky::Logger.error("[DiscordAdapter] /users/@me failed, not retrying: #{e.}") return end @gateway.start do |evt| handle_gateway_event(evt) end rescue GatewayClient::AuthError => e Clacky::Logger.error("[DiscordAdapter] Authentication failed, not retrying: #{e.}") end |
#stop ⇒ Object
83 84 85 86 |
# File 'lib/clacky/server/channel/adapters/discord/adapter.rb', line 83 def stop @running = false @gateway.stop end |
#supports_message_updates? ⇒ Boolean
104 105 106 |
# File 'lib/clacky/server/channel/adapters/discord/adapter.rb', line 104 def true end |
#update_message(chat_id, message_id, text) ⇒ Object
96 97 98 99 100 101 102 |
# File 'lib/clacky/server/channel/adapters/discord/adapter.rb', line 96 def (chat_id, , text) @api.(chat_id, , text) true rescue ApiClient::ApiError => e Clacky::Logger.warn("[DiscordAdapter] update_message failed: #{e.}") false end |
#validate_config(config) ⇒ Object
115 116 117 118 119 |
# File 'lib/clacky/server/channel/adapters/discord/adapter.rb', line 115 def validate_config(config) errors = [] errors << "bot_token is required" if config[:bot_token].nil? || config[:bot_token].empty? errors end |