Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve gateway logging #169

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 14 additions & 16 deletions lib/discordrb/gateway.rb
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ def run_async
@ws_thread = Thread.new do
Thread.current[:discordrb_name] = 'websocket'
connect_loop
LOGGER.warn('The WS loop exited! Not sure if this is a good thing')
LOGGER.debug('The WS loop exited! Not sure if this is a good thing')
end

LOGGER.debug('WS thread created! Now waiting for confirmation that everything worked')
Expand Down Expand Up @@ -553,7 +553,7 @@ def process_gateway
end

def connect
LOGGER.debug('Connecting')
LOGGER.info('Connecting to gateway')

# Get the URI we should connect to
url = process_gateway
Expand Down Expand Up @@ -745,13 +745,7 @@ def handle_reconnect
# Op 9
def handle_invalidate_session
LOGGER.debug('Received op 9, invalidating session and re-identifying.')

if @session
@session.invalidate
else
LOGGER.warn('Received op 9 without a running session! Not invalidating, we *should* be fine though.')
end

@session&.invalidate
identify
end

Expand Down Expand Up @@ -799,27 +793,31 @@ def handle_internal_close(e)
def handle_close(e)
@bot.__send__(:raise_event, Events::DisconnectEvent.new(@bot))

if e.respond_to? :code
case e
when ::WebSocket::Frame::Incoming::Client
# It is a proper close frame we're dealing with, print reason and message to console
LOGGER.error('Websocket close frame received!')
LOGGER.error("Code: #{e.code}")
LOGGER.error("Message: #{e.data}")

if e.code == 4014
LOGGER.error(<<~ERROR)
You attempted to identify with privileged intents that your bot is not authorized to use
Please enable the privileged intents on the bot page of your application on the discord developer page.
Read more here https://discord.com/developers/docs/topics/gateway#privileged-intents

Your bot attempted to identify with privileged intents that it is not authorized to use.
You must either enable these for your application on the Discord Developer Portal, or
set the intents: parameter of Bot#initialize to request only the intents that you need.
Read more here: https://discord.com/developers/docs/topics/gateway#privileged-intents
https://drb.shardlab.dev/main/Discordrb/Bot.html#initialize-instance_method
ERROR
end

@should_reconnect = false if FATAL_CLOSE_CODES.include?(e.code)
elsif e.is_a? Exception
when Exception
# Log the exception
LOGGER.error('The websocket connection has closed due to an error!')
LOGGER.error('The gateway connection has closed due to an error!')
LOGGER.log_exception(e)
else
LOGGER.error("The websocket connection has closed: #{e&.inspect || '(no information)'}")
LOGGER.error("The gateway connection has closed: #{e&.inspect}")
end
end

Expand Down