Reputation: 11
I have this code:
import asyncio
import random
from discord.ext import commands
bot = commands.Bot(command_prefix='.')
a=['Hello','Hi','Hello World']
async def background_task():
time = 5 # 86400
await asyncio.sleep(time)
w = random.choice(a)
q = "".join(w)
message = q
await bot.get_channel(id_channel).send(message)
async def on_ready():
print('Bot logged as {}'.format(bot.user))
async def on_message(ctx):
pass
bot.loop.create_task(background_task())
token = 'token'
bot.run(token)
I need to loop background_task(). But bot.loop.create_task(background_task()) don't working. What should I do? I will be grateful fo your help.
Upvotes: 0
Views: 676
Reputation: 11
while True solve my problem
import asyncio
import random
from discord.ext import commands
bot = commands.Bot(command_prefix='.')
a=['Hello','Hi','Hello World']
async def background_task():
w = random.choice(a)
q = "".join(w)
message = q
while True:
time = 5 # 86400
await asyncio.sleep(time)
await bot.get_channel(771365589355855883).send(message)
async def on_ready():
print('Bot logged as {}'.format(bot.user))
async def on_message(ctx):
pass
bot.loop.create_task(background_task())
token = 'NzcxMzYwMzk4NzAzOTg0NjYx.X5q_Wg.w00I702euybW-Z2AYSQpHIsfifQ'
bot.run(token)
Upvotes: 1