Discord Bot Asynchronous Loop Failure

Please be patient with me. I'm actually not a coder and working with code that was left to us by someone who we can no longer contact and get help with. I've tried to do some research on my own, but I unfortunately can't extrapolate from other solutions to ours due to my insufficient skills.

Part of the issue is also that our code is probably less than optimal and could be looped better by assigning i-values to each task, but I unfortunately don't know how to change it, so I'm just trying to find a solution with what we have. It was working on Heroku, but with their upcoming removal of their free services, we're looking to import it elsewhere and running into errors.

Briefly, we have a very simple Discord bot whose purpose is to check certain channels and send a message when those channels have not had activity within a certain time period. In more detail, here is the general code for checking one of the channels:

import discord
import datetime
import asyncio
import math
from discord.ext import tasks

client = discord.Client(intents=discord.Intents.default())

@client.event
async def on_ready():
    print('Ready'.format(client))

@tasks.loop(seconds = 30)
async def channelname():
    await client.wait_until_ready()
    channel_id = ################
    
    channel = client.get_channel(channel_id)
    message = await channel.fetch_message(channel.last_message_id)
    printchannel = client.get_channel(################)
    
    if "message to turn off bot" in message.content.lower():
        return
    else:
        msg_secs = (datetime.datetime.utcnow() - message.created_at).total_seconds()
        if msg_secs >=300  and msg_secs <= 360:
            await printchannel.send('reminder message')
        else:
            return

channelname.start()

client.run('bot token')

When we try to run this code, we are currently running into this error (on Railway):

Traceback (most recent call last):
File "botname.py", line 5610, in <module>
test.start()
File "/opt/venv/lib/python3.8/site-packages/discord/ext/tasks/__init__.py", line 398, in start
self._task = asyncio.create_task(self._loop(*args, **kwargs))
File "/nix/store/bhny2arkxrifw0afjbnqqi0ilqnwndqr-setup-env/lib/python3.8/asyncio/tasks.py", line 381, in create_task
loop = events.get_running_loop()
RuntimeError: no running event loop
sys:1: RuntimeWarning: coroutine 'Loop._loop' was never awaited

From what I can tell from looking up this error, the problem is that the loop was just created and cannot have tasks attached to it, so I need to have the code add the tasks in by itself. Unfortunately, all of the solutions I'm finding do use i-values, which we don't use, so I can't figure out how to make it work for us.

Thank you very much in advance for any assistance!



Comments

Popular posts from this blog

Today Walkin 14th-Sept

Spring Elasticsearch Operations

Hibernate Search - Elasticsearch with JSON manipulation