discord.py returns input with ValueError: invalid literal for int() with base 10: 'hey'
discord.py returns input with ValueError: invalid literal for int() with base 10: 'hey'
我正在制作一个 discord 机器人,它将接收输入并将其存储在一个 int 变量中,以便稍后将其写入文本文件:
@client.command()
async def createproject(ctx):
def check(m):
return m.author == ctx.message.author
await ctx.send("This is a test")
msg = await client.wait_for("message", check = check, timeout = 30)
desc = int(msg.content)
await ctx.send(desc)
机器人成功发送了“这是一条测试消息”,它 运行 完美无缺,直到出现此错误
指定的错误代码是:
File "c:/Users/data/Desktop/discord bot/main.py", line 101, in createproject
desc = int(msg.content)
ValueError: invalid literal for int() with base 10: 'hey'
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "C:\Users\data\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\ext\commands\bot.py", line 903, in invoke
await ctx.command.invoke(ctx)
File "C:\Users\data\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\ext\commands\core.py", line 855, in invoke
await injected(*ctx.args, **ctx.kwargs)
File "C:\Users\data\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\ext\commands\core.py", line 94, in wrapped
raise CommandInvokeError(exc) from exc
discord.ext.commands.errors.CommandInvokeError: Command raised an exception:
ValueError: invalid literal for int() with base 10: 'hey'
感谢您的帮助。
在第 9 行中,您尝试使用 desc = int(msg.content)
将 msg.content
更改为整数。但如果消息包含非数字字母,则会引发此错误。如果只想获取整数消息,可以使用 str.isdigit
函数。您可以查看:
msg = await client.wait_for("message", check = check, timeout = 30)
if msg.isdigit():
desc = int(msg.content)
因此您的代码应如下所示:
@client.command()
async def createproject(ctx):
def check(m):
return m.author == ctx.message.author
await ctx.send("This is a test")
msg = await client.wait_for("message", check = check, timeout = 30)
if msg.isdigit():
desc = int(msg.content)
else:
await ctx.send('Please type numbers only')
我正在制作一个 discord 机器人,它将接收输入并将其存储在一个 int 变量中,以便稍后将其写入文本文件:
@client.command()
async def createproject(ctx):
def check(m):
return m.author == ctx.message.author
await ctx.send("This is a test")
msg = await client.wait_for("message", check = check, timeout = 30)
desc = int(msg.content)
await ctx.send(desc)
机器人成功发送了“这是一条测试消息”,它 运行 完美无缺,直到出现此错误
指定的错误代码是:
File "c:/Users/data/Desktop/discord bot/main.py", line 101, in createproject
desc = int(msg.content)
ValueError: invalid literal for int() with base 10: 'hey'
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "C:\Users\data\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\ext\commands\bot.py", line 903, in invoke
await ctx.command.invoke(ctx)
File "C:\Users\data\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\ext\commands\core.py", line 855, in invoke
await injected(*ctx.args, **ctx.kwargs)
File "C:\Users\data\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\ext\commands\core.py", line 94, in wrapped
raise CommandInvokeError(exc) from exc
discord.ext.commands.errors.CommandInvokeError: Command raised an exception:
ValueError: invalid literal for int() with base 10: 'hey'
感谢您的帮助。
在第 9 行中,您尝试使用 desc = int(msg.content)
将 msg.content
更改为整数。但如果消息包含非数字字母,则会引发此错误。如果只想获取整数消息,可以使用 str.isdigit
函数。您可以查看:
msg = await client.wait_for("message", check = check, timeout = 30)
if msg.isdigit():
desc = int(msg.content)
因此您的代码应如下所示:
@client.command()
async def createproject(ctx):
def check(m):
return m.author == ctx.message.author
await ctx.send("This is a test")
msg = await client.wait_for("message", check = check, timeout = 30)
if msg.isdigit():
desc = int(msg.content)
else:
await ctx.send('Please type numbers only')