简体   繁体   English

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

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

I've made a command that the bot will ask some questions and once you are done answering it the bot will message your answers to a channel..我已经发出命令,机器人会问一些问题,一旦你回答完,机器人会将你的答案发送到频道。

My problem is that you can trigger the command over and over.我的问题是您可以一遍又一遍地触发命令。 Is there a way to make it run only once by a user and can be run again by that user once all of the question is asked?有没有办法让它只由用户运行一次,并且一旦提出所有问题就可以由该用户再次运行?

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

Here's my version of your code, which does what you want.这是我的代码版本,可以满足您的要求。 I use self.test.reset_cooldown(ctx) to reset the cooldown:我使用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)

You can find my message_check code here你可以在这里找到我的message_check代码

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

相关问题 如何只使某个条件在方法中运行一次? - How can I only make a certain condition run once in a method? 如何使 `else` 块在此代码中仅出现一次? - How can I make the `else` block only occur once in this code? 如何让我的列表只返回列表中的每个元素一次? - How can I make my list return each element only once in a list? 我如何才能团结一致地只找到一次我的测试方法? - How can I make unitest to find my single test method only once? 我怎样才能使它只有具有特定角色的成员才能执行命令? - How can I make it so only members with a specific role can execute a command? 我如何制作 Spotify 命令? - How can i make a spotify command? 如何一次对多个OID执行SNMP命令? - How can I perform SNMP command for several OIDs at once? 如何在Fabric中以并行模式运行命令? - How can I run a command once in parallel mode in fabric? 每次我运行 pyinstaller 可执行文件时,pyngrok 都会下载 ngrok。 我怎样才能让它只下载一次 ngrok? - pyngrok downloads ngrok each time i run the pyinstaller executable. How can i make it download ngrok only once? 我将如何做到这一点,以便个人是唯一可以使用 discord 机器人运行命令的人? - How would I make it so that an individual person is the only one who can run a command with a discord bot?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM