Class: Fluxerrb::Bot
- Inherits:
-
Object
- Object
- Fluxerrb::Bot
- Defined in:
- lib/fluxerrb/client.rb
Instance Attribute Summary collapse
-
#base_url ⇒ Object
Returns the value of attribute base_url.
-
#prefix ⇒ Object
Returns the value of attribute prefix.
-
#reconnect_attempts ⇒ Object
Returns the value of attribute reconnect_attempts.
-
#token ⇒ Object
Returns the value of attribute token.
Instance Method Summary collapse
- #get_channel(channel_id) ⇒ Object
- #get_guild(guild_id) ⇒ Object
-
#initialize(config = {}) ⇒ Bot
constructor
A new instance of Bot.
- #nsfw?(channel_or_id) ⇒ Boolean
- #on(event_type, &block) ⇒ Object
- #request(method, path, body = nil) ⇒ Object
- #send_message(channel_id, content = "", embed = nil) ⇒ Object
- #server_owner?(author_id, guild_or_id) ⇒ Boolean
- #start ⇒ Object
- #user_id?(author_id, target_id) ⇒ Boolean
Constructor Details
#initialize(config = {}) ⇒ Bot
Returns a new instance of Bot.
10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/fluxerrb/client.rb', line 10 def initialize(config = {}) @base_url = config[:base_url] || 'https://api.fluxer.app/v1' @gateway_url = config[:gateway_url] || 'wss://gateway.fluxer.app/?v=1' @token = config[:token] @prefix = config[:prefix] || '!' @reconnect_attempts = 0 @max_reconnects = 5 @event_handlers = { message: [], command: [] } @sequence = nil end |
Instance Attribute Details
#base_url ⇒ Object
Returns the value of attribute base_url.
8 9 10 |
# File 'lib/fluxerrb/client.rb', line 8 def base_url @base_url end |
#prefix ⇒ Object
Returns the value of attribute prefix.
8 9 10 |
# File 'lib/fluxerrb/client.rb', line 8 def prefix @prefix end |
#reconnect_attempts ⇒ Object
Returns the value of attribute reconnect_attempts.
8 9 10 |
# File 'lib/fluxerrb/client.rb', line 8 def reconnect_attempts @reconnect_attempts end |
#token ⇒ Object
Returns the value of attribute token.
8 9 10 |
# File 'lib/fluxerrb/client.rb', line 8 def token @token end |
Instance Method Details
#get_channel(channel_id) ⇒ Object
190 191 192 |
# File 'lib/fluxerrb/client.rb', line 190 def get_channel(channel_id) request('get', "/channels/#{channel_id}") end |
#get_guild(guild_id) ⇒ Object
193 194 195 |
# File 'lib/fluxerrb/client.rb', line 193 def get_guild(guild_id) request('get', "/guilds/#{guild_id}") end |
#nsfw?(channel_or_id) ⇒ Boolean
196 197 198 199 |
# File 'lib/fluxerrb/client.rb', line 196 def nsfw?(channel_or_id) channel_data = channel_or_id.is_a?(Hash) ? channel_or_id : get_channel(channel_or_id) !!channel_data['nsfw'] end |
#on(event_type, &block) ⇒ Object
22 23 24 |
# File 'lib/fluxerrb/client.rb', line 22 def on(event_type, &block) @event_handlers[event_type] << block if @event_handlers.key?(event_type) end |
#request(method, path, body = nil) ⇒ Object
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 |
# File 'lib/fluxerrb/client.rb', line 159 def request(method, path, body = nil) uri = URI("#{@base_url}#{path}") http = Net::HTTP.new(uri.host, uri.port) if uri.scheme == 'https' http.use_ssl = true http.verify_mode = OpenSSL::SSL::VERIFY_NONE end req_class = Object.const_get("Net::HTTP::#{method.capitalize}") req = req_class.new(uri.path, { 'Authorization' => "Bot #{@token}", 'Content-Type' => 'application/json' }) req.body = body.to_json if body response = http.request(req) if response.code.to_i == 429 puts "[fluxerrb]: AN ERROR HAS OCCURED: 429 Ratelimit." @running = false exit(1) end JSON.parse(response.body) rescue response.body end |
#send_message(channel_id, content = "", embed = nil) ⇒ Object
184 185 186 187 188 189 |
# File 'lib/fluxerrb/client.rb', line 184 def (channel_id, content = "", = nil) payload = {} payload[:content] = content unless content.empty? payload[:embed] = if request('post', "/channels/#{channel_id}/messages", payload) end |
#server_owner?(author_id, guild_or_id) ⇒ Boolean
200 201 202 203 |
# File 'lib/fluxerrb/client.rb', line 200 def server_owner?(, guild_or_id) guild_data = guild_or_id.is_a?(Hash) ? guild_or_id : get_guild(guild_or_id) .to_s == guild_data['owner_id'].to_s end |
#start ⇒ Object
26 27 28 29 30 31 32 33 |
# File 'lib/fluxerrb/client.rb', line 26 def start raise "[fluxerrb]: Bot token not set" unless @token @running = true connect_gateway while @running sleep 1 end end |
#user_id?(author_id, target_id) ⇒ Boolean
204 205 206 |
# File 'lib/fluxerrb/client.rb', line 204 def user_id?(, target_id) .to_s == target_id.to_s end |