簡體   English   中英

discord.py 命令冷卻時間為禁令命令

[英]discord.py command cooldown for ban command

我的機器人有這個代碼,它允許禁止某些人的用戶。 但是,有沒有辦法讓工作人員每 2 小時只能使用一次禁令命令。 我希望所有其他命令都沒有冷卻時間,但對於禁止命令,有一種方法只能允許每個用戶每 2 小時使用一次。 這是我到目前為止的代碼:

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

我需要添加/更改什么來為該命令添加每個用戶 2 小時的命令冷卻時間。 我試過環顧四周,但我只找到了讓所有命令都有冷卻時間的方法,而不是這個特定的命令。 謝謝!

像這樣的東西怎么樣:

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.')

注意:記得導入asyncio 另請記住,一旦您的機器人離線,存儲在列表中的所有用戶都將被刪除。


請閱讀

更好的方法是將禁止時間與作者姓名一起存儲,並檢查當前時間是否比保存的時間至少多一個小時。 為了更安全,作者姓名和時間可以保存在數據庫或外部文本文件中,這樣如果機器人離線,您的數據就不會被刪除。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM