Goal:
I am simply trying to send a message to a discord channel from a @tasks.loop()
without the need for a discord message variable from @client.event async def on_message
. The discord bot is kept running in repl.it using uptime robot.
Method / Background:
A simple while True
loop in will not work for the larger project I will be applying this principle to, as detailed by Karen's answer here. I am now using @tasks.loop()
which Lovesh has quickly detailed here: (see Lovesh's work).
Problem:
I still get an error for using the most common method to send a message in discord using discord.py. The error has to have something to do with the await channel.send( )
method. Neither of the messages get sent in discord. Here is the error message.
Code:
from discord.ext import tasks, commands
import os
from keep_alive import keep_alive
import time
token = os.environ['goofyToken']
# Set Up Discord Client & Ready Function
client = discord.Client()
channel = client.get_channel(CHANNEL-ID)
@client.event
async def on_ready():
print('We have logged in as {0.user}'.format(client))
@tasks.loop(count=1)
async def myloop(word):
await channel.send(word)
@client.event
async def on_message(message):
msg = message.content
if msg.startswith('!'):
message_to_send = 'Hello World!'
await channel.send(message_to_send)
myloop.start(message_to_send)
keep_alive()
client.run(token)
Attempted Solutions:
A message can be sent from the on_message
event using the syntax await message.channel.send('Hello World!)
. However, I just can't use this. The code is kept running online by uptimerobot, a free website which pings the repository on repl.it. When the robot pings the repository, the message variable is lost, so the loop would stop scanning my data in the larger project I am working on which is giving me this issue.
When using any client.get_*
method the bot will try to grab the object from the cache, the channel
global variable is defined before the bot actually runs (so the cache is empty). You should get the channel inside the loop function:
@tasks.loop(count=1)
async def myloop(word):
channel = client.get_channel(CHANNEL_ID)
await channel.send(word)