如何制作只能被触发一次的命令?

时间:2020-07-20 13:54:42

标签: python discord.py

我已经下达命令,机器人会询问一些问题,一旦您回答完毕,机器人就会将您的答案告知频道。

我的问题是您可以反复触发该命令。有没有办法让它仅由用户运行一次,并在所有问题被询问后可以再次由该用户运行?

@commands.command(name='test')
@cooldown(1, 60)
async def test(self, ctx):
    if not ctx.guild:
        return
    test = []
    q1 = ['test', 'test']
    channel = self.client.get_channel(733649176373755945)
    dm = await ctx.author.create_dm()

    def check(author):
        def inner_check(message):
            if message.author != author:
                return False
            try:
                str(message.content)
                return True
            except ValueError:
                return False

        return inner_check

    for question in q1:
        await dm.send(question)
        msg = await self.client.wait_for('message', check=check(ctx.author))
        test.append(msg.content)

    answers = "\n".join(f'{a}. {b}' for a, b in enumerate(test, 1))
    submit = f'\n{answers}'
    await channel.send(submit)

1 个答案:

答案 0 :(得分:1)

这是您的代码的我的版本,它可以满足您的要求。我使用self.test.reset_cooldown(ctx)重设冷却时间:

from discord.ext import commands
from discord_util import message_check

class MyCog(commands.Cog):
    def __init__(self, bot):
        self.bot = bot
    @commands.command()
    @commands.cooldown(1, 100, commands.BucketType.user)
    @commands.dm_only()
    async def test(self, ctx):
        answers = []
        questions = ["test1", "test2"]
        for question in questions:
            dm = await ctx.author.send(question)
            msg = await self.bot.wait_for('message', check=message_check(channel=dm.channel, author=ctx.author))
            answers.append(msg.content)
        answer_str = "\n".join(f'{a}. {b}' for a, b in enumerate(answers, 1))
        await ctx.send(f"\n{answer_str}")
        self.test.reset_cooldown(ctx)

您可以找到我的message_check code here