Class: ChatSDK::Discord::Adapter
- Inherits:
-
Adapter::Base
- Object
- Adapter::Base
- ChatSDK::Discord::Adapter
- Defined in:
- lib/chat_sdk/discord/adapter.rb
Instance Attribute Summary collapse
-
#client ⇒ Object
readonly
Returns the value of attribute client.
Instance Method Summary collapse
- #ack_response(rack_request) ⇒ Object
- #add_reaction(channel_id:, message_id:, emoji:) ⇒ Object
- #delete_message(channel_id:, message_id:) ⇒ Object
- #edit_message(channel_id:, message_id:, message:) ⇒ Object
- #fetch_messages(channel_id:, thread_id: nil, cursor: nil, limit: 50) ⇒ Object
- #get_user(user_id) ⇒ Object
-
#initialize(bot_token: nil, public_key: nil, application_id: nil) ⇒ Adapter
constructor
A new instance of Adapter.
- #mention(user_id) ⇒ Object
- #name ⇒ Object
- #open_dm(user_id) ⇒ Object
- #parse_events(rack_request) ⇒ Object
-
#post_message(channel_id:, message:, thread_id: nil) ⇒ Object
Outbound.
- #remove_reaction(channel_id:, message_id:, emoji:) ⇒ Object
- #render(postable_message) ⇒ Object
-
#start_gateway(&block) ⇒ Object
Discord-specific: receive real-time events via the Discord Gateway WebSocket.
- #start_typing(channel_id:, thread_id: nil) ⇒ Object
- #upload_file(channel_id:, io:, filename:, thread_id: nil, comment: nil) ⇒ Object
-
#verify_request!(rack_request) ⇒ Object
Inbound.
Constructor Details
#initialize(bot_token: nil, public_key: nil, application_id: nil) ⇒ Adapter
Returns a new instance of Adapter.
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
#client ⇒ Object (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, , emoji) end |
#delete_message(channel_id:, message_id:) ⇒ Object
104 105 106 |
# File 'lib/chat_sdk/discord/adapter.rb', line 104 def (channel_id:, message_id:) @client.(channel_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 (channel_id:, message_id:, message:) content, , components = () @client.( channel_id, , content: content, 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 (channel_id:, thread_id: nil, cursor: nil, limit: 50) = @client.(channel_id, limit: limit, before: cursor) = [] unless .is_a?(Array) = .map do |data| (data, channel_id) end next_cursor = .any? ? .last["id"] : nil [, 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 |
#name ⇒ Object
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 (channel_id:, message:, thread_id: nil) content, , components = () result = @client.( channel_id, content: content, 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, , emoji) end |
#render(postable_message) ⇒ Object
157 158 159 160 161 162 163 |
# File 'lib/chat_sdk/discord/adapter.rb', line 157 def render() if .card? @renderer.render(.card) else .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.
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. do |event| next if event..bot_account? = ChatSDK::Author.new( id: event..id.to_s, name: event..username, platform: :discord, bot: false ) = ChatSDK::Message.new( id: event..id.to_s, text: event..content, author: , thread_id: event..id.to_s, channel_id: event.channel.id.to_s, platform: :discord, raw: {content: event..content, author_id: event..id.to_s} ) mention_event = ChatSDK::Events::Mention.new( message: , thread_id: .thread_id, channel_id: .channel_id, platform: :discord, adapter_name: :discord, raw: .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") = rack_request.get_header("HTTP_X_SIGNATURE_TIMESTAMP") unless signature && raise ChatSDK::SignatureVerificationError, "Missing Discord signature headers" end Signature.verify!(@verify_key, signature, , body) end |