TimboKZ / discord-spoiler-bot

🗣️🛑 Spoiler support for Discord
https://www.npmjs.com/package/discord-spoiler-bot
MIT License
61 stars 19 forks source link

Bot frequently stops due to unhandled error event #29

Open yokuyuki opened 6 years ago

yokuyuki commented 6 years ago

The stacktrace contains:

events.js:188
      throw err;
      ^

Error: Unhandled "error" event. ([object Object])
    at Client.emit (events.js:186:19)
    at WebSocketConnection.onError (/home/yokuyuki/spoilerbot/node_modules/discord.js/src/client/websocket/WebSocketConnection.js:374:17)
    at WebSocket.onError (/home/yokuyuki/spoilerbot/node_modules/discord.js/node_modules/ws/lib/event-target.js:128:16)
    at emitOne (events.js:116:13)
    at WebSocket.emit (events.js:211:7)
    at WebSocket.finalize (/home/yokuyuki/spoilerbot/node_modules/discord.js/node_modules/ws/lib/websocket.js:185:12)
    at ClientRequest._req.on (/home/yokuyuki/spoilerbot/node_modules/discord.js/node_modules/ws/lib/websocket.js:641:12)
    at emitOne (events.js:116:13)
    at ClientRequest.emit (events.js:211:7)
    at HTTPParser.parserOnIncomingClient [as onIncoming] (_http_client.js:551:21)
    at HTTPParser.parserOnHeadersComplete (_http_common.js:115:23)
    at TLSSocket.socketOnData (_http_client.js:440:20)
    at emitOne (events.js:116:13)
    at TLSSocket.emit (events.js:211:7)
    at addChunk (_stream_readable.js:263:12)
    at readableAddChunk (_stream_readable.js:250:11)
TimboKZ commented 6 years ago

Interesting, not sure how easy it will be to reproduce.

How are you running the bot? Are you passing in an external bot instance?

yokuyuki commented 6 years ago

I have it set up in the same way as specified in basic usage. If I leave the bot running all the time, it happens once a week when I notice the bot has left the channel because it is killed due to the above error.

TimboKZ commented 6 years ago

Fair enough, will take a look later today.

Meanwhile you might wanna use a process manager like pm2 to automatically restart the bot after it crashes.

yokuyuki commented 6 years ago

Based on https://github.com/discordjs/discord.js/issues/2523 it seems like maybe client just need to handle the error. Maybe something as simple as client.on('error', log.error). Thanks for the tip about the process manager though.

Taelkir commented 5 years ago

Did adding client.on('error', log.error) prevent the issue happening again for you? I added something like that to my bot a few weeks ago and I'm still getting the error you described on a regular basis

GuimDev commented 5 years ago

Do you have an idea about the origin ?

XenHat commented 5 years ago

I'm also having this issue, when using the bot by passing it to an external instance. It seems to happen pretty reliably when sending spoiler creation requests through <post>:spoils:<topic> more than once every 5 seconds, although it still crashes if I want more (10, 15 seconds)

EDIT: I ran the bot standalone and I am also experiencing these issues:

[Nov 5, 2018, 20:27] Discord Spoiler Bot is running!
[Nov 5, 2018, 20:27] Processed 'Test' spoiler from XenHat.
[Nov 5, 2018, 20:27] Processed 'Test' spoiler from XenHat.
[Nov 5, 2018, 20:27] Processed 'Test' spoiler from XenHat.

Then it died. The input text was: Test:spoiler:A Test!

Adding client.on('error'... in various places did not produce any output, either I did not put the code in the right function, or it's not catching anything.

I also tried hooking the node instance to Google Chrome in debugging mode, and nothing new came out of that either.

Gravitygrey commented 5 years ago

It's from the computer/bot going idle, it stops its self from just being on all the time... This may just be discord kicking you offline also from your bot not being used... Try adding a small thing that makes your bot want to always do something like show it typing on a text server every 5 min or so... Also, if this doesn't work try moving your bot to a host (I don't recommend glitch) or buy a Raspberry Pi 3b for like $35 and run it on that... I'm about to make the change to the pi here soon knowing the pi will keep it running.... I don't recommend running your bot on your computer either because you might not have the hardware rated for 24/7 usage like me (my read and write speeds are now cut to a small percentage of what it used to be and the ram was always bad on this computer)

Edit/Summary: its not really a bug either way... just make your computer not turn off or the bot go inactive for a bit or read above and check other options...

yokuyuki commented 5 years ago

@Gravitygrey already running it on a host so that's not the issue. I mostly got around it using pm2 to restart the bot so for the most part it's not noticeable now, but I don't think that's an actual fix to the issue.

ipadpuppydogdude1 commented 5 years ago

It's your computer being disconnected from wifi, simple.

Thedude7054 commented 5 years ago

Can we get some light shed on this? This is still a problem for many of us in the community.

AzureeDev commented 5 years ago

Hi,

What you can do is a simple change in node_modules/discord.js/src/client/websocket/WebSocketConnection.js

Line 364, change the whole function onError(err) to:

onError(error) {
    if (error && error.message === 'uWs client connection error') {
      this.reconnect();
      return;
    }

    this.reconnect();
    return;
  }

Which will force a connection instead of crashing on that error.

Swifty2019 commented 4 years ago

this fixed my problem. I slightly altered mwSora's version -- this.reconnect() errored out for me

client.on('error', error => { if (error && error.message === 'uWs client connection error') { this.reconnect(); return; } });

XenHat commented 4 years ago

Can we get some light shed on this? This is still a problem for many of us in the community.

Yes, as other said, after my own investigations and asking a few other devs, this is a problem with discord.js timing/erroring out on some long-time connections. Simply catching the error and forcing the client to reconnect/restart will resolve the issue.

Another (less helpful) way to say this is "upstream issue".

TimboKZ commented 4 years ago

Hi people, can I ask you what are you using the bot for?

I think built-in spoiler functionality covers all use cases, or am I missing something?