/botly/task.py (cbc32e0ea4a895323185a0bb17e519663858e1fc) (1995 bytes) (mode 100644) (type blob)

#!/usr/bin/env python
"""Contains the TaskBase to be inherited for Tasks creation"""

import asyncio
from importlib import import_module
from os import listdir
from os.path import isfile, join

import discord


class TaskBase:
    """Base class for bot's background tasks"""

    def __init__(self):
        self.endTask = False

    def set_instance_info(self, bot):
        self.bot = bot
        self.knowledge = bot.knowledge
        self.settings = bot.settings
        self.comm = bot.comm

        self.mainChannel = discord.Object(
            id=bot.settings.get_string('DefaultChannelId'))

    async def run(self):
        """Entry point of task. Do not overwrite."""

        # Wait until the bot is ready before running
        await self.comm.wait_until_ready()

        while not self.comm.is_closed or not self.endTask: 
            await asyncio.sleep(self.get_next_timeout())
            await self.do()

    def get_next_timeout(self):
        """Should return the time, in sec, to wait before each do() call."""
        return 10

    def do(self):
        """Proceed with the task's action. To be overwritten when inherited"""
        pass


def load_tasks(bot, tasksParent):
    """Function that loads the tasks from given paren module directory."""
    # Get fs path to module directory
    dpath = './' + tasksParent.replace('.', '/')

    # Retreive every python script in the tasks module directory
    files = [f for f in listdir(dpath)
             if isfile(join(dpath, f))
                and f.endswith('.py')]

    # Build the list of tasks
    tasks = []
    for file in files:
        # Module name should not end with '.py', so remove that part
        module = file[:-3]
        _taskModule = import_module(tasksParent + '.' + module)
        task = _taskModule.Task()
        tasks.append(task)
        # Inject instance info in task object
        task.set_instance_info(bot)
    print('Loaded ' + str(len(tasks)) + ' tasks from drive.')
    return tasks




Mode Type Size Ref File
100644 blob 896 ac3944627b46e5914bc225d541cab61e13102ad4 README.md
040000 tree - c3633971c8e36cc59dd5c41fd76fa9255765416e botly
100644 blob 172 3f148ab99d152565c46e51fd45e51030417f7faa example.py
040000 tree - 902cb30cdf6276223ffa4043fbdaee680d0a984c examplebot
Hints:
Before first commit, do not forget to setup your git environment:
git config --global user.name "your_name_here"
git config --global user.email "your@email_here"

Clone this repository using HTTP(S):
git clone https://rocketgit.com/user/detche/Botly

Clone this repository using ssh (do not forget to upload a key first):
git clone ssh://rocketgit@ssh.rocketgit.com/user/detche/Botly

Clone this repository using git:
git clone git://git.rocketgit.com/user/detche/Botly

You are allowed to anonymously push to this repository.
This means that your pushed commits will automatically be transformed into a merge request:
... clone the repository ...
... make some changes and some commits ...
git push origin main