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: <<
120 121 122 123 |
# File 'lib/discordrb/bot.rb', line 120 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
87 88 89 |
# File 'lib/discordrb/bot.rb', line 87 def debug=(debug) @debug = debug end |
#disconnected(attributes = {}, &block) ⇒ Object
99 100 101 |
# File 'lib/discordrb/bot.rb', line 99 def disconnected(attributes = {}, &block) register_event(DisconnectEvent, attributes, block) end |
#mention(attributes = {}, &block) ⇒ Object
111 112 113 |
# File 'lib/discordrb/bot.rb', line 111 def mention(attributes = {}, &block) register_event(MentionEvent, attributes, block) end |
#message(attributes = {}, &block) ⇒ Object
91 92 93 |
# File 'lib/discordrb/bot.rb', line 91 def (attributes = {}, &block) register_event(MessageEvent, attributes, block) end |
#presence(attributes = {}, &block) ⇒ Object
107 108 109 |
# File 'lib/discordrb/bot.rb', line 107 def presence(attributes = {}, &block) register_event(PresenceEvent, attributes, block) end |
#private_channel(id) ⇒ Object
60 61 62 63 64 65 66 67 |
# 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] response = RestClient.get Discordrb::Endpoints::USERS + "/#{id}/channels", {:Authorization => @token} channel = Channel.new(JSON.parse(response), self) @private_channels[id] = channel end |
#ready(attributes = {}, &block) ⇒ Object
95 96 97 |
# File 'lib/discordrb/bot.rb', line 95 def ready(attributes = {}, &block) register_event(ReadyEvent, attributes, block) end |
#remove_handler(handler) ⇒ Object
115 116 117 118 |
# File 'lib/discordrb/bot.rb', line 115 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 puts "disconnected. attempting to reconnect. " + Time.now.to_s sleep 2 @token = login end end |
#send_message(channel_id, content) ⇒ Object
77 78 79 80 81 82 83 84 85 |
# File 'lib/discordrb/bot.rb', line 77 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
73 74 75 |
# File 'lib/discordrb/bot.rb', line 73 def server(id) @servers[id] end |
#typing(attributes = {}, &block) ⇒ Object
103 104 105 |
# File 'lib/discordrb/bot.rb', line 103 def typing(attributes = {}, &block) register_event(TypingEvent, attributes, block) end |
#user(id) ⇒ Object
69 70 71 |
# File 'lib/discordrb/bot.rb', line 69 def user(id) @users[id] end |