Skip to content

Instantly share code, notes, and snippets.

@AwSkies
Last active April 13, 2023 17:59
Show Gist options
  • Save AwSkies/6128d89a1e842b4e802b3a077d7f98a5 to your computer and use it in GitHub Desktop.
Save AwSkies/6128d89a1e842b4e802b3a077d7f98a5 to your computer and use it in GitHub Desktop.
commands with whitelisted cooldowns for discord.py rewrite
import discord
from discord.ext import commands
from discord.ext.commands.cooldowns import BucketType
bot = commands.Bot(command_prefix = "your prefix here")
token = 'token goes here'
#on ready login message
@bot.event
async def on_ready():
print('Logged in as {} - {}'.format(bot.user.name, bot.user.id))
#rate: how many times the command can be used before triggering the cooldown
rate = 1
#per: the amount of seconds the cooldown lasts
per = 10
#type: the scope of the cooldown, go to https://discordpy.readthedocs.io/en/latest/ext/commands/api.html#discord.ext.commands.cooldown for more info
t = BucketType.default
#example command
@bot.command(
name = 'command',
description = 'example description',
)
#cooldown
@commands.cooldown(rate, per, t)
async def example_command(ctx):
#put your command things here
pass
#whitelist: the list of IDs of people/roles/channels/things to ignore
#to whitelist someone/something, add their ID to the whitelist
whitelist = [123456789, 987654321]
@example_command.after_invoke
async def reset_cooldown(ctx):
for e in whitelist:
#to whitelist a person:
if e == ctx.author.id:
example_command.reset_cooldown(ctx)
#to whitelist a channel:
if e == ctx.message.channel.id:
example_command.reset_cooldown(ctx)
#to whitelist a guild/server:
if e == ctx.message.guild.id:
example_command.reset_cooldown(ctx)
#to whitelist a role:
if e in [role.id for role in ctx.author.roles]:
example_command.reset_cooldown(ctx)
bot.run(token)
import discord
from discord.ext import commands
from discord.ext.commands.cooldowns import BucketType
#rate: how many times the command can be used before triggering the cooldown
rate = 1
#per: the amount of seconds the cooldown lasts
per = 10
#type: the scope of the cooldown, go to https://discordpy.readthedocs.io/en/latest/ext/commands/api.html#discord.ext.commands.cooldown to see what they are
t = BucketType.default
class MyCog(commands.Cog):
def __init__(self, bot):
self.bot = bot
#whitelist: the list of IDs of people/roles/channels/things to ignore
#to whitelist someone/something, add their ID to the whitelist
self.whitelist = [123456789, 987654321]
#example command
@bot.command(
name = 'command',
description = 'example description',
)
#cooldown
@commands.cooldown(rate, per, t)
async def example_command(self, ctx):
#put your command things here
pass
@example_command.after_invoke
async def reset_cooldown(self, ctx):
for e in self.whitelist:
#to whitelist a person:
if e == ctx.author.id:
self.example_command.reset_cooldown(ctx)
#to whitelist a channel:
if e == ctx.message.channel.id:
self.example_command.reset_cooldown(ctx)
#to whitelist a guild/server:
if e == ctx.message.guild.id:
self.example_command.reset_cooldown(ctx)
#to whitelist a role:
if e in [role.id for role in ctx.author.roles]:
self.example_command.reset_cooldown(ctx)
#cog setup function
def setup(bot):
bot.add_cog(MyCog(bot))
@xjunko
Copy link

xjunko commented Nov 27, 2019

i used cogs how can i turn this into cogs compatible do i just add async def reset_cooldown(self ,ctx): and the code below it to under @commands.command()

@AwSkies
Copy link
Author

AwSkies commented Nov 28, 2019

No, you can change it cogs by after defining the command, use @example_command.after_invoke and then define the function to reset the cooldown. I'm going to add a cogs example.

@MrHitMan1
Copy link

does the whitelist here mean that whitlisted people can bypass the cooldown or that only whitelisted people can use the command?

@AwSkies
Copy link
Author

AwSkies commented Oct 31, 2021

@MrHitMan1 it means that only whitelisted people can bypass the cooldown.

@daffanovel12
Copy link

hi, @CapClumsy mine doesnt work (command not found).
i change the command_prefix to "."

@daffanovel12
Copy link

it should work if i type .pass right? because i set the prefix to "." and the #put your commands here to pass

@AwSkies
Copy link
Author

AwSkies commented Aug 13, 2022

@daffanovel12 Is the name of your command pass? The name of your command is specified by the name of the function or by the name parameter in the @bot.command decorator. The keyword pass is just something used as example code to put in a command. My guess would be try using .command or .example_command. I may just be confused by your question however.

@daffanovel12
Copy link

nvm, i missunderstood by the code. i thought its for bypass slowmode. sorry to bother

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment