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
-
#token ⇒ Object
readonly
Returns the value of attribute token.
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.
- #disconnected(attributes = {}, &block) ⇒ Object
- #game=(name_or_id) ⇒ Object
-
#initialize(email, password, debug = false) ⇒ Bot
constructor
A new instance of Bot.
- #join(invite) ⇒ Object
- #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(async = false) ⇒ Object
- #run_async ⇒ Object
- #send_file(channel_id, file) ⇒ Object
- #send_message(channel_id, content) ⇒ Object
- #server(id) ⇒ 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.
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/discordrb/bot.rb', line 30 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 Attribute Details
#bot_user ⇒ Object (readonly)
Returns the value of attribute bot_user.
28 29 30 |
# File 'lib/discordrb/bot.rb', line 28 def bot_user @bot_user end |
#debug(message, important = false) ⇒ Object
223 224 225 |
# File 'lib/discordrb/bot.rb', line 223 def debug(, important = false) puts "[DEBUG @ #{Time.now}] #{}" if @debug || important end |
#token ⇒ Object (readonly)
Returns the value of attribute token.
28 29 30 |
# File 'lib/discordrb/bot.rb', line 28 def token @token end |
Instance Method Details
#add_handler(handler) ⇒ Object Also known as: <<
218 219 220 221 |
# File 'lib/discordrb/bot.rb', line 218 def add_handler(handler) clazz = event_class(handler.class) @event_handlers[clazz] << handler end |
#channel(id) ⇒ Object
92 93 94 95 96 97 98 99 |
# File 'lib/discordrb/bot.rb', line 92 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
180 181 182 |
# File 'lib/discordrb/bot.rb', line 180 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
196 197 198 |
# File 'lib/discordrb/bot.rb', line 196 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
188 189 190 |
# File 'lib/discordrb/bot.rb', line 188 def channel_update(attributes = {}, &block) register_event(ChannelUpdateEvent, attributes, block) end |
#disconnected(attributes = {}, &block) ⇒ Object
160 161 162 |
# File 'lib/discordrb/bot.rb', line 160 def disconnected(attributes = {}, &block) register_event(DisconnectEvent, attributes, block) end |
#game=(name_or_id) ⇒ Object
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 |
# File 'lib/discordrb/bot.rb', line 134 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 |
#join(invite) ⇒ Object
110 111 112 113 114 |
# File 'lib/discordrb/bot.rb', line 110 def join(invite) invite = invite[invite.rindex('/') + 1..-1] if invite.start_with?('http') || invite.start_with?('discord.gg') resolved = JSON.parse(API.resolve_invite(@token, invite))['code'] API.join_server(@token, resolved) end |
#mention(attributes = {}, &block) ⇒ Object
172 173 174 |
# File 'lib/discordrb/bot.rb', line 172 def mention(attributes = {}, &block) register_event(MentionEvent, attributes, block) end |
#message(attributes = {}, &block) ⇒ Object
152 153 154 |
# File 'lib/discordrb/bot.rb', line 152 def (attributes = {}, &block) register_event(MessageEvent, attributes, block) end |
#presence(attributes = {}, &block) ⇒ Object
168 169 170 |
# File 'lib/discordrb/bot.rb', line 168 def presence(attributes = {}, &block) register_event(PresenceEvent, attributes, block) end |
#private_channel(id) ⇒ Object
101 102 103 104 105 106 107 108 |
# File 'lib/discordrb/bot.rb', line 101 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
156 157 158 |
# File 'lib/discordrb/bot.rb', line 156 def ready(attributes = {}, &block) register_event(ReadyEvent, attributes, block) end |
#remove_handler(handler) ⇒ Object
213 214 215 216 |
# File 'lib/discordrb/bot.rb', line 213 def remove_handler(handler) clazz = event_class(handler.class) @event_handlers[clazz].delete(handler) end |
#run(async = false) ⇒ Object
50 51 52 53 54 55 56 |
# File 'lib/discordrb/bot.rb', line 50 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
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/discordrb/bot.rb', line 58 def run_async # Handle heartbeats @heartbeat_interval = 1 @heartbeat_active = false @heartbeat_thread = Thread.new do loop do sleep @heartbeat_interval send_heartbeat if @heartbeat_active end end @ws_thread = Thread.new do 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
130 131 132 |
# File 'lib/discordrb/bot.rb', line 130 def send_file(channel_id, file) API.send_file(@token, channel_id, file) end |
#send_message(channel_id, content) ⇒ Object
124 125 126 127 128 |
# File 'lib/discordrb/bot.rb', line 124 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
120 121 122 |
# File 'lib/discordrb/bot.rb', line 120 def server(id) @servers[id] end |
#stop ⇒ Object
88 89 90 |
# File 'lib/discordrb/bot.rb', line 88 def stop @ws_thread.kill end |
#sync ⇒ Object
84 85 86 |
# File 'lib/discordrb/bot.rb', line 84 def sync @ws_thread.join end |
#typing(attributes = {}, &block) ⇒ Object
164 165 166 |
# File 'lib/discordrb/bot.rb', line 164 def typing(attributes = {}, &block) register_event(TypingEvent, attributes, block) end |
#user(id) ⇒ Object
116 117 118 |
# File 'lib/discordrb/bot.rb', line 116 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
209 210 211 |
# File 'lib/discordrb/bot.rb', line 209 def voice_state_update(attributes = {}, &block) register_event(VoiceStateUpdateEvent, attributes, block) end |