Class: Discordrb::Bot
Instance Method Summary collapse
- #add_handler(handler) ⇒ Object (also: #<<)
- #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.
- #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
-
#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.
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/discordrb/bot.rb', line 26 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 @email = email @password = password @token = login @event_handlers = {} @channels = {} @users = {} end |
Instance Method Details
#add_handler(handler) ⇒ Object Also known as: <<
174 175 176 177 |
# File 'lib/discordrb/bot.rb', line 174 def add_handler(handler) clazz = event_class(handler.class) @event_handlers[clazz] << handler end |
#channel(id) ⇒ Object
65 66 67 68 69 70 71 72 |
# File 'lib/discordrb/bot.rb', line 65 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 |
#channel_create(attributes = {}, &block) ⇒ Object
Handle channel creation Attributes:
-
type: Channel type (‘text’ or ‘voice’)
-
name: Channel name
136 137 138 |
# File 'lib/discordrb/bot.rb', line 136 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
152 153 154 |
# File 'lib/discordrb/bot.rb', line 152 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
144 145 146 |
# File 'lib/discordrb/bot.rb', line 144 def channel_update(attributes = {}, &block) register_event(ChannelUpdateEvent, attributes, block) end |
#debug=(debug) ⇒ Object
104 105 106 |
# File 'lib/discordrb/bot.rb', line 104 def debug=(debug) @debug = debug end |
#disconnected(attributes = {}, &block) ⇒ Object
116 117 118 |
# File 'lib/discordrb/bot.rb', line 116 def disconnected(attributes = {}, &block) register_event(DisconnectEvent, attributes, block) end |
#mention(attributes = {}, &block) ⇒ Object
128 129 130 |
# File 'lib/discordrb/bot.rb', line 128 def mention(attributes = {}, &block) register_event(MentionEvent, attributes, block) end |
#message(attributes = {}, &block) ⇒ Object
108 109 110 |
# File 'lib/discordrb/bot.rb', line 108 def (attributes = {}, &block) register_event(MessageEvent, attributes, block) end |
#presence(attributes = {}, &block) ⇒ Object
124 125 126 |
# File 'lib/discordrb/bot.rb', line 124 def presence(attributes = {}, &block) register_event(PresenceEvent, attributes, block) end |
#private_channel(id) ⇒ Object
74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/discordrb/bot.rb', line 74 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
112 113 114 |
# File 'lib/discordrb/bot.rb', line 112 def ready(attributes = {}, &block) register_event(ReadyEvent, attributes, block) end |
#remove_handler(handler) ⇒ Object
169 170 171 172 |
# File 'lib/discordrb/bot.rb', line 169 def remove_handler(handler) clazz = event_class(handler.class) @event_handlers[clazz].delete(handler) end |
#run ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/discordrb/bot.rb', line 46 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
95 96 97 98 99 100 101 102 |
# File 'lib/discordrb/bot.rb', line 95 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
91 92 93 |
# File 'lib/discordrb/bot.rb', line 91 def server(id) @servers[id] end |
#typing(attributes = {}, &block) ⇒ Object
120 121 122 |
# File 'lib/discordrb/bot.rb', line 120 def typing(attributes = {}, &block) register_event(TypingEvent, attributes, block) end |
#user(id) ⇒ Object
87 88 89 |
# File 'lib/discordrb/bot.rb', line 87 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
165 166 167 |
# File 'lib/discordrb/bot.rb', line 165 def voice_state_update(attributes = {}, &block) register_event(VoiceStateUpdateEvent, attributes, block) end |