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)


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

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.



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

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



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

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

#delete_message(channel_id:, message_id:) ⇒ Object



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

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

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



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

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



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

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

#mention(user_id) ⇒ Object



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

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



119
120
121
# File 'lib/chat_sdk/discord/adapter.rb', line 119

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

#parse_events(rack_request) ⇒ Object



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

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



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

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



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

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

#render(postable_message) ⇒ Object



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

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

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



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

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



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 @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