在我的discord bot(python)上,我试图检测或更确切地说是检查命令中发送的消息:如果有人键入命令$start,则该命令应在while循环中检查$start命令之后发送的消息。如果发送的消息为“加入”,则应将发送消息的用户添加到列表中。我试图通过on_message()函数检测消息并将其保存在变量中,但它似乎不起作用,我认为我的代码没有多大意义,但我不知道如何正确实现它
import discord
from discord.ext import commands
client = commands.Bot(command_prefix = "$")
@client.event
async def on_ready():
print("started")
sentMsg = ""
users = []
@client.event
async def on_message(msg):
if msg.author == client.user:
return
else:
sentMsg = msg.content
print(sentMsg)
@client.command()
async def start(ctx):
print(sentMsg)
await ctx.send("starting ...")
users.append(ctx.author)
while True:
if sentMsg == "join":
ctx.send("Joined!")
sentMsg = ""
client.run(*token*)
我把sentMsg varible放在VS代码的Watch部分,它总是说“不可用”,尽管它打印了正确的值。在on_message()中将鼠标悬停在其上方时,会显示:“sentMsg”未被访问。 有人能改进我的代码吗?或者有人有更好的想法来实现这一点吗
我非常感谢任何人的帮助
您可以让bot使用client.wait_for检测消息,而不是使用on_消息事件
例如:
相关问题 更多 >
编程相关推荐