This is the code line for that error:
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)
And this is the error I keep on receiving:
Ignoring exception in on_message
Traceback (most recent call last):
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/client.py", line 343, in _run_event
await coro(*args, **kwargs)
File "main.py", line 69, in on_message
index = int(msg.split("!del", 1)[1])
IndexError: list index out of range
I'm getting the error every time I try to run the !del command.
CodePudding user response:
The exception "IndexError: list index out of range" could happening when the msg is split but the result only contains one element. The code is trying to fetch the second element but only one element exists. See the examples below:
msg="!del"
print('one',msg.split("!del",1))
msg="something"
print('two',msg.split("!del",1))
msg="something"
print('three', msg.split("!del",1)[1])
Output:
one ['', '']
two ['something']
---------------------------------------------------------------------------
IndexError Traceback (most recent call last)
<ipython-input-13-4193103020d6> in <module>
6
7 msg="something"
----> 8 print('three',msg.split("!del",1)[1])
9
10
IndexError: list index out of range
CodePudding user response:
That is a basic python error. Kindly check out docs on split() and sequence's index.
If you're using discord.Client
object for your bot, you'll probably want to take a look at commands.Bot
object.