简体   繁体   English

discord.py 命令冷却时间为禁令命令

[英]discord.py command cooldown for ban command

I've got this code for my bot, which allows banning users for certain people.我的机器人有这个代码,它允许禁止某些人的用户。 However, is there a way to make it so that a staff member can only use the ban command once every 2 hours.但是,有没有办法让工作人员每 2 小时只能使用一次禁令命令。 I want every other command to have no cooldown, but just for the ban command, have a way to only allow it to be used once per 2 hours per user.我希望所有其他命令都没有冷却时间,但对于禁止命令,有一种方法只能允许每个用户每 2 小时使用一次。 Here's the code I've got so far:这是我到目前为止的代码:

@commands.has_permissions(administrator=True)
async def pogoban (ctx, member:discord.User=None, *, reason =None):
    if member == None or member == ctx.message.author:
        await ctx.channel.send("You cannot ban yourself")
        return
    if reason == None:
        reason = "For breaking the rules."
    message = f"You have been banned from {ctx.guild.name} for {reason}"
    await member.send(message)
    await ctx.guild.ban(member, reason=reason)
    await ctx.channel.send(f"{member} is banned!")

What would I need to add/change to add a command cooldown of 2 hours per user for this command.我需要添加/更改什么来为该命令添加每个用户 2 小时的命令冷却时间。 I've tried looking around, but I've only found ways to make all commands have a cooldown, instead of this one specific command.我试过环顾四周,但我只找到了让所有命令都有冷却时间的方法,而不是这个特定的命令。 Thanks!谢谢!

How about something like this:像这样的东西怎么样:

cooldown = []

@client.command()
async def pogoban(ctx, member: discord.Member = None, *, reason = None):
    author = str(ctx.author)
    if author in cooldown:
        await ctx.send('Calm down! You\'ve already banned someone less that two hours ago.')
        return

    try:
        if reason == None:
            reason = 'breaking the rules.'
        await member.send(f'You have been banned from **{ctx.guild.name}** for **{reason}**')
        await member.ban(reason = reason)
        await ctx.send(f'{member.mention} has been banned.')
        cooldown.append(author)
        await asyncio.sleep(2 * 60 * 60)    #The argument is in seconds. 2hr = 7200s
        cooldown.remove(author)
    except:
        await ctx.send('Sorry, you aren\'t allowed to do that.')

Note: Remember to import asyncio .注意:记得导入asyncio Also remember that once your bot goes offline, all the users stored in the list will be erased.另请记住,一旦您的机器人离线,存储在列表中的所有用户都将被删除。


Please Read请阅读

A better approach would be to store the time of banning along with the the authors name and check if the current time is at least an hour more than the saved time.更好的方法是将禁止时间与作者姓名一起存储,并检查当前时间是否比保存的时间至少多一个小时。 And to make it a lot safer, the authors name along with the time can be saved in a database or an external text file, this way your data won't get erased if the bot goes offline.为了更安全,作者姓名和时间可以保存在数据库或外部文本文件中,这样如果机器人离线,您的数据就不会被删除。

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

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