/botly/bot.py (8e6311b6edcbaadc7aa9178098977cbaf7123493) (2306 bytes) (mode 100644) (type blob)

#!/usr/bin/env python
"""Main class that will load the various parts of the bot."""

import asyncio

import discord

from botly.settings import Settings
from botly.knowledge import Knowledge
from botly.comm import Comm
from botly.task import load_tasks
from botly.reaction import load_reactions
from botly.behaviour import Behaviour


class Bot:
    """Main class that represents the bot. Will load various dependencies."""

    def __init__(self, rootModule, dbPath):
        """Initialization loads database, reactions and tasks from drive."""
        self.me = False
  
        self.settings = Settings()
        self.settings.load(dbPath)
        self.knowledge = Knowledge()

        # Create discord client object for communication and event reception       
        self.comm = Comm(self)

        # Load the bot background tasks
        for task in load_tasks(self, rootModule + '.tasks'):
            self.comm.loop.create_task(task.run())

        # Load the bot behaviour (reactions to events)
        self.behaviour = Behaviour(load_reactions(self,
            rootModule + '.reactions'))

    def live(self):
        """Runs the bot until it is exited. """
        # Listen to Discord's events (blocking call)
        print("Connecting to Discord...")
        self.comm.run()
        print("\nBotly exited.")
    
    async def leave(self):
        """Coroutine that disconnects the robot from Discord."""
        await self.comm.logout()

    async def say(self, channel, message):
        """Coroutine that send a message to discord."""
        await self.comm.send_message(channel, message)

    def set_whoami(self, user):
        """This will be called once the bot is connected to Discord."""
        self.me = user
    
    async def react_to(self, eventName, **eventInfo):
        """Coroutine that will be called upon events. Checks for triggers."""
        reactions = self.behaviour.get_reactions(eventName)
        if reactions:
            for reaction in reactions:
                if reaction.trigger.is_triggered(self, **eventInfo):
                    print("Reaction '" + reaction.moduleName
                          + "' triggered by event '" + eventName + "'.")
                    reaction.prepare_react(**eventInfo)
                    await reaction.react()
        




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