简体   繁体   English

discord.py 返回带有 ValueError 的输入:int() 的无效文字,基数为 10:'hey'

[英]discord.py returns input with ValueError: invalid literal for int() with base 10: 'hey'

I'm making a discord bot that will take input and store it in a int variable for later writing it into a text file:我正在制作一个不和谐机器人,它将接受输入并将其存储在一个 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)

The bot succesfully sends the "This is a test message" and it had ran flawlessly untill this error机器人成功地发送了“这是一条测试消息”并且它一直完美运行,直到出现这个错误

The error code specified is:指定的错误代码是:

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'

Thank you for your help.感谢您的帮助。

In line 9, you tried to change the msg.content to integer with desc = int(msg.content) .在第 9 行中,您尝试使用desc = int(msg.content)msg.content更改为整数。 But if a message includes a non numeric letter, then this error will raise.但是,如果消息包含非数字字母,则会引发此错误。 If you just want to get integer messages, you can use str.isdigit function.如果您只想获取整数消息,可以使用str.isdigit函数。 You can check it:你可以检查一下:

msg = await client.wait_for("message", check = check, timeout = 30)
if msg.isdigit():
    desc = int(msg.content)

So your code should seem like:所以你的代码应该是这样的:

@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')

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Discord.py音乐bot,使用文本文件保存音量整数,但我得到ValueError:int()的无效文字,基数为10:” - Discord.py music bot, using a text file to save volume integer but I get ValueError: invalid literal for int() with base 10: '' ValueError: int() 以 10 为基数的无效文字:'' 询问输入时 - ValueError: invalid literal for int() with base 10: '' when asking for input 输入用户名和密码= ValueError:int()以10为基的无效文字:'' - Input Username and Password = ValueError: invalid literal for int() with base 10: '' Django:ValueError:基数为10的int()的无效文字: - Django: ValueError: invalid literal for int() with base 10: ValueError:int()以10为底的无效文字:'' - ValueError:invalid literal for int() with base 10:' ' ValueError:以10为底的int()的无效文字:-CharField - ValueError: invalid literal for int() with base 10: - CharField ValueError:int() 的无效文字,基数为 10:'' 错误 - ValueError: invalid literal for int() with base 10: '' error pandas ValueError:以10为底的int()的无效文字:'' - pandas ValueError: invalid literal for int() with base 10: '' ValueError以10为底的int()的无效文字:'' - ValueError invalid literal for int() with base 10: '' ValueError:int() 的无效文字,基数为 10: '' (Tkinter) - ValueError: invalid literal for int() with base 10: '' (Tkinter)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM