You're getting that error because the channel variable is None
(a NoneType
doesn't have any attributes/methods) because the channel it's not in the internal cache, you're blocking the whole thread so it never loads. I'm guessing I could fix your code but is a really bad solution to background tasks. Fortunately discord.py
comes with a built-in extension for doing such things, here an example:
from discord.ext import tasks
@tasks.loop(minutes=10) # You can either pass seconds, minutes or hours
async def send_image(channel: discord.TextChannel):
image = discord.File("path here")
await channel.send(file=image)
# Starting the task (you can also do it on the `on_ready` event so it starts when the bot is running)
@client.command()
async def start(ctx):
channel = client.get_channel(ID_HERE)
send_image.start(channel)
# Using the `on_ready` event
@client.event
async def on_ready():
await client.wait_until_ready() # Waiting till the internal cache is done loading
channel = client.get_channel(ID_HERE)
send_image.start(channel)
@client.command()
async def stop(ctx):
send_image.stop()
Reference:
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…