【发布时间】:2021-10-08 02:03:38
【问题描述】:
您好,我想编写一个 discord.py 词汇学习机器人。我的代码是这样的
@client.event
async def on_message(message):
vocab_list = ["suburb",
"timer"]
if message.content.startswith('-vokabeln'):
channel = message.channel
random_vokabel = random.choice(vocab_list)
await channel.send(random_vokabel)
if random_vokabel == "suburb":
async def check(m):
return m.author == message.author and m.channel == message.channel
msg = await client.wait_for('message', check=check)
if msg.content == 'Vorort':
await message.channel.send("richtig!")
await msg.add_reaction("➡️")
else:
await message.channel.send("Leider falsch die Lösung wäre **Vorort** gewesen")
await msg.add_reaction("➡️")
async def check(reaction, user):
return user == message.author and str(reaction.emoji) == '➡️'
reaction, user = await client.wait_for('reaction_add', check=check)
await message.channel.send(random_vokabel)
if random_vokabel == "timer":
async def check(m):
return m.author == message.author and m.channel == message.channel
msg = await client.wait_for('message', check=check)
if msg.content == 'Timer':
await message.channel.send("richtig!")
await msg.add_reaction("➡️")
else:
await message.channel.send("Leider falsch die Lösung wäre **Timer** gewesen")
await msg.add_reaction("➡️")
async def check(reaction, user):
return user == message.author and str(reaction.emoji) == '➡️'
reaction, user = await client.wait_for('reaction_add', check=check)
await message.channel.send(random_vokabel)
await client.process_commands(message)
但是当我运行这段代码时,机器人会根据自己的反应做出反应 所以机器人对用户消息做出反应,然后等待反应的事情被机器人反应激活 所以机器人会自己激活
我怎样才能让机器人不会被他自己的反应激活
另一个问题是,当等待反应被激活时,机器人会发送一个新的词汇,但在此之后我的代码将无法工作。所以机器人不会对任何事情做出反应,因为整个功能都没有被激活。机器人发送一条随机消息,但 -vokabeln 未激活 有没有办法让工作-vokabeln 和反应的东西?
最后一个也是最大的问题是我要添加大约 100 个词汇 这将使代码变得如此庞大和低效 有没有另一种方法可以用词汇和翻译制作两个列表,所以机器人选择一个词汇,然后他得到正确的翻译,他会等待正确的翻译 我只是想让代码更高效,而不是那么长
【问题讨论】:
标签: python discord.py