s-hfarooq / AlmaBotter

Discord Bot for the UIUC 202x Server
https://discord.gg/2pFv4Wq
MIT License
0 stars 11 forks source link

Fix UnhandledPromiseRejectionWarning #18

Closed s-hfarooq closed 4 years ago

s-hfarooq commented 4 years ago

An UnhandledPromiseRejectionWarning error occurs when running the bot (seems to be coming when all brownout images are fetched)

Specifically, the output is

(node:170636) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'attachments' of undefined at Client. (/home/hassan/Documents/Desktop/Discord/QueenMoonBot/index.js:245:39) at Client.emit (events.js:315:20) at MessageCreateAction.handle (/home/hassan/Documents/Desktop/Discord/QueenMoonBot/node_modules/discord.js/src/client/actions/MessageCreate.js:31:14) at Object.module.exports [as MESSAGE_CREATE] (/home/hassan/Documents/Desktop/Discord/QueenMoonBot/node_modules/discord.js/src/client/websocket/handlers/MESSAGE_CREATE.js:4:32) at WebSocketManager.handlePacket (/home/hassan/Documents/Desktop/Discord/QueenMoonBot/node_modules/discord.js/src/client/websocket/WebSocketManager.js:386:31) at WebSocketShard.onPacket (/home/hassan/Documents/Desktop/Discord/QueenMoonBot/node_modules/discord.js/src/client/websocket/WebSocketShard.js:436:22) at WebSocketShard.onMessage (/home/hassan/Documents/Desktop/Discord/QueenMoonBot/node_modules/discord.js/src/client/websocket/WebSocketShard.js:293:10) at WebSocket.onMessage (/home/hassan/Documents/Desktop/Discord/QueenMoonBot/node_modules/ws/lib/event-target.js:125:16) at WebSocket.emit (events.js:315:20) at Receiver.receiverOnMessage (/home/hassan/Documents/Desktop/Discord/QueenMoonBot/node_modules/ws/lib/websocket.js:800:20) (Use node --trace-warnings ... to show where the warning was created) (node:170636) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag --unhandled-rejections=strict (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1) (node:170636) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

s-hfarooq commented 4 years ago

Fixed in latest push.

The issue was that the bot was trying to choose a random element in the brownout array while it was still empty (images hadn't been completely fetched yet)

Added a check to make sure the array length was greater than 0 to solve issue.