0

I am programming a discord bot using the following link: https://www.youtube.com/watch?v=SPTfmiYiuok

It's a bot that's meant to give an inspiring message when called, and users can add and delete said messages as well. The problem I am facing :

A) Whenever I type anything, it gives the received command of 'New encouraging message added', even though I'm not calling that function.

B) The code itself is giving me an error stating that the list index is out of range.

For reference, here is the block of code in which the error message is occurring, and the range of which I suspect the error to be at. The actual error is the line towards the bottom:

index = int(msg.split("+del",1)[1])'):

And here is the code

if msg.startswith('+inspire'):
    quote = get_quote()
    await message.channel.send(quote)

  options = starter_encouragements
  if "encouragements" in db.keys():
    options = options + db["encouragements"].value

  if any(word in msg for word in sad_words):
    await message.channel.send(random.choice(options))

  if msg.startswith('+new'):
    encouraging_message = msg.split("+new ",1)[1]
    update_encouragements(encouraging_message)
  await message.channel.send("New encouraging message added :)")

  if msg.startswith("+del"):
    encouragements = []
  if "encouragements" in db.keys():
    index = int(msg.split("+del",1)[1])
    delete_encouragement(index)
    encouragements = db["encouragements"]
    await 
message.channel.send(encouragements)
4
  • 1
    Why you have added android and ios tags? Commented Nov 20, 2021 at 3:19
  • Try adding curly braces { } to your IF statements, I think that with the lack of whitespace, await message.channel.send("New encouraging message added :)") is always firing. Commented Nov 20, 2021 at 3:26
  • Update: I was able to figure out the error message given within the actual consul, but I still can't figure out why whenever I type, it gives me the received message of 'new encouragement message added'. I have tried using the curly braces as Luke Duncan suggested, but that just gave an invalid syntax suggestion.
    – Snickzoryx
    Commented Nov 20, 2021 at 3:30
  • @Snickzoryx apologies, I couldn't remember if Python had curlys or not but the latest answer seems to be correct. Commented Nov 20, 2021 at 3:40

1 Answer 1

2

I believe the error within your code has to do with your indentation. Within python, indentations are very important for ensuring what code executes, and within what control structure.

First error: Sending 'New encouraging message added' after every message is sent Due to an indentation error, you have this code running every time a message a sent. This can be fixed by changing

if msg.startswith('+new'):
  encouraging_message = msg.split("+new ",1)[1]
  update_encouragements(encouraging_message)
await message.channel.send("New encouraging message added :)")

to

if msg.startswith('+new'):
  encouraging_message = msg.split("+new ",1)[1]
  update_encouragements(encouraging_message)
  await message.channel.send("New encouraging message added :)")

This change makes sure that you will only send "New encouraging message added :)" when a message start with +new

Second error: Sending 'list index is out of range'

You currently use the split function, but because of indentation this code also runs regardless of whether a message starts with +del This can be fixed by changing

  if msg.startswith("+del"):
    encouragements = []
  if "encouragements" in db.keys():
    index = int(msg.split("+del",1)[1])
    delete_encouragement(index)
    encouragements = db["encouragements"]
    await message.channel.send(encouragements)

to

  if msg.startswith("+del"):
    encouragements = []
    if "encouragements" in db.keys():
      index = int(msg.split("+del",1)[1])
      delete_encouragement(index)
      encouragements = db["encouragements"]
      await message.channel.send(encouragements)

This change makes sure that you will never receive the error "list index is out of range" because msg will contain "+del", causing msg.split(+del",1) to return a list containing at least two items.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.