在此开始命令
@client.command()
async def rps(ctx, *, message):
ai_choices = ['rock', 'paper', 'scissors']
ai_choice = random.choice(ai_choices)
message.lower()
这里有逻辑语句
if ai_choice == message:
ctx.send(f'{message.author}, you played {message}, and Miska played {ai_choice}, so {message.author}it\'s a tie!')
elif ai_choice == 'rock' and message == 'paper':
ctx.send(f'{message.author}, you played paper, Miska played rock, so {message.author} YOU WIN!!')
elif ai_choice == 'paper' and message == 'scissors':
ctx.send(f'{message.author}, you played paper, Miska played rock, so {message.author} YOU WIN!!')
elif ai_choice == 'scissors' and message == 'rock':
ctx.send(f'{message.author}, you played rock, Miska played scissors, so {message.author} YOU WIN!!')
elif ai_choice == 'rock' and message == 'scissors':
ctx.send(f'{message.author}, you played scissors, Miska played rock, so MISKA WINS')
elif ai_choice == 'paper' and message == 'rock':
ctx.send(f'{message.author}, you played rock, Miska played paper, so MISKA WINS')
elif ai.choice == 'scissors' and message == 'paper':
ctx.send(f'{message.author}, you played paper, Miska played scissors, so MISKA WINS')
else:
ctx.send('Miska says, "You entered an invalid input please try entering ROCK, PAPER, or SCISSORS"')
在此错误日志中省略了硬盘驱动器路径
Ignoring exception in command rps:
Traceback (most recent call last):
File "core.py", line 79, in wrapped
ret = await coro(*args, **kwargs)
File "main.py", line 61, in rps
ctx.send(f'{message.author}, you played {message}, and Miska played {ai_choice}, so {message.author}it\'s a tie!')
AttributeError: 'str' object has no attribute 'author'
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "bot.py", line 863, in invoke
await ctx.command.invoke(ctx)
File "core.py", line 728, in invoke
await injected(*ctx.args, **ctx.kwargs)
File "core.py", line 88, in wrapped
raise CommandInvokeError(exc) from exc
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: AttributeError: 'str' object has no attribute 'author'
我也想不出一种更好的方法来使我的代码对所有if,elif和else都更加有效,因此,如果您有任何帮助使其更高效的方法,请告诉我,因为这是我的编程新手。 >
答案 0 :(得分:0)
在您的情况下,参数message
实际上是一个字符串,因此错误显示为'str' object has no attribute 'author'
。
通过上下文参数获取消息作者的姓名。
ctx.message.author.name
。