我想编写一个函数,当用户回答 Y
时,机器人发送消息“重新加载幸福”,否则,如果用户回答 N
,则发送消息晚安。我试图运行此代码,该代码回复“嗨”,但之后,当我输入 Y 或 N 时,机器人无法回复。我的代码示例:
@client.event
async def on_message(message):
if message.author == client.user:
return
# if message.content.startswith('$inspire'):
# quote = get_quote()
# await message.channel.send(quote)
if message.content.startswith('hi'): # Delete this after testing
await message.channel.send('Congratulations! Your Disgotchi is hatched! (Play around? : Y/N)')
if message.content.includes('y', 'Y'):
await message.channel.send('Happiness reloaded! ( ^Θ^)❤️')
elif message.content.includes('n', 'N'):
await message.channel.send('I will go to bed, good night!')
答案 0 :(得分:-2)
应该做你想做的 您所要做的就是将 id 存储在回调之外,期望同一回调中的另一条消息随着消息更新的变化而无用
import discord
from discord.ext.commands import Bot
bot = Bot(command_prefix='$')
y_users = set() # set is best for this
@bot.event
async def on_ready():
print(f'Bot connected as {bot.user}')
@bot.event
async def on_message(message):
if message.content == 'hi':
await message.channel.send('Congratulations! Your Disgotchi is hatched! (Play around? : Y/N)')
y_users.add(message.author.id) # saving the id
# checking if id is in set which means user sent hello previously
elif (message.content.startswith('y', 'Y') and message.author.id in y_users):
await message.channel.send('Happiness reloaded! ( ^Θ^)❤️')
y_users.remove(message.author.id)
elif (message.content.startswith('n', 'N') and message.author.id in y_users):
await message.channel.send('I will go to bed, good night!')
y_users.remove(message.author.id)
bot.run("token")
如果它符合您的要求,请将其标记为正确的解决方案