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)


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

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
  @verify_key = Ed25519::VerifyKey.new([@public_key].pack("H*")) if @public_key
  @dm_channels = {}
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



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

def client
  @client
end

Instance Method Details

#ack_response(rack_request) ⇒ Object



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

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



112
113
114
# File 'lib/chat_sdk/discord/adapter.rb', line 112

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

#delete_message(channel_id:, message_id:) ⇒ Object



104
105
106
# File 'lib/chat_sdk/discord/adapter.rb', line 104

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

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



92
93
94
95
96
97
98
99
100
101
102
# File 'lib/chat_sdk/discord/adapter.rb', line 92

def edit_message(channel_id:, message_id:, message:)
  content, embeds, components = prepare_message_payload(message)

  @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



137
138
139
140
141
142
143
144
145
146
147
# File 'lib/chat_sdk/discord/adapter.rb', line 137

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

  messages = messages_data.map do |data|
    parse_discord_message(data, channel_id)
  end

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

#get_user(user_id) ⇒ Object



120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/chat_sdk/discord/adapter.rb', line 120

def get_user(user_id)
  data = @client.get_user(user_id)
  return nil unless data && data["id"]

  ChatSDK::Author.new(
    id: data["id"],
    name: data["username"],
    platform: :discord,
    bot: data["bot"] || false,
    raw: data
  )
end

#mention(user_id) ⇒ Object



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

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

#nameObject



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

def name
  :discord
end

#open_dm(user_id) ⇒ Object



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

def open_dm(user_id)
  @dm_channels[user_id] ||= @client.create_dm(user_id)["id"]
end

#parse_events(rack_request) ⇒ Object



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

def parse_events(rack_request)
  payload = read_json_body(rack_request)
  EventParser.parse(payload)
rescue JSON::ParserError
  []
end

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

Outbound



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/chat_sdk/discord/adapter.rb', line 71

def post_message(channel_id:, message:, thread_id: nil)
  content, embeds, components = prepare_message_payload(message)

  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



116
117
118
# File 'lib/chat_sdk/discord/adapter.rb', line 116

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

#render(postable_message) ⇒ Object



157
158
159
160
161
162
163
# File 'lib/chat_sdk/discord/adapter.rb', line 157

def render(postable_message)
  if postable_message.card?
    @renderer.render(postable_message.card)
  else
    postable_message.text
  end
end

#start_gateway(&block) ⇒ Object

Discord-specific: receive real-time events via the Discord Gateway WebSocket. Requires the optional 'discordrb' gem. Not part of the base adapter contract.

Raises:

  • (ArgumentError)


167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
# File 'lib/chat_sdk/discord/adapter.rb', line 167

def start_gateway(&block)
  raise ArgumentError, "start_gateway requires a block" unless block

  begin
    require "discordrb"
  rescue LoadError
    raise ChatSDK::ConfigurationError,
      "Discord gateway requires the 'discordrb' gem. Add gem 'discordrb' to your Gemfile."
  end

  bot = Discordrb::Bot.new(token: "Bot #{@bot_token}")

  bot.message do |event|
    next if event.author.bot_account?

    author = ChatSDK::Author.new(
      id: event.author.id.to_s,
      name: event.author.username,
      platform: :discord,
      bot: false
    )
    message = ChatSDK::Message.new(
      id: event.message.id.to_s,
      text: event.message.content,
      author: author,
      thread_id: event.message.id.to_s,
      channel_id: event.channel.id.to_s,
      platform: :discord,
      raw: {content: event.message.content, author_id: event.author.id.to_s}
    )

    mention_event = ChatSDK::Events::Mention.new(
      message: message,
      thread_id: message.thread_id,
      channel_id: message.channel_id,
      platform: :discord,
      adapter_name: :discord,
      raw: message.raw
    )

    block.call(mention_event)
  end

  bot.run
end

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



149
150
151
# File 'lib/chat_sdk/discord/adapter.rb', line 149

def start_typing(channel_id:, thread_id: nil)
  @client.trigger_typing(channel_id)
end

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



108
109
110
# File 'lib/chat_sdk/discord/adapter.rb', line 108

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

#verify_request!(rack_request) ⇒ Object

Inbound



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

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

  unless @verify_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!(@verify_key, signature, timestamp, body)
end