Class: Discordrb::Bot

Inherits:
Object
  • Object
show all
Includes:
Events
Defined in:
lib/discordrb/bot.rb

Instance Method Summary collapse

Methods included from Events

matches_all

Constructor Details

#initialize(email, password) ⇒ Bot

Returns a new instance of Bot.



17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/discordrb/bot.rb', line 17

def initialize(email, password)
  @email = email
  @password = password

  @token = 

  @event_handlers = {}

  @channels = {}
  @users = {}

  @debug = false
end

Instance Method Details

#channel(id) ⇒ Object



45
46
47
48
49
50
51
52
# File 'lib/discordrb/bot.rb', line 45

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



68
69
70
# File 'lib/discordrb/bot.rb', line 68

def debug=(debug)
  @debug = debug
end

#disconnected(attributes = {}, &block) ⇒ Object



82
83
84
85
# File 'lib/discordrb/bot.rb', line 82

def disconnected(attributes = {}, &block)
  @event_handlers[DisconnectEvent] ||= []
  @event_handlers[DisconnectEvent] << DisconnectEventHandler.new(attributes, block)
end

#message(attributes = {}, &block) ⇒ Object



72
73
74
75
# File 'lib/discordrb/bot.rb', line 72

def message(attributes = {}, &block)
  @event_handlers[MessageEvent] ||= []
  @event_handlers[MessageEvent] << MessageEventHandler.new(attributes, block)
end

#ready(attributes = {}, &block) ⇒ Object



77
78
79
80
# File 'lib/discordrb/bot.rb', line 77

def ready(attributes = {}, &block)
  @event_handlers[ReadyEvent] ||= []
  @event_handlers[ReadyEvent] << ReadyEventHandler.new(attributes, block)
end

#runObject



31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/discordrb/bot.rb', line 31

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

  websocket_connect
end

#send_message(channel_id, content) ⇒ Object



58
59
60
61
62
63
64
65
66
# File 'lib/discordrb/bot.rb', line 58

def send_message(channel_id, content)
  debug("Sending message to #{channel_id} with content '#{content}'")
  data = {
    'content' => content,
    'mentions' => []
  }

  RestClient.post Discordrb::Endpoints::CHANNELS + "/#{channel_id}/messages", data.to_json, {:Authorization => @token, :content_type => :json}
end

#typing(attributes = {}, &block) ⇒ Object



87
88
89
90
# File 'lib/discordrb/bot.rb', line 87

def typing(attributes = {}, &block)
  @event_handlers[TypingEvent] ||= []
  @event_handlers[TypingEvent] << TypingEvent.new(attributes, block)
end

#user(id) ⇒ Object



54
55
56
# File 'lib/discordrb/bot.rb', line 54

def user(id)
  @users[id]
end