gpt4 book ai didi

python - 如何在 Discord 中更新每条新消息的 message.content ?

转载 作者:行者123 更新时间:2023-12-01 01:03:45 26 4
gpt4 key购买 nike

这个问题措辞很糟糕,但是对于我正在制作的一个不和谐机器人,它会在 message.content 中查找“s!rps”,如果在聊天中发送“s!rps”,它会要求 Rock, Paper ,或剪刀。我希望它更新每条新消息的消息内容,但 message.content 保持为 s!rps 并创建无限循环。如何更新每条新消息的 message.content?

if "s!rps" in message.content:
MAI = message.author.id
msg = "Do you pick Rock, Paper, or Scissors?".format(message)
await client.send_message(message.channel, msg)
while True:
cr = random.randint(1,3)
if message.content.startswith("Rock"):
if message.author.id == MAI:
choice = 1
break
elif message.content.startswith("Paper"):
if message.author.id == MAI:
choice = 2
break
elif message.content.startswith("Scissors"):
if message.author.id == MAI:
choice = 3
break
elif message.content.startswith("rock"):
if message.author.id == MAI:
choice = 1
break
elif message.content.startswith("paper"):
if message.author.id == MAI:
choice = 2
break
elif message.content.startswith("scissors"):
if message.author.id == MAI:
choice = 3
break
else:
if message.author.id == MAI:
if message.content != "s!rps":
msg = ("\"%s\" isn't a valid input." % message.content).format(message)
await client.send_message(message.channel,msg)
if choice == "Rock":
choiceNum = 1
if choice == "Paper":
choiceNum = "2"
if choice == 3:
choiceWord = ""
if cr == 1:
crWord = "Rock"
if cr == 2:
crWord = "Paper"
if cr == 3:
crWord = "Scissors"
if (cr-choice+5)%3 == 1:
msg = ("You picked %s, I picked %s, you win!" % (choiceWord, crWord)).format(message)
await client.send_message(message.channel, msg)
if (cr-choice+5)%3 == 2:
msg = ("You picked %s, I picked %s, I win!" % (choiceWord, crWord)).format(message)
await client.send_message(message.channel, msg)
if cr == choice:
msg = ("We both picked %s, it's a tie." % choiceWord).format(message)
await client.send_message(message.channel, msg)

最佳答案

您的消息内容在整个过程中都是固定的,您永远不会更新或等待新消息。此外,该循环可能会完全阻塞您的工作流程!删除 while 循环,改用 Client.wait_for_message(如果要重写,则使用 Client.wait_for)。

我将按如下方式执行此操作,在事件内部定义一个内部函数,可在等待特定消息时用作检查:

def check(m):
answers = ["rock", "paper", "scissors"]
if any(answer == m.content.lower() for answer in answers):
return True
else:
client.send_message(m.channel, "{0.content} is not a viable option".format(m))
return False

然后,摆脱 while 循环,即将代码修改为:

    MAI = message.author.id
msg = "Do you pick Rock, Paper, or Scissors?".format(message)
await client.send_message(message.channel, msg)
answer_msg = await client.wait_for_message(author = MAI, check = check)
answer = answer_msg.content.lower()

if answer == "rock":
choice = 1
elif answer == "scissors":
.....

请注意,wait_for_message 有一个author 参数,确保消息作者确实是您想要的作者,这意味着您的if message.author.id == MAI 将被考虑在内。而且现在不区分大小写。

关于python - 如何在 Discord 中更新每条新消息的 message.content ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55565913/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com