Hey everyone! I’m trying to create a fun feature for my Discord bot using Python. I want it to mention a user multiple times in separate messages. The number of mentions should be random, anywhere from 0 to 1000.
I’ve tried something like this:
if msg.content.startswith('!mention'):
await msg.channel.send('Starting mention spree!')
for _ in range(100):
await msg.channel.send(msg.author.mention)
But it’s not quite what I’m looking for. How can I make the number of mentions random? And is there a better way to structure this code? Any help would be awesome! Thanks in advance!
I’ve implemented something similar in one of my Discord bots. Here’s a more efficient approach using Python’s random module:
import random
if msg.content.startswith('!mention'):
mention_count = random.randint(0, 1000)
await msg.channel.send(f'Starting mention spree! You'll be mentioned {mention_count} times.')
mentions = [msg.author.mention] * mention_count
await msg.channel.send('\n'.join(mentions))
This generates a random number of mentions, informs the user, and sends all mentions in a single message. It’s more efficient and less likely to hit rate limits. Remember to add appropriate cooldowns or user restrictions to prevent spam.
I’ve had some experience with this kind of feature, and there are a few things to consider. While random mentions can be fun, sending too many messages too quickly can lead to rate limiting issues with Discord’s API. Here’s an approach I’ve used that works well:
for i in range(0, count, 10):
mentions = ' '.join([msg.author.mention] * min(10, count - i))
await msg.channel.send(mentions)
await asyncio.sleep(1) # Add a small delay between batches
This groups mentions into batches of 10 and adds a small delay between each batch. It’s more efficient and less likely to hit rate limits. You might want to add some permissions checks too, so not just anyone can trigger it.