Skip to content

Containers

Matthew Carey edited this page Nov 15, 2020 · 1 revision

Starting with release 1.7.0, discordrb allows you to make bots highly modular using container modules. These are modules that extend the EventContainer or CommandContainer modules - Bot classes extend them by default. The powerful feature is that a container can be included into another container, which copies over all events (or commands) from the included container into the including container. See this example:

module JoinAnnouncer
  extend Discordrb::EventContainer

  member_join do |event|
    event.server.general_channel.send_message "#{event.user.name} joined!"
  end
end

bot = Discordrb::Bot.new # ...
bot.include! JoinAnnouncer
bot.run

This creates a new container that contains one event, and then later includes it using the include! method. (Even though this method is called include_events by default, it is recommended to use include! to be consistent with command events, where including events and commands have to be separate.) Of course, the same thing is possible with command containers:

module Utilities
  extend Discordrb::Commands::CommandContainer

  command :my_id do |event|
    event.user.id
  end
end

bot = Discordrb::Commands::CommandBot.new # ...
bot.include! Utilities
bot.run

It is possible to have a module that is both a command container and an event container, by simply extending both CommandContainer and EventContainer in your module. Note that the extend Discordrb::Commands::CommandContainer must come after the extend Discordrb::EventContainer!