Python 如何为discord bot编码是/否功能
我想编写一个函数,在这个函数中,当用户回答Python 如何为discord bot编码是/否功能,python,discord.py,Python,Discord.py,我想编写一个函数,在这个函数中,当用户回答Y时,bot会发送消息“happiness reloated”,否则,如果用户回答N,bot会发送消息“晚安”。我曾尝试运行此代码,它会回复“hi”,但之后,当我键入Y或N时,bot无法回复。我的代码示例: @client.event 异步def on_消息(消息): 如果message.author==client.user: 返回 #如果message.content.startswith(“$inspire”): #quote=get_quote(
Y
时,bot会发送消息“happiness reloated”,否则,如果用户回答N
,bot会发送消息“晚安”。我曾尝试运行此代码,它会回复“hi”,但之后,当我键入Y或N时,bot无法回复。我的代码示例:
@client.event
异步def on_消息(消息):
如果message.author==client.user:
返回
#如果message.content.startswith(“$inspire”):
#quote=get_quote()
#等待消息。频道。发送(报价)
如果message.content.startswith('hi'):#测试后删除此项
等待消息。频道。发送('祝贺您!您的非哥特语已孵化!(玩游戏?:Y/N)'))
如果message.content.includes('y','y'):
等待消息。频道。发送('幸福重新加载( ^Θ^)❤️')
elif message.content.includes('n','n'):
等待消息。频道。发送('我要睡觉了,晚安!')
应该做你想做的事
您所要做的就是将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")
如果它符合您的要求,请将其标记为正确的解决方案您使用这种方法处理此类问题,这是一个糟糕的解决方案