Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何生成只能触发一次的命令?_Python_Discord.py - Fatal编程技术网

Python 如何生成只能触发一次的命令?

Python 如何生成只能触发一次的命令?,python,discord.py,Python,Discord.py,我已发出命令,机器人将询问一些问题,一旦您回答完毕,机器人将向频道发送您的答案 我的问题是你可以一次又一次地触发命令。有没有一种方法可以让一个用户只运行一次,并且在所有问题被问到后,该用户可以再次运行 @commands.command(name='test') @cooldown(1, 60) async def test(self, ctx): if not ctx.guild: return test = [] q1 = ['test', 'test

我已发出命令,机器人将询问一些问题,一旦您回答完毕,机器人将向频道发送您的答案

我的问题是你可以一次又一次地触发命令。有没有一种方法可以让一个用户只运行一次,并且在所有问题被问到后,该用户可以再次运行

@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\u冷却时间(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)

你可以找到我的

共享你的代码,基本上就是这样。你需要保存关于用户的数据,这意味着在工作内存中有一个列表(或一个单独的文件或数据库等),你可以管理并重复更新。没有“简单的一种方法”可以做到这一点,discord.py当然没有内置的功能。。唯一的区别是,您将指定每个用户的冷却时间,而不是全局冷却时间感谢回复!所以我只添加@cooldown(1,#)?忘记自我。。。哈哈。冷却开始工作了,谢谢!但是我的check msg=wait self.client.wait_for('message',check=message_check(channel=dm.channel,author=ctx.author))AttributeError:'DMChannel'对象没有属性'channel',您有一些代码
dm=wait ctx.author.create_dm()
,而在我的代码
dm
中是我们发送给用户的消息。(您不需要手动获取DM频道,因为您可以
直接向用户发送
。)非常感谢Pat!:)