Class: Clacky::Channel::Adapters::Discord::Adapter

Inherits:
Base
  • Object
show all
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

Instance Method Summary collapse

Methods inherited from Base

#platform_id

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_keysObject



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_idObject



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.message }
rescue StandardError => e
  { ok: false, error: e.message }
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.message}")
  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.send_message(chat_id, text, reply_to: reply_to)
  { message_id: res["id"] }
rescue ApiClient::ApiError => e
  Clacky::Logger.error("[DiscordAdapter] send_text failed: #{e.message}")
  { 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(&on_message)
  @running    = true
  @on_message = 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.message}")
    return
  end

  @gateway.start do |evt|
    handle_gateway_event(evt)
  end
rescue GatewayClient::AuthError => e
  Clacky::Logger.error("[DiscordAdapter] Authentication failed, not retrying: #{e.message}")
end

#stopObject



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

Returns:

  • (Boolean)


104
105
106
# File 'lib/clacky/server/channel/adapters/discord/adapter.rb', line 104

def supports_message_updates?
  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 update_message(chat_id, message_id, text)
  @api.edit_message(chat_id, message_id, text)
  true
rescue ApiClient::ApiError => e
  Clacky::Logger.warn("[DiscordAdapter] update_message failed: #{e.message}")
  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