Method: Discordrb::Bot#initialize

Defined in:
lib/discordrb/bot.rb

#initialize(log_mode: :normal, token: nil, client_id: nil, type: nil, name: '', fancy_log: false, suppress_ready: false, parse_self: false, shard_id: nil, num_shards: nil, redact_token: true, ignore_bots: false, compress_mode: :large, intents: :all) ⇒ Bot

Makes a new bot with the given authentication data. It will be ready to be added event handlers to and can eventually be run with #run.

As support for logging in using username and password has been removed in version 3.0.0, only a token login is possible. Be sure to specify the type parameter as :user if you're logging in as a user.

Simply creating a bot won't be enough to start sending messages etc. with, only a limited set of methods can be used after logging in. If you want to do something when the bot has connected successfully, either do it in the EventContainer#ready event, or use the #run method with the :async parameter and do the processing after that.

Parameters:

  • log_mode (Symbol) (defaults to: :normal)

    The mode this bot should use for logging. See Logger#mode= for a list of modes.

  • token (String) (defaults to: nil)

    The token that should be used to log in. If your bot is a bot account, you have to specify this. If you're logging in as a user, make sure to also set the account type to :user so discordrb doesn't think you're trying to log in as a bot.

  • client_id (Integer) (defaults to: nil)

    If you're logging in as a bot, the bot's client ID. This is optional, and may be fetched from the API by calling #bot_application (see Application).

  • type (Symbol) (defaults to: nil)

    This parameter lets you manually overwrite the account type. This needs to be set when logging in as a user, otherwise discordrb will treat you as a bot account. Valid values are :user and :bot.

  • name (String) (defaults to: '')

    Your bot's name. This will be sent to Discord with any API requests, who will use this to trace the source of excessive API requests; it's recommended to set this to something if you make bots that many people will host on their servers separately.

  • fancy_log (true, false) (defaults to: false)

    Whether the output log should be made extra fancy using ANSI escape codes. (Your terminal may not support this.)

  • suppress_ready (true, false) (defaults to: false)

    Whether the READY packet should be exempt from being printed to console. Useful for very large bots running in debug or verbose log_mode.

  • parse_self (true, false) (defaults to: false)

    Whether the bot should react on its own messages. It's best to turn this off unless you really need this so you don't inadvertently create infinite loops.

  • shard_id (Integer) (defaults to: nil)

    The number of the shard this bot should handle. See https://github.com/discord/discord-api-docs/issues/17 for how to do sharding.

  • num_shards (Integer) (defaults to: nil)

    The total number of shards that should be running. See https://github.com/discord/discord-api-docs/issues/17 for how to do sharding.

  • redact_token (true, false) (defaults to: true)

    Whether the bot should redact the token in logs. Default is true.

  • ignore_bots (true, false) (defaults to: false)

    Whether the bot should ignore bot accounts or not. Default is false.

  • compress_mode (:none, :large, :stream) (defaults to: :large)

    Sets which compression mode should be used when connecting to Discord's gateway. :none will request that no payloads are received compressed (not recommended for production bots). :large will request that large payloads are received compressed. :stream will request that all data be received in a continuous compressed stream.

  • intents (:all, :unprivileged, Array<Symbol>, :none) (defaults to: :all)

    Gateway intents that this bot requires. :all will request all intents. :unprivileged will request only intents that are not defined as "Privileged". :none will request no intents. An array of symbols will request only those intents specified.

See Also:



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/discordrb/bot.rb', line 114

def initialize(
  log_mode: :normal,
  token: nil, client_id: nil,
  type: nil, name: '', fancy_log: false, suppress_ready: false, parse_self: false,
  shard_id: nil, num_shards: nil, redact_token: true, ignore_bots: false,
  compress_mode: :large, intents: :all
)
  LOGGER.mode = log_mode
  LOGGER.token = token if redact_token

  @should_parse_self = parse_self

  @client_id = client_id

  @type = type || :bot
  @name = name

  @shard_key = num_shards ? [shard_id, num_shards] : nil

  LOGGER.fancy = fancy_log
  @prevent_ready = suppress_ready

  @compress_mode = compress_mode

  raise 'Token string is empty or nil' if token.nil? || token.empty?

  @intents = case intents
             when :all
               ALL_INTENTS
             when :unprivileged
               UNPRIVILEGED_INTENTS
             when :none
               NO_INTENTS
             else
               calculate_intents(intents)
             end

  @token = process_token(@type, token)
  @gateway = Gateway.new(self, @token, @shard_key, @compress_mode, @intents)

  init_cache

  @voices = {}
  @should_connect_to_voice = {}

  @ignored_ids = Set.new
  @ignore_bots = ignore_bots

  @event_threads = []
  @current_thread = 0

  @status = :online

  @application_commands = {}
end