Class: Nanobot::Channels::Discord

Inherits:
BaseChannel show all
Defined in:
lib/nanobot/channels/discord.rb

Overview

Discord channel integration using the discordrb gem. Listens for server messages and responds in the same channel.

Instance Attribute Summary

Attributes inherited from BaseChannel

#bus, #config, #logger, #name

Instance Method Summary collapse

Methods inherited from BaseChannel

#allowed?, #initialize, #running?

Constructor Details

This class inherits a constructor from Nanobot::Channels::BaseChannel

Instance Method Details

#send(message) ⇒ Object

Send a message to a Discord channel, splitting at the 2000-char limit.



40
41
42
43
44
45
46
47
48
49
# File 'lib/nanobot/channels/discord.rb', line 40

def send(message)
  return unless @bot

  channel = @bot.channel(message.chat_id.to_i)
  return unless channel

  split_message(message.content, 2000).each do |chunk|
    channel.send_message(chunk)
  end
end

#start ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/nanobot/channels/discord.rb', line 14

def start
  @running = true
  @bot = Discordrb::Bot.new(token: @config.token, intents: [:server_messages])

  @bot.message do |event|
    next if event.author.bot_account?
    next unless allowed?(event.author.id.to_s)

    handle_message(
      sender_id: event.author.id.to_s,
      chat_id: event.channel.id.to_s,
      content: event.content
    )
  end

  @logger.info 'Discord bot connecting...'
  @bot.run
end

#stop ⇒ Object



33
34
35
36
# File 'lib/nanobot/channels/discord.rb', line 33

def stop
  @running = false
  @bot&.stop
end