2
votes

I'm having fun programming a discord bot for my friend's channel but i need some help: I'm tracking messages sent by a particular friend and trolling him back, but i dont want to troll him on every message he sent. So i thought about putting the bot to sleep for some time after the bot sent a troll message, lets say 20seconds, and then track a future message to troll him again.

I tried using time.sleep() but that just delays the response time of the bot, causing it to sent a lot of messages in a row. I just want do deactivate it for some time after a troll message.

I hid some parts of the accounts info but the base code is like this:

@client.event
async def on_message(message):
        await message.channel.send('insert troll massage here')

Thanks in advance!

1

1 Answers

0
votes

You don't want to stop execution of the bot. You simply want the bot to ignore messages given within a certain timestamp.

As a rough draft, we can use a global variable to do this. To delay for one minute, consider

from datetime import datetime, timedelta

LAST_TROLL = datetime.fromtimestamp(0) # A loooooong time ago
TIME_TO_DELAY = timedelta(minutes=1)

@client.event
async def on_message(message):
    global LAST_TROLL
    now = datetime.now()
    if now - LAST_TROLL > TIME_TO_DELAY:
        await message.channel.send('insert troll massage here')
        LAST_TROLL = now

If you're writing a bot at scale, you'll want the bot to be nicely encapsulated in a class, and then this variable would be an instance variable on that class.