krisppurg / dimscord

A Discord Bot & REST Library for Nim.
https://krisppurg.github.io/dimscord/
MIT License
222 stars 20 forks source link

Error: unhandled exception: value out of range: 18 notin 0 .. 14 #97

Closed GeeLeonidas closed 1 year ago

GeeLeonidas commented 1 year ago

Description

I've recently noticed that my bot crashed every time it was trying to play audio, apparently a new voice op code is being given to be handled and it is not defined in the VoiceOp enum. Error message is the one in the title. Traceback points to dimscord-#head/dimscord/voice.nim(398) handleSocketMessageIter: https://github.com/krisppurg/dimscord/blob/41eea2aeafe2f6ea3ab861efa8e1d4ddbf214dc1/dimscord/voice.nim#L398

Temporary solution

Quite inelegant and probably troublesome, but hey it works™ (i.e. stops crashing):

const VoiceOpRange = VoiceOp.low.ord .. VoiceOp.high.ord
let
  dataOp = data["op"].num
  dataVoiceOp = if dataOp in VoiceOpRange: VoiceOp(dataOp) else: Something
case dataVoiceOp
of # ...

How to reproduce

Using examples/voice.nim and dimscord#head should suffice (https://github.com/krisppurg/dimscord/blob/41eea2aeafe2f6ea3ab861efa8e1d4ddbf214dc1/examples/voice.nim)

krisppurg commented 1 year ago

Think it would be better to simply change VoiceOp to const. Will fix in a day or two.

ire4ever1190 commented 1 year ago

Think the solution given is best

Add an Unknown value to the enum which is used when the opcode isn't in the range of FirstGoodValue..LastValue

Switching to const would remove some type safety and make it a bit more unclean imo

krisppurg commented 1 year ago

The issue is fixed, should be fine now.