Class: ChatSDK::Discord::Adapter

Inherits:
Adapter::Base
  • Object
show all
Defined in:
lib/chat_sdk/discord/adapter.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(bot_token: nil, public_key: nil, application_id: nil) ⇒ Adapter

Returns a new instance of Adapter.

Raises:

  • (ChatSDK::ConfigurationError)


13
14
15
16
17
18
19
20
21
22
# File 'lib/chat_sdk/discord/adapter.rb', line 13

def initialize(bot_token: nil, public_key: nil, application_id: nil)
  @bot_token = bot_token || ENV["DISCORD_BOT_TOKEN"]
  @public_key = public_key || ENV["DISCORD_PUBLIC_KEY"]
  @application_id = application_id || ENV["DISCORD_APPLICATION_ID"]

  raise ChatSDK::ConfigurationError, "Discord bot_token required" unless @bot_token

  @client = ApiClient.new(bot_token: @bot_token)
  @renderer = EmbedRenderer.new
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



11
12
13
# File 'lib/chat_sdk/discord/adapter.rb', line 11

def client
  @client
end

Instance Method Details

#ack_response(rack_request) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/chat_sdk/discord/adapter.rb', line 47

def ack_response(rack_request)
  body = rack_request.body.read
  rack_request.body.rewind

  payload = begin
    JSON.parse(body)
  rescue JSON::ParserError
    return nil
  end

  return nil unless payload["type"] == 1

  [200, {"content-type" => "application/json"}, ['{"type":1}']]
end

#add_reaction(channel_id:, message_id:, emoji:) ⇒ Object



141
142
143
144
# File 'lib/chat_sdk/discord/adapter.rb', line 141

def add_reaction(channel_id:, message_id:, emoji:)
  require_capability!(:reactions)
  @client.add_reaction(channel_id, message_id, emoji)
end

#delete_message(channel_id:, message_id:) ⇒ Object



127
128
129
130
# File 'lib/chat_sdk/discord/adapter.rb', line 127

def delete_message(channel_id:, message_id:)
  require_capability!(:delete_messages)
  @client.delete_message(channel_id, message_id)
end

#edit_message(channel_id:, message_id:, message:) ⇒ Object



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/chat_sdk/discord/adapter.rb', line 104

def edit_message(channel_id:, message_id:, message:)
  require_capability!(:edit_messages)
  msg = ChatSDK::PostableMessage.from(message)

  content = msg.text || msg.card&.fallback_text || ""
  embeds = nil
  components = nil

  if msg.card?
    rendered = @renderer.render(msg.card)
    embeds = rendered["embeds"]
    components = rendered["components"]
  end

  @client.edit_message(
    channel_id,
    message_id,
    content: content,
    embeds: embeds,
    components: components
  )
end

#fetch_messages(channel_id:, thread_id: nil, cursor: nil, limit: 50) ⇒ Object



157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/chat_sdk/discord/adapter.rb', line 157

def fetch_messages(channel_id:, thread_id: nil, cursor: nil, limit: 50)
  require_capability!(:message_history)

  messages_data = @client.get_messages(channel_id, limit: limit, before: cursor)
  messages_data = [] unless messages_data.is_a?(Array)

  messages = messages_data.map do |msg|
    ChatSDK::Message.new(
      id: msg["id"],
      text: msg["content"] || "",
      author: ChatSDK::Author.new(
        id: msg.dig("author", "id") || "unknown",
        name: msg.dig("author", "username") || "unknown",
        platform: :discord
      ),
      thread_id: msg["id"],
      channel_id: channel_id,
      platform: :discord,
      raw: msg
    )
  end

  next_cursor = messages_data.any? ? messages_data.last["id"] : nil
  [messages, next_cursor]
end

#mention(user_id) ⇒ Object



191
192
193
# File 'lib/chat_sdk/discord/adapter.rb', line 191

def mention(user_id)
  "<@#{user_id}>"
end

#nameObject



24
25
26
# File 'lib/chat_sdk/discord/adapter.rb', line 24

def name
  :discord
end

#open_dm(user_id) ⇒ Object



151
152
153
154
155
# File 'lib/chat_sdk/discord/adapter.rb', line 151

def open_dm(user_id)
  require_capability!(:direct_messages)
  result = @client.create_dm(user_id)
  result["id"]
end

#open_modal(trigger_id:, modal:) ⇒ Object



183
184
185
# File 'lib/chat_sdk/discord/adapter.rb', line 183

def open_modal(trigger_id:, modal:)
  super # raises NotSupportedError
end

#parse_events(rack_request) ⇒ Object



62
63
64
65
66
67
68
69
70
# File 'lib/chat_sdk/discord/adapter.rb', line 62

def parse_events(rack_request)
  body = rack_request.body.read
  rack_request.body.rewind

  payload = JSON.parse(body)
  EventParser.parse(payload)
rescue JSON::ParserError
  []
end

#post_ephemeral(channel_id:, user_id:, message:, thread_id: nil) ⇒ Object



132
133
134
# File 'lib/chat_sdk/discord/adapter.rb', line 132

def post_ephemeral(channel_id:, user_id:, message:, thread_id: nil)
  super # raises NotSupportedError
end

#post_message(channel_id:, message:, thread_id: nil) ⇒ Object

Outbound



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/chat_sdk/discord/adapter.rb', line 73

def post_message(channel_id:, message:, thread_id: nil)
  msg = ChatSDK::PostableMessage.from(message)

  content = msg.text || msg.card&.fallback_text || ""
  embeds = nil
  components = nil

  if msg.card?
    rendered = @renderer.render(msg.card)
    embeds = rendered["embeds"]
    components = rendered["components"]
  end

  result = @client.create_message(
    channel_id,
    content: content,
    embeds: embeds,
    components: components
  )

  ChatSDK::Message.new(
    id: result["id"],
    text: content,
    author: ChatSDK::Author.new(id: @application_id || "bot", name: "bot", platform: :discord, bot: true),
    thread_id: thread_id || result["id"],
    channel_id: channel_id,
    platform: :discord,
    raw: result
  )
end

#remove_reaction(channel_id:, message_id:, emoji:) ⇒ Object



146
147
148
149
# File 'lib/chat_sdk/discord/adapter.rb', line 146

def remove_reaction(channel_id:, message_id:, emoji:)
  require_capability!(:reactions)
  @client.remove_reaction(channel_id, message_id, emoji)
end

#render(postable_message) ⇒ Object



195
196
197
198
199
200
201
202
# File 'lib/chat_sdk/discord/adapter.rb', line 195

def render(postable_message)
  msg = ChatSDK::PostableMessage.from(postable_message)
  if msg.card?
    @renderer.render(msg.card)
  else
    msg.text
  end
end

#start_typing(channel_id:, thread_id: nil) ⇒ Object



187
188
189
# File 'lib/chat_sdk/discord/adapter.rb', line 187

def start_typing(channel_id:, thread_id: nil)
  super # raises NotSupportedError
end

#upload_file(channel_id:, io:, filename:, thread_id: nil, comment: nil) ⇒ Object



136
137
138
139
# File 'lib/chat_sdk/discord/adapter.rb', line 136

def upload_file(channel_id:, io:, filename:, thread_id: nil, comment: nil)
  require_capability!(:file_uploads)
  @client.upload_file(channel_id, io, filename)
end

#verify_request!(rack_request) ⇒ Object

Inbound



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/chat_sdk/discord/adapter.rb', line 29

def verify_request!(rack_request)
  body = rack_request.body.read
  rack_request.body.rewind

  unless @public_key
    raise ChatSDK::ConfigurationError, "Discord public_key required for signature verification"
  end

  signature = rack_request.get_header("HTTP_X_SIGNATURE_ED25519")
  timestamp = rack_request.get_header("HTTP_X_SIGNATURE_TIMESTAMP")

  unless signature && timestamp
    raise ChatSDK::SignatureVerificationError, "Missing Discord signature headers"
  end

  Signature.verify!(@public_key, signature, timestamp, body)
end