Class: Discordrb::Bot
Instance Method Summary collapse
- #add_handler(handler) ⇒ Object (also: #<<)
- #channel(id) ⇒ Object
- #debug=(debug) ⇒ Object
- #disconnected(attributes = {}, &block) ⇒ Object
-
#initialize(email, password, debug = false) ⇒ Bot
constructor
A new instance of Bot.
- #mention(attributes = {}, &block) ⇒ Object
- #message(attributes = {}, &block) ⇒ Object
- #presence(attributes = {}, &block) ⇒ Object
- #private_channel(id) ⇒ Object
- #ready(attributes = {}, &block) ⇒ Object
- #remove_handler(handler) ⇒ Object
- #run ⇒ Object
- #send_message(channel_id, content) ⇒ Object
- #server(id) ⇒ Object
- #typing(attributes = {}, &block) ⇒ Object
- #user(id) ⇒ Object
Methods included from Events
Constructor Details
#initialize(email, password, debug = false) ⇒ Bot
Returns a new instance of Bot.
18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/discordrb/bot.rb', line 18 def initialize(email, password, debug = false) @debug = debug @email = email @password = password @token = login @event_handlers = {} @channels = {} @users = {} end |
Instance Method Details
#add_handler(handler) ⇒ Object Also known as: <<
123 124 125 126 |
# File 'lib/discordrb/bot.rb', line 123 def add_handler(handler) clazz = event_class(handler.class) @event_handlers[clazz] << handler end |
#channel(id) ⇒ Object
51 52 53 54 55 56 57 58 |
# File 'lib/discordrb/bot.rb', line 51 def channel(id) debug("Obtaining data for channel with id #{id}") return @channels[id] if @channels[id] response = RestClient.get Discordrb::Endpoints::CHANNELS + "/#{id}", {:Authorization => @token} channel = Channel.new(JSON.parse(response), self) @channels[id] = channel end |
#debug=(debug) ⇒ Object
90 91 92 |
# File 'lib/discordrb/bot.rb', line 90 def debug=(debug) @debug = debug end |
#disconnected(attributes = {}, &block) ⇒ Object
102 103 104 |
# File 'lib/discordrb/bot.rb', line 102 def disconnected(attributes = {}, &block) register_event(DisconnectEvent, attributes, block) end |
#mention(attributes = {}, &block) ⇒ Object
114 115 116 |
# File 'lib/discordrb/bot.rb', line 114 def mention(attributes = {}, &block) register_event(MentionEvent, attributes, block) end |
#message(attributes = {}, &block) ⇒ Object
94 95 96 |
# File 'lib/discordrb/bot.rb', line 94 def (attributes = {}, &block) register_event(MessageEvent, attributes, block) end |
#presence(attributes = {}, &block) ⇒ Object
110 111 112 |
# File 'lib/discordrb/bot.rb', line 110 def presence(attributes = {}, &block) register_event(PresenceEvent, attributes, block) end |
#private_channel(id) ⇒ Object
60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/discordrb/bot.rb', line 60 def private_channel(id) debug("Creating private channel with user id #{id}") return @private_channels[id] if @private_channels[id] data = { 'recipient_id' => id } response = RestClient.post Discordrb::Endpoints::USERS + "/#{@bot_user.id}/channels", data.to_json, {:Authorization => @token, :content_type => :json} channel = Channel.new(JSON.parse(response), self) @private_channels[id] = channel end |
#ready(attributes = {}, &block) ⇒ Object
98 99 100 |
# File 'lib/discordrb/bot.rb', line 98 def ready(attributes = {}, &block) register_event(ReadyEvent, attributes, block) end |
#remove_handler(handler) ⇒ Object
118 119 120 121 |
# File 'lib/discordrb/bot.rb', line 118 def remove_handler(handler) clazz = event_class(handler.class) @event_handlers[clazz].delete(handler) end |
#run ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/discordrb/bot.rb', line 32 def run # Handle heartbeats @heartbeat_interval = 1 @heartbeat_active = false @heartbeat_thread = Thread.new do while true do sleep @heartbeat_interval send_heartbeat if @heartbeat_active end end while true do websocket_connect debug("Disconnected! Attempting to reconnect in 5 seconds.") sleep 5 @token = login end end |
#send_message(channel_id, content) ⇒ Object
81 82 83 84 85 86 87 88 |
# File 'lib/discordrb/bot.rb', line 81 def (channel_id, content) debug("Sending message to #{channel_id} with content '#{content}'") data = { 'content' => content.to_s, 'mentions' => [] } RestClient.post Discordrb::Endpoints::CHANNELS + "/#{channel_id}/messages", data.to_json, {:Authorization => @token, :content_type => :json} end |
#server(id) ⇒ Object
77 78 79 |
# File 'lib/discordrb/bot.rb', line 77 def server(id) @servers[id] end |
#typing(attributes = {}, &block) ⇒ Object
106 107 108 |
# File 'lib/discordrb/bot.rb', line 106 def typing(attributes = {}, &block) register_event(TypingEvent, attributes, block) end |
#user(id) ⇒ Object
73 74 75 |
# File 'lib/discordrb/bot.rb', line 73 def user(id) @users[id] end |