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
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# 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 = {} add_default_handlers end |
Instance Method Details
#add_handler(handler) ⇒ Object Also known as: <<
125 126 127 128 |
# File 'lib/discordrb/bot.rb', line 125 def add_handler(handler) clazz = event_class(handler.class) @event_handlers[clazz] << handler end |
#channel(id) ⇒ Object
53 54 55 56 57 58 59 60 |
# File 'lib/discordrb/bot.rb', line 53 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
92 93 94 |
# File 'lib/discordrb/bot.rb', line 92 def debug=(debug) @debug = debug end |
#disconnected(attributes = {}, &block) ⇒ Object
104 105 106 |
# File 'lib/discordrb/bot.rb', line 104 def disconnected(attributes = {}, &block) register_event(DisconnectEvent, attributes, block) end |
#mention(attributes = {}, &block) ⇒ Object
116 117 118 |
# File 'lib/discordrb/bot.rb', line 116 def mention(attributes = {}, &block) register_event(MentionEvent, attributes, block) end |
#message(attributes = {}, &block) ⇒ Object
96 97 98 |
# File 'lib/discordrb/bot.rb', line 96 def (attributes = {}, &block) register_event(MessageEvent, attributes, block) end |
#presence(attributes = {}, &block) ⇒ Object
112 113 114 |
# File 'lib/discordrb/bot.rb', line 112 def presence(attributes = {}, &block) register_event(PresenceEvent, attributes, block) end |
#private_channel(id) ⇒ Object
62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/discordrb/bot.rb', line 62 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
100 101 102 |
# File 'lib/discordrb/bot.rb', line 100 def ready(attributes = {}, &block) register_event(ReadyEvent, attributes, block) end |
#remove_handler(handler) ⇒ Object
120 121 122 123 |
# File 'lib/discordrb/bot.rb', line 120 def remove_handler(handler) clazz = event_class(handler.class) @event_handlers[clazz].delete(handler) end |
#run ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/discordrb/bot.rb', line 34 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
83 84 85 86 87 88 89 90 |
# File 'lib/discordrb/bot.rb', line 83 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
79 80 81 |
# File 'lib/discordrb/bot.rb', line 79 def server(id) @servers[id] end |
#typing(attributes = {}, &block) ⇒ Object
108 109 110 |
# File 'lib/discordrb/bot.rb', line 108 def typing(attributes = {}, &block) register_event(TypingEvent, attributes, block) end |
#user(id) ⇒ Object
75 76 77 |
# File 'lib/discordrb/bot.rb', line 75 def user(id) @users[id] end |