Im creating a discord bot in python and i have few problems i would like to get a help with.
1. I would like to know how to add cooldown on a command. So for example someone will use a command and everyone will not be able to use the same command for some amount of time.
Here is one of my commands, i tried using time module at the end of the code, it works but you are not able to use any other commands. Any advice ?
@bot.event async def on_message(msg): chat = bot.get_channel(792030436523114496) if msg.content.startswith("Who"): if msg.channel.id != channel: return await chat.send("You are " + msg.author.name) time.sleep(60)
2. Second problem is similiar to the first one, but instead of adding cooldown on a command. I would like to add cooldown on a question.
Here is my code on a question and i want to add cooldown so people will try to answer the question but they will be able to do it only, lets say, for 30 seconds, then the question will just end if nobody had answered it.
if msg.content.startswith("Question"): if msg.channel.id != channel: return await chat.send("1+1 ?") await bot.wait_for("message", check=lambda msg: msg.content == "2") await chat.send("Good work " + msg.author.mention)
3. The third one is kinda tricky, its about that bot would be able to add reactions on its own message.
Here you can see a picture how it should looks like. I want the bot to add reactions on its own message and then if there is any method when someone click on A, something happens.
4. And the last problem is quite simple.
Here we can take the same code from the first problem. To execute this command, people must write Who but i want to make that people can also write wHo, whO, WHo and so on regardless on uppercases or lowercases.
@bot.event async def on_message(msg): chat = bot.get_channel(792030436523114496) if msg.content.startswith("Who"):
Advertisement
Answer
discord.py
has a built-in support for cooldowns, here an example
@bot.command() @commands.cooldown(1, 6.0, commands.BucketType.guild) # rate, per, BucketType async def foo(ctx, arg): await ctx.send(arg)
- This question is kinda tricky, you’d need to use a cog and
commands.CooldownMapping
(There’s no docs about it so I can’t give you the link)
class SomeCog(commands.Cog): def __init__(self, bot): self.bot = bot self._cd = commands.CooldownMapping.from_cooldown(1, 6.0, commands.BucketType.guild) # Change accordingly def get_ratelimit(self, message): """Returns the ratelimit left""" bucket = self._cd.get_bucket(message) return bucket.update_rate_limit() @commands.Cog.listener() async def on_message(self, message): if message.content.lower() == "question": # Getting the ratelimit retry_after = self.get_ratelimit(message) if retry_after is None: # You're not ratelimited, send the question await message.channel.send("Question") else: # You're ratelimited, don't send the question await message.channel.send(f"You need to wait {retry_after} seconds to get another question") bot.add_cog(SomeCog(bot))
(This was heavily inspired by one of my previous answers)
- This question is a really easy one, simply use
Message.add_reaction
method
@bot.command() async def foo(ctx): message = await ctx.send("This is the message I want to add the reaction to") await message.add_reaction("đŠ")
Note: For default reactions the reaction must be a unicode, to get it :emoji:
- You can use the
str.lower
method
>>> "HeLlO".lower() "hello"
if message.content.lower().starswith("who"): # ...
If you’re using discord.Client
none of this is gonna work, you need to use commands.Bot
Reference:
I answered your questions cause I know the answer to all of them, next time try to separate each question and focus more on each of them, what have you tried, what is the result, what is the expected result…