3
votes

I have been trying to create a bot for Discord using the discord.py library however when I am running the program it is not sending the message as expected. It is a simple bot that is suppose to send a message every 10 minutes to a channel. I am not getting any error messages in the command line and can't seem to see any obvious errors? Any help would be greatly appreciated.

import asyncio

client = discord.Client()

async def my_background_task():
    await client.wait_until_ready()
    counter = 0
    channel = discord.Object(id='my channel ID goes here')
    while not client.is_closed:
        counter += 1
        await message.channel.send("TEST")
        await asyncio.sleep(5) # 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('My token goes here')
3
Does it give any errors or does it just run. Also the message.channel.send should probably be channel.send since you're setting the channel manually and not from a message.duckboycool
@duckboycool No errors, it just runs in the command line with the output below. Logged in as TestBot Bot ID ------LockTheTaskBar
And what does it do if you replace await message.channel.send("TEST")?duckboycool
Just the same, just sits running in a command prompt.LockTheTaskBar
You need to fix a few typos. Use client.is_closed() instead of client.is_closed, channel = client.get_channel(12345) instead of channel = discord.Object(id='12345') and await channel.send("TEST") instead of await message.channel.send("TEST").Benjin

3 Answers

0
votes

If the channel ID is a string, It should be an integer.

Turn this:

channel = discord.Object(id='my channel ID goes here')

into:

channel = discord.Object(id=101029321892839) # an example ID

If that doesn't solve your problem, try setting 'message.channel.send' to 'channel.send' and client.is_closed should be

client.is_closed() # put brackets
0
votes

Instead of client.loop.create_task(my_background_task()), just put the background function in the on_ready event. And take out await client.wait_until_ready() and just put the function in the on_ready event as well. I have also changed the client variable, taken out unnecessary code, and added some modules.

import asyncio, discord
from discord.ext import commands

client = commands.Bot(command_prefix = ".") # If you don't want a prefix just take out the parameter.

async def my_background_task():
    channel = discord.Object(id='my channel ID goes here')
    while True:
        await channel.send("TEST")
        await asyncio.sleep(5) # task runs every 60 seconds

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

client.run('My token goes here')

Also in future when posting a question, please tag the python version as I don't know which version it is and then this and other answers could be wrong.

0
votes

Although creating a task could work, I wouldn't recommend it when there are better options. Dpy's command.ext addon has a task based system built in directly, so let's look into using that instead.

import discord
from discord.ext import tasks

client = discord.Client()

@tasks.loop(minutes=10)
async def my_background_task():
    """A background task that gets invoked every 10 minutes."""
    channel = client.get_channel(754904403710050375) # Get the channel, the id has to be an int
    await channel.send('TEST!')
    
@my_background_task.before_loop
async def my_background_task_before_loop():
    await client.wait_until_ready()

my_background_task.start()
client.run('Your token goes here')

The time until which the above loop will run is dependent upon human psychology, laws of energy and cosmos. That is:

• You get bored of it

• The power goes down and your script stops working

• The universe explodes

Read more about it here: