Discord 机器人类型错误
Discord Bot TypeError
我正在尝试制作一个不和谐的机器人游戏,玩家说 "hit" 并且怪物会受到随机数量的伤害。怪物受到一定程度的伤害后,他们死亡,新的怪物出现。
但是,当我在 discord 中输入 "hit" 时,我收到一条错误消息
TypeError: send_message() takes from 2 to 3 positional arguments but 7 were given
这是我的代码:
@client.command(pass_context=True)
async def hit (ctx):
global HP
damage = random.randrange(50,500)
HP -= damage
if HP > 0 :
await client.say('The monster took', damage, 'damage and has', HP, 'health left')
else :
await client.say('The monster has died! Another one approaches...')
HP = random.randrange(600,1000)
谁能告诉我哪里出了问题以及如何解决。谢谢!
将字符串传递给 client.say()
时,您 creating/concatenating 字符串不正确
client.say('The monster took', damage, 'damage and has', HP, 'health left')
这里每次出现逗号时,都会将其视为传递给函数的新参数。
您需要创建字符串并将其作为一个变量传递:
client.say('The monster took ' + damage + ' damage and has ' + HP + ' health left')
注意这里使用 +
而不是 ,
来连接字符串。
我正在尝试制作一个不和谐的机器人游戏,玩家说 "hit" 并且怪物会受到随机数量的伤害。怪物受到一定程度的伤害后,他们死亡,新的怪物出现。 但是,当我在 discord 中输入 "hit" 时,我收到一条错误消息
TypeError: send_message() takes from 2 to 3 positional arguments but 7 were given
这是我的代码:
@client.command(pass_context=True)
async def hit (ctx):
global HP
damage = random.randrange(50,500)
HP -= damage
if HP > 0 :
await client.say('The monster took', damage, 'damage and has', HP, 'health left')
else :
await client.say('The monster has died! Another one approaches...')
HP = random.randrange(600,1000)
谁能告诉我哪里出了问题以及如何解决。谢谢!
将字符串传递给 client.say()
client.say('The monster took', damage, 'damage and has', HP, 'health left')
这里每次出现逗号时,都会将其视为传递给函数的新参数。
您需要创建字符串并将其作为一个变量传递:
client.say('The monster took ' + damage + ' damage and has ' + HP + ' health left')
注意这里使用 +
而不是 ,
来连接字符串。