Tollpatsch
Tollpatsch

Reputation: 336

How to let a bot post/send a message every 5 minutes or because of another event?

I thought that would be a simple task but somehow i can even mess this up. So i just want to have my bot send a message (to troll my friends) a specific message every 5 minutes. So i found this which doesnt work or any other code. I dont even get an error message. So im pretty much clueless, which sucks.

import discord
import asyncio

client = discord.Client()

async def my_background_task():
    await client.wait_until_ready()
    counter = 0
    channel = discord.Object(id='channel_id_here')
    while not client.is_closed:
        counter += 1
        await client.send_message(channel, counter)
        await asyncio.sleep(60) # task runs every 60 seconds

@client.event
async def on_ready():
    print('Logged in as')
    print(client.user.name)
    print(client.user.id)
    print('------')

client.loop.create_task(my_background_task())
client.run('token')

Anyway here is my boiled down code, without all of the Bot commands.

import discord
from discord.ext.commands import Bot, has_permissions

import secrets
import numpy as np
import re

TOKEN = 'mytokenasastring'
BOT = Bot(command_prefix='!')
#... some commands for my bot for my friends and myself
BOT.loop.create_task(my_background_task())
BOT.run(TOKEN)

So i just added the above code like this

async def my_background_task():
await BOT.wait_until_ready()
counter = 0
channel = discord.Object(id='mytestchannelidasastring') #i also tried as int but also doenst work
while not BOT.is_closed:
    counter += 1
    await BOT.send_message(channel, counter)
    await asyncio.sleep(60) # task runs every 60 seconds

then vs code tells my Bot doenst have a send_message method. so i change the code of the send_message to this

await channel.send(counter)

But now i get the warning/error form VS code that channel doenst have a send method so i get the (real?!) channel like this

BOT.get_channel(id='mychannelidasstring')

and it still doesn't work or i dont get any kind of error message of any kind... pls help or i will go mad....

Upvotes: 2

Views: 5239

Answers (2)

Eric Jin
Eric Jin

Reputation: 3924

Try using discord.tasks.loop.

from discord.ext import tasks

counter = 0

@tasks.loop(minutes=1.0, count=None)
async def my_background_task():
    global counter
    channel = BOT.get_channel(123456789) # channel id as an int
    counter += 1
    await channel.send(f'{counter}')
my_background_task.start()

to loop your troll function every minute.

Upvotes: 2

Diggy.
Diggy.

Reputation: 6944

It looks as though you're using some of the old async's (v0.16.x) docs/tutorials, which can be seen from send_message() and writing IDs as strings. I'd recommend you try to find more recent tutorials and read the recent docs instead.

See here for all the major changes from then into the most recent version, rewrite (v1.x)

As for your code, here is what it should look like:

async def my_background_task():
    await client.wait_until_ready() # ensures cache is loaded
    counter = 0
    channel = client.get_channel(id=112233445566778899) # replace with target channel id
    while not client.is_closed():
        counter += 1
        await channel.send(counter)
        await asyncio.sleep(60) # or 300 if you wish for it to be 5 minutes

@client.event
async def on_ready():
    print('Logged in as')
    print(client.user.name)
    print(client.user.id)
    print('------')
    client.loop.create_task(my_background_task()) # best to put it in here

client.run("token")

References:

Upvotes: 1

Related Questions