Class: Fluxerrb::Bot

Inherits:
Object
  • Object
show all
Defined in:
lib/fluxerrb/client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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_urlObject

Returns the value of attribute base_url.



8
9
10
# File 'lib/fluxerrb/client.rb', line 8

def base_url
  @base_url
end

#prefixObject

Returns the value of attribute prefix.



8
9
10
# File 'lib/fluxerrb/client.rb', line 8

def prefix
  @prefix
end

#reconnect_attemptsObject

Returns the value of attribute reconnect_attempts.



8
9
10
# File 'lib/fluxerrb/client.rb', line 8

def reconnect_attempts
  @reconnect_attempts
end

#tokenObject

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

Returns:

  • (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 send_message(channel_id, content = "", embed = nil)
	payload = {}
	payload[:content] = content unless content.empty?
	payload[:embed] = embed if embed
	request('post', "/channels/#{channel_id}/messages", payload)
end

#server_owner?(author_id, guild_or_id) ⇒ Boolean

Returns:

  • (Boolean)


200
201
202
203
# File 'lib/fluxerrb/client.rb', line 200

def server_owner?(author_id, guild_or_id)
	guild_data = guild_or_id.is_a?(Hash) ? guild_or_id : get_guild(guild_or_id)
	author_id.to_s == guild_data['owner_id'].to_s
end

#startObject



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

Returns:

  • (Boolean)


204
205
206
# File 'lib/fluxerrb/client.rb', line 204

def user_id?(author_id, target_id)
	author_id.to_s == target_id.to_s
end