Class: Lita::Adapters::Discord_oauth

Inherits:
Adapter
  • Object
show all
Defined in:
lib/lita/adapters/discord_oauth.rb

Instance Method Summary collapse

Constructor Details

#initialize(robot) ⇒ Discord_oauth

Returns a new instance of Discord_oauth.



9
10
11
12
# File 'lib/lita/adapters/discord_oauth.rb', line 9

def initialize(robot)
  super
  @client = ::Discordrb::Bot.new token: config.token, client_id: config.client
end

Instance Method Details

#runObject



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/lita/adapters/discord_oauth.rb', line 14

def run
  Lita.logger.debug('Starting discord_oauth adapter')
  @client.ready do |e|
    robot.trigger(:connected)

    version = Gem.loaded_specs['lita-discord_oauth'].version

    @client.game = "Version #{version}"

    @client.message do |event|
      message = event.message
      channel = event.channel.id.to_s
      author_id = message.author.id.to_s

      if event.channel.pm?
        # We're dealing with a PM
        author_name = message.author.username.to_s
      else
        # We're dealing with a regular (server) message
        author_name = message.author.display_name.to_s
      end



      Lita.logger.debug("Received message from #{author_name}(#{author_id}): #{message.content}")
      Lita.logger.debug("Finding user #{author_name}")

      user = Lita::User.find_by_name(author_name)

      if user == nil
        Lita.logger.debug("User #{author_name} not found, trying ID #{author_id}")
        user = Lita::User.find_by_id(author_id)

        if user != nil
          Lita.logger.debug("User #{author_id} found, updating name to #{author_name}")
          user = Lita::User.create(author_id, {name: author_name})
        end

        Lita.logger.debug("User #{author_id} not found, creating now")
        user = Lita::User.create(author_id, {name: author_name}) unless user
      end

      Lita.logger.debug('User ID: ' + user.id)
      Lita.logger.debug('Channel ID: ' + channel)

      source = Lita::Source.new(user: user, room: channel)
      msg = Lita::Message.new(robot, message.content, source)

      robot.receive(msg) unless message.from_bot?

    end
  end


  @client.run
end

#send_messages(target, messages) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/lita/adapters/discord_oauth.rb', line 78

def send_messages(target, messages)
  Lita.logger.debug("Sending message to user #{target.user.id} in channel #{target.room}")

  mention = @client.user(target.user.id).mention

  messages.each do |message|
    if mention
      no_mention = message.slice!('|||NOMENTION|||')

      message = mention + ",\n" + message if no_mention != '|||NOMENTION|||'



      Lita.logger.info('Message is too long') if message.length > 2000

      @client.send_message(target.room, message)
    end
  end
end

#shut_downObject



71
72
73
74
75
76
# File 'lib/lita/adapters/discord_oauth.rb', line 71

def shut_down
  @client.stop
  # FIXME: Heroku nightly power cycles don't always properly disconnect
  # Perhaps slowing down the shutdown might help.
  sleep(1)
end