繁体   English   中英

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

[英]How can I make a command that can only be triggered once?

我已经发出命令,机器人会问一些问题,一旦你回答完,机器人会将你的答案发送到频道。

我的问题是您可以一遍又一遍地触发命令。 有没有办法让它只由用户运行一次,并且一旦提出所有问题就可以由该用户再次运行?

@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)

这是我的代码版本,可以满足您的要求。 我使用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代码

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM