Forums

discord.py Bot Long Running Tasks

Hello, So I'm trying to get a bot online 24/7 using the scheduled task trick. I copied the example and set the task to run every hour in "Schedule" but it seems to be failing.

Traceback

    Traceback (most recent call last):
  File "/home/Necro/necrobot/starter.py", line 25, in <module>
    from bot import *
  File "/home/Necro/necrobot/bot.py", line 28
        async def get_pre(bot, message):
                ^
    SyntaxError: invalid syntax

2017-08-03 20:41:06 -- Completed task, took 2.00 seconds, return code was 1.

So the error is coming from code in the bot but only when I import it to the file used to check whether it's running. Here's the code I use to check if it's running, a slightly modified version of the example:

import logging
import socket
import sys
from bot import *

lock_socket = None  # we want to keep the socket open until the very end of
                    # our script so we use a global variable to avoid going
                    # out of scope and being garbage-collected

def is_lock_free():
    global lock_socket
    lock_socket = socket.socket(socket.AF_UNIX, socket.SOCK_DGRAM)
    try:
        lock_id = "necro.necrobot"   # this should be unique. using your username as a prefix is a convention
        lock_socket.bind('\0' + lock_id)
        logging.debug("Acquired lock %r" % (lock_id,))
        return True
    except socket.error:
        # socket already locked, task must already be running
        logging.info("Failed to acquire lock %r" % (lock_id,))
        return False

if not is_lock_free():
    sys.exit()

And if people think the bot code is really the trouble they can check it out there, but it works fine from my computer and when starting it manually. https://github.com/ClementJ18/necrobot

Thanks for any help

Are you not running the code under python 3.5? I see that the invalid syntax is pointed towards the async key (which is exclusively implemented for python 3.5+). Try seeing if you can add a shebang to the top of your file. Something like this would do: #!/usr/bin/python3.5

see the 'specifying the python version' section here: http://help.pythonanywhere.com/pages/ScheduledTasks

Thanks for the help, adding the shebang definitely did help and actually solved my problem. However, the task went on to the next line (in which I open a file) and decided that the file didn't actually exist when it actually does.

WARNING:discord.client:PyNaCl is not installed, voice will NOT be supported
Traceback (most recent call last):
  File "/home/Necro/necrobot/starter.py", line 27, in <module>
    from bot import *
  File "/home/Necro/necrobot/bot.py", line 51, in <module>
    with open("data/userdata.csv","r") as f:
FileNotFoundError: [Errno 2] No such file or directory: 'data/userdata.csv'
ERROR:asyncio:Unclosed client session
client_session: <aiohttp.client.ClientSession object at 0x7fee3ebe86a0>

2017-08-04 06:50:29 -- Completed task, took 12.00 seconds, return code was 1.

It's becoming clear to me that I'm clearly going along this wrong, it seems importing the entire bot file doesn't work. I would need an actual function to run the bot.

Try using a full path instead of a relative path.

ie. something like /home/Necro/necrobot/data/userdata.csv

Full path worked like a charm, thanks a lot for the help. Also overhauled the bot slightly too, that probably helped.

Bot is now running fine and the scheduled task too

That's great!

             / ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄\
<<<<<<:>~  <   Yay!           |
              \_________/