Class: Discordrb::Bot
Overview
Represents a Discord bot, including servers, users, etc.
Direct Known Subclasses
Instance Attribute Summary collapse
-
#bot_user ⇒ Object
readonly
Returns the value of attribute bot_user.
- #debug(message, important = false) ⇒ Object
-
#event_threads ⇒ Object
readonly
Returns the value of attribute event_threads.
-
#profile ⇒ Object
readonly
Returns the value of attribute profile.
-
#servers ⇒ Object
readonly
Returns the value of attribute servers.
-
#should_parse_self ⇒ Object
Returns the value of attribute should_parse_self.
-
#token ⇒ Object
readonly
Returns the value of attribute token.
-
#users ⇒ Object
readonly
Returns the value of attribute users.
Instance Method Summary collapse
- #add_await(key, type, attributes = {}, &block) ⇒ Object
- #add_handler(handler) ⇒ Object (also: #<<)
- #await(attributes = {}, &block) ⇒ Object
- #channel(id) ⇒ Object
-
#channel_create(attributes = {}, &block) ⇒ Object
Handle channel creation Attributes: * type: Channel type (‘text’ or ‘voice’) * name: Channel name.
-
#channel_delete(attributes = {}, &block) ⇒ Object
Handle channel deletion Attributes: * type: Channel type (‘text’ or ‘voice’) * name: Channel name.
-
#channel_update(attributes = {}, &block) ⇒ Object
Handle channel update Attributes: * type: Channel type (‘text’ or ‘voice’) * name: Channel name.
-
#create_server(name, region = :london) ⇒ Object
Regions: :london, :amsterdam, :frankfurt, :us-east, :us-west, :singapore, :sydney.
- #delete_invite(code) ⇒ Object
- #disconnected(attributes = {}, &block) ⇒ Object
- #find(channel_name, server_name = nil, threshold = 0) ⇒ Object
- #game=(name_or_id) ⇒ Object
- #handler_class(event_class) ⇒ Object
-
#initialize(email, password, debug = false) ⇒ Bot
constructor
A new instance of Bot.
- #join(invite) ⇒ Object
- #log_exception(e) ⇒ Object
- #member_join(attributes = {}, &block) ⇒ Object
- #member_leave(attributes = {}, &block) ⇒ Object
- #member_update(attributes = {}, &block) ⇒ Object
- #mention(attributes = {}, &block) ⇒ Object
-
#message(attributes = {}, &block) ⇒ Object
## ### ## ## ######## ## ######## ######## ###### ## ## ## ### ## ## ## ## ## ## ## ## ## ## ## ## #### ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ###### ######## ###### ## ######### ## #### ## ## ## ## ## ## ## ## ## ## ## ### ## ## ## ## ## ## ## ## ## ## ## ## ## ######## ######## ######## ## ## ######.
- #parse_mention(mention) ⇒ Object
- #pm(attributes = {}, &block) ⇒ Object (also: #private_message)
- #presence(attributes = {}, &block) ⇒ Object
- #private_channel(id) ⇒ Object
- #ready(attributes = {}, &block) ⇒ Object
- #remove_handler(handler) ⇒ Object
- #resolve_invite_code(invite) ⇒ Object
- #run(async = false) ⇒ Object
- #run_async ⇒ Object
- #send_file(channel_id, file) ⇒ Object
- #send_message(channel_id, content) ⇒ Object
- #server(id) ⇒ Object
- #server_create(attributes = {}, &block) ⇒ Object
- #server_delete(attributes = {}, &block) ⇒ Object
- #server_update(attributes = {}, &block) ⇒ Object
- #stop ⇒ Object
- #sync ⇒ Object
- #typing(attributes = {}, &block) ⇒ Object
- #user(id) ⇒ Object
-
#voice_state_update(attributes = {}, &block) ⇒ Object
Handle a change to a voice state.
Methods included from Events
Constructor Details
#initialize(email, password, debug = false) ⇒ Bot
Returns a new instance of Bot.
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/discordrb/bot.rb', line 34 def initialize(email, password, debug = false) # Make sure people replace the login details in the example files... if email.end_with? 'example.com' puts 'You have to replace the login details in the example files with your own!' exit end @debug = debug @should_parse_self = false @email = email @password = password @token = login @event_handlers = {} @channels = {} @users = {} @awaits = {} @event_threads = [] @current_thread = 0 end |
Instance Attribute Details
#bot_user ⇒ Object (readonly)
Returns the value of attribute bot_user.
31 32 33 |
# File 'lib/discordrb/bot.rb', line 31 def bot_user @bot_user end |
#debug(message, important = false) ⇒ Object
331 332 333 |
# File 'lib/discordrb/bot.rb', line 331 def debug(, important = false) puts "[DEBUG : #{Thread.current[:discordrb_name]} @ #{Time.now}] #{message}" if @debug || important end |
#event_threads ⇒ Object (readonly)
Returns the value of attribute event_threads.
31 32 33 |
# File 'lib/discordrb/bot.rb', line 31 def event_threads @event_threads end |
#profile ⇒ Object (readonly)
Returns the value of attribute profile.
31 32 33 |
# File 'lib/discordrb/bot.rb', line 31 def profile @profile end |
#servers ⇒ Object (readonly)
Returns the value of attribute servers.
31 32 33 |
# File 'lib/discordrb/bot.rb', line 31 def servers @servers end |
#should_parse_self ⇒ Object
Returns the value of attribute should_parse_self.
32 33 34 |
# File 'lib/discordrb/bot.rb', line 32 def should_parse_self @should_parse_self end |
#token ⇒ Object (readonly)
Returns the value of attribute token.
31 32 33 |
# File 'lib/discordrb/bot.rb', line 31 def token @token end |
#users ⇒ Object (readonly)
Returns the value of attribute users.
31 32 33 |
# File 'lib/discordrb/bot.rb', line 31 def users @users end |
Instance Method Details
#add_await(key, type, attributes = {}, &block) ⇒ Object
178 179 180 181 182 |
# File 'lib/discordrb/bot.rb', line 178 def add_await(key, type, attributes = {}, &block) fail "You can't await an AwaitEvent!" if type == Discordrb::Events::AwaitEvent await = Await.new(self, key, type, attributes, block) @awaits[key] = await end |
#add_handler(handler) ⇒ Object Also known as: <<
326 327 328 329 |
# File 'lib/discordrb/bot.rb', line 326 def add_handler(handler) clazz = event_class(handler.class) @event_handlers[clazz] << handler end |
#await(attributes = {}, &block) ⇒ Object
311 312 313 |
# File 'lib/discordrb/bot.rb', line 311 def await(attributes = {}, &block) register_event(AwaitEvent, attributes, block) end |
#channel(id) ⇒ Object
104 105 106 107 108 109 110 111 |
# File 'lib/discordrb/bot.rb', line 104 def channel(id) debug("Obtaining data for channel with id #{id}") return @channels[id] if @channels[id] response = API.channel(@token, id) channel = Channel.new(JSON.parse(response), self) @channels[id] = channel end |
#channel_create(attributes = {}, &block) ⇒ Object
Handle channel creation Attributes:
-
type: Channel type (‘text’ or ‘voice’)
-
name: Channel name
254 255 256 |
# File 'lib/discordrb/bot.rb', line 254 def channel_create(attributes = {}, &block) register_event(ChannelCreateEvent, attributes, block) end |
#channel_delete(attributes = {}, &block) ⇒ Object
Handle channel deletion Attributes:
-
type: Channel type (‘text’ or ‘voice’)
-
name: Channel name
270 271 272 |
# File 'lib/discordrb/bot.rb', line 270 def channel_delete(attributes = {}, &block) register_event(ChannelDeleteEvent, attributes, block) end |
#channel_update(attributes = {}, &block) ⇒ Object
Handle channel update Attributes:
-
type: Channel type (‘text’ or ‘voice’)
-
name: Channel name
262 263 264 |
# File 'lib/discordrb/bot.rb', line 262 def channel_update(attributes = {}, &block) register_event(ChannelUpdateEvent, attributes, block) end |
#create_server(name, region = :london) ⇒ Object
Regions: :london, :amsterdam, :frankfurt, :us-east, :us-west, :singapore, :sydney
185 186 187 188 189 190 191 192 |
# File 'lib/discordrb/bot.rb', line 185 def create_server(name, region = :london) response = API.create_server(@token, name, region) id = JSON.parse(response)['id'].to_i sleep 0.1 until @servers[id] server = @servers[id] debug "Successfully created server #{server.id} with name #{server.name}" server end |
#delete_invite(code) ⇒ Object
134 135 136 137 |
# File 'lib/discordrb/bot.rb', line 134 def delete_invite(code) invite = resolve_invite_code(code) API.delete_invite(@token, invite) end |
#disconnected(attributes = {}, &block) ⇒ Object
234 235 236 |
# File 'lib/discordrb/bot.rb', line 234 def disconnected(attributes = {}, &block) register_event(DisconnectEvent, attributes, block) end |
#find(channel_name, server_name = nil, threshold = 0) ⇒ Object
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 |
# File 'lib/discordrb/bot.rb', line 147 def find(channel_name, server_name = nil, threshold = 0) require 'levenshtein' results = [] @servers.values.each do |server| server.channels.each do |channel| distance = Levenshtein.distance(channel.name, channel_name) distance += Levenshtein.distance(server_name || server.name, server.name) next if distance > threshold # Make a singleton accessor "distance" channel.instance_variable_set(:@distance, distance) class << channel attr_reader :distance end results << channel end end results end |
#game=(name_or_id) ⇒ Object
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 |
# File 'lib/discordrb/bot.rb', line 200 def game=(name_or_id) game = Discordrb::Games.find_game(name_or_id) @game = game data = { 'op' => 3, 'd' => { 'idle_since' => nil, 'game_id' => game ? game.id : 60 # 60 blanks out the game playing } } @ws.send(data.to_json) game end |
#handler_class(event_class) ⇒ Object
340 341 342 |
# File 'lib/discordrb/bot.rb', line 340 def handler_class(event_class) class_from_string(event_class.to_s + 'Handler') end |
#join(invite) ⇒ Object
128 129 130 131 132 |
# File 'lib/discordrb/bot.rb', line 128 def join(invite) invite = resolve_invite_code(invite) resolved = JSON.parse(API.resolve_invite(@token, invite))['code'] API.join_server(@token, resolved) end |
#log_exception(e) ⇒ Object
335 336 337 338 |
# File 'lib/discordrb/bot.rb', line 335 def log_exception(e) debug("Exception: #{e.inspect}", true) e.backtrace.each { |line| debug(line, true) } end |
#member_join(attributes = {}, &block) ⇒ Object
287 288 289 |
# File 'lib/discordrb/bot.rb', line 287 def member_join(attributes = {}, &block) register_event(GuildMemberAddEvent, attributes, block) end |
#member_leave(attributes = {}, &block) ⇒ Object
295 296 297 |
# File 'lib/discordrb/bot.rb', line 295 def member_leave(attributes = {}, &block) register_event(GuildMemberDeleteEvent, attributes, block) end |
#member_update(attributes = {}, &block) ⇒ Object
291 292 293 |
# File 'lib/discordrb/bot.rb', line 291 def member_update(attributes = {}, &block) register_event(GuildMemberUpdateEvent, attributes, block) end |
#mention(attributes = {}, &block) ⇒ Object
246 247 248 |
# File 'lib/discordrb/bot.rb', line 246 def mention(attributes = {}, &block) register_event(MentionEvent, attributes, block) end |
#message(attributes = {}, &block) ⇒ Object
## ### ## ## ######## ## ######## ######## ######
## ## ## ### ## ## ## ## ## ## ## ## ##
## ## ## #### ## ## ## ## ## ## ## ##
## ## ## ## ## ## ## ## ###### ######## ######
## ######### ## #### ## ## ## ## ## ## ##
## ## ## ## ### ## ## ## ## ## ## ## ##
## ## ## ## ## ######## ######## ######## ## ## ######
226 227 228 |
# File 'lib/discordrb/bot.rb', line 226 def (attributes = {}, &block) register_event(MessageEvent, attributes, block) end |
#parse_mention(mention) ⇒ Object
194 195 196 197 198 |
# File 'lib/discordrb/bot.rb', line 194 def parse_mention(mention) # Mention format: <@id> return nil unless /\<@(?<id>\d+)\>?/ =~ mention user(id) end |
#pm(attributes = {}, &block) ⇒ Object Also known as: private_message
315 316 317 |
# File 'lib/discordrb/bot.rb', line 315 def pm(attributes = {}, &block) register_event(PrivateMessageEvent, attributes, block) end |
#presence(attributes = {}, &block) ⇒ Object
242 243 244 |
# File 'lib/discordrb/bot.rb', line 242 def presence(attributes = {}, &block) register_event(PresenceEvent, attributes, block) end |
#private_channel(id) ⇒ Object
113 114 115 116 117 118 119 120 |
# File 'lib/discordrb/bot.rb', line 113 def private_channel(id) debug("Creating private channel with user id #{id}") return @private_channels[id] if @private_channels[id] response = API.create_private(@token, @bot_user.id, id) channel = Channel.new(JSON.parse(response), self) @private_channels[id] = channel end |
#ready(attributes = {}, &block) ⇒ Object
230 231 232 |
# File 'lib/discordrb/bot.rb', line 230 def ready(attributes = {}, &block) register_event(ReadyEvent, attributes, block) end |
#remove_handler(handler) ⇒ Object
321 322 323 324 |
# File 'lib/discordrb/bot.rb', line 321 def remove_handler(handler) clazz = event_class(handler.class) @event_handlers[clazz].delete(handler) end |
#resolve_invite_code(invite) ⇒ Object
122 123 124 125 126 |
# File 'lib/discordrb/bot.rb', line 122 def resolve_invite_code(invite) invite = invite.code if invite.is_a? Discordrb::Invite invite = invite[invite.rindex('/') + 1..-1] if invite.start_with?('http') || invite.start_with?('discord.gg') invite end |
#run(async = false) ⇒ Object
60 61 62 63 64 65 66 |
# File 'lib/discordrb/bot.rb', line 60 def run(async = false) run_async return if async debug('Oh wait! Not exiting yet as run was run synchronously.') sync end |
#run_async ⇒ Object
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/discordrb/bot.rb', line 68 def run_async # Handle heartbeats @heartbeat_interval = 1 @heartbeat_active = false @heartbeat_thread = Thread.new do Thread.current[:discordrb_name] = 'heartbeat' loop do sleep @heartbeat_interval send_heartbeat if @heartbeat_active end end @ws_thread = Thread.new do Thread.current[:discordrb_name] = 'websocket' loop do websocket_connect debug('Disconnected! Attempting to reconnect in 5 seconds.') sleep 5 @token = login end end debug('WS thread created! Now waiting for confirmation that everything worked') @ws_success = false sleep(0.5) until @ws_success debug('Confirmation received! Exiting run.') end |
#send_file(channel_id, file) ⇒ Object
174 175 176 |
# File 'lib/discordrb/bot.rb', line 174 def send_file(channel_id, file) API.send_file(@token, channel_id, file) end |
#send_message(channel_id, content) ⇒ Object
168 169 170 171 172 |
# File 'lib/discordrb/bot.rb', line 168 def (channel_id, content) debug("Sending message to #{channel_id} with content '#{content}'") response = API.(@token, channel_id, content) Message.new(JSON.parse(response), self) end |
#server(id) ⇒ Object
143 144 145 |
# File 'lib/discordrb/bot.rb', line 143 def server(id) @servers[id] end |
#server_create(attributes = {}, &block) ⇒ Object
299 300 301 |
# File 'lib/discordrb/bot.rb', line 299 def server_create(attributes = {}, &block) register_event(GuildCreateEvent, attributes, block) end |
#server_delete(attributes = {}, &block) ⇒ Object
307 308 309 |
# File 'lib/discordrb/bot.rb', line 307 def server_delete(attributes = {}, &block) register_event(GuildDeleteEvent, attributes, block) end |
#server_update(attributes = {}, &block) ⇒ Object
303 304 305 |
# File 'lib/discordrb/bot.rb', line 303 def server_update(attributes = {}, &block) register_event(GuildUpdateEvent, attributes, block) end |
#stop ⇒ Object
100 101 102 |
# File 'lib/discordrb/bot.rb', line 100 def stop @ws_thread.kill end |
#sync ⇒ Object
96 97 98 |
# File 'lib/discordrb/bot.rb', line 96 def sync @ws_thread.join end |
#typing(attributes = {}, &block) ⇒ Object
238 239 240 |
# File 'lib/discordrb/bot.rb', line 238 def typing(attributes = {}, &block) register_event(TypingEvent, attributes, block) end |
#user(id) ⇒ Object
139 140 141 |
# File 'lib/discordrb/bot.rb', line 139 def user(id) @users[id] end |
#voice_state_update(attributes = {}, &block) ⇒ Object
Handle a change to a voice state. This includes joining a voice channel or changing mute or deaf state. Attributes:
-
from: User whose voice state changed
-
mute: server mute status
-
deaf: server deaf status
-
self_mute: self mute status
-
self_deaf: self deaf status
-
channel: channel the user joined
283 284 285 |
# File 'lib/discordrb/bot.rb', line 283 def voice_state_update(attributes = {}, &block) register_event(VoiceStateUpdateEvent, attributes, block) end |