I'm trying to make a event so that whenever someone send a message, the bot will respond with the message with ever other character swapped case. This is what I have so far:
@client.event
async def on_message(message):
username = message.author.name
if message.author == client.user:
return
exp = []
for index, element in message.content:
if index % 2 == 0:
exp.append(element.swapcase())
else:
exp.append(element)
response = "".join(exp)
await message.channel.send(response)
When I run this i get a error message in Python saying:
Ignoring exception in on_message
Traceback (most recent call last):
File "C:UsersProgrammerPycharmProjectsDiscordBottingvenvlibsite-packagesdiscordclient.py", line 343, in _run_event
await coro(*args, **kwargs)
File "C:UsersProgrammerPycharmProjectsDiscordBottingot.py", line 33, in on_message
for index, element in x: # message.content
ValueError: not enough values to unpack (expected 2, got 1)
EDIT: So I used a different method of iterating over the string shown below:
@client.event
async def on_message(message):
username = message.author.name
if message.author == client.user:
return
exp = []
for i in range(len(message.content)):
if i % 2 == 0:
exp.append(message.content[i].swapcase())
else:
exp.append(message.content[i])
response = "".join(exp)
await message.channel.send(response)
However I still don't know why the first method didn't work.
question from:
https://stackoverflow.com/questions/66064239/valueerror-not-enough-values-to-unpack-using-discord-api 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…