【问题标题】:How do I check if the same user replies to my discord.py bot如何检查同一用户是否回复了我的 discord.py 机器人
【发布时间】:2021-02-13 20:36:59
【问题描述】:

所以基本上我正在尝试制作一个投票机器人(受 PollMaster 机器人的启发)并开始制作投票创建向导,但目前任何人都可以回复我的机器人:

#Basic Poll Creation Wizard Command
@client.command(name = 'create')
async def create(context):

    #Author Check Function
    def check(author):
        def inner_check():
            if message.author != author:
                return False
            else:
                return True
        return inner_check

    #Sets The Question For The Poll
    setquestion = discord.Embed(title = '**Poll Maker**', color = 0x0f00ff)
    author = context.message.author
    setquestion.set_footer(text = f'{str(author)[:-5]}\'s poll', icon_url = context.message.author.avatar_url)
    setquestion.add_field(name = f'What are you asking?', value = 'What is the question you have, try to be as descriptivve \nas possible without typing an annoying long question.')
    await context.message.channel.send(author.mention, embed = setquestion)
    question = (await client.wait_for('message', check=check, timeout=30)).content

    #Determines Whether Or Not The Author Wants The Poll To be Anonymous
    isanonymous = discord.Embed(title = '**Poll Maker**', description = f'Nice, you\'ve got your question; now decide \nwhether or not you want the poll to be anonymous.\n \n`0 - No`\n`1 - Yes`\n \nAn anonymous poll has the following effects: \n:small_blue_diamond: You will never see who voted for which option', color = 0x0f00ff)
    isanonymous.set_footer(text = f'{str(author)[:-5]}\'s poll', icon_url = context.message.author.avatar_url)
    await context.message.channel.send(author.mention, embed = isanonymous)
    anonymous = (await client.wait_for('message', check=check, timeout=30)).content
    while True:
        try:
            anonymous = int(anonymous)
            break
        except:
            await context.message.channel.send(f'{author.mention} **Please enter a valid number (either 0 or 1)**', delete_after = 3)
            anonymous = (await client.wait_for('message', check=check, timeout=30)).content

这是我目前所拥有的,我想确保只有原始消息的作者才能回复我的机器人。感谢您的帮助!

【问题讨论】:

    标签: python discord.py


    【解决方案1】:

    你的检查功能错误

    def check(author):
        def inner(message):
            return message.author == author
        return inner
    
    # To use it
    m = await client.wait_for('message', check=check(context.author))
    

    如果你想在没有内部函数的情况下使用它

    def check(message):
        return message.author == context.author
    
    m = await client.wait_for('message', check=check)
    

    【讨论】:

      猜你喜欢
      • 2022-08-13
      • 2021-10-14
      • 2020-07-29
      • 2021-10-13
      • 2021-07-28
      • 2018-02-03
      • 2018-07-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多