File botly/bot.py changed (mode: 100644) (index ac1883e..dd79f54) |
... |
... |
import discord |
9 |
9 |
from botly.db import Db |
from botly.db import Db |
10 |
10 |
from botly.settings import Settings |
from botly.settings import Settings |
11 |
11 |
from botly.knowledge import Knowledge |
from botly.knowledge import Knowledge |
12 |
|
from botly.comm import Comm |
|
|
12 |
|
from botly.client import Client |
13 |
13 |
from botly.task import load_tasks |
from botly.task import load_tasks |
14 |
14 |
from botly.reaction import load_reactions |
from botly.reaction import load_reactions |
15 |
15 |
from botly.behaviour import Behaviour |
from botly.behaviour import Behaviour |
|
... |
... |
class Bot: |
22 |
22 |
"""Initialization loads database, reactions and tasks from drive.""" |
"""Initialization loads database, reactions and tasks from drive.""" |
23 |
23 |
self.me = None |
self.me = None |
24 |
24 |
self.server = None |
self.server = None |
|
25 |
|
self.askedToLeave = False |
25 |
26 |
self.logPath = logFile |
self.logPath = logFile |
26 |
27 |
|
|
27 |
28 |
# Load database and models |
# Load database and models |
|
... |
... |
class Bot: |
30 |
31 |
self.knowledge = Knowledge() |
self.knowledge = Knowledge() |
31 |
32 |
|
|
32 |
33 |
# Create discord client object for communication and event reception |
# Create discord client object for communication and event reception |
33 |
|
self.comm = Comm(self) |
|
|
34 |
|
self.client = Client(self) |
|
35 |
|
# alias, as old name may be still in use somwhere |
|
36 |
|
self.comm = self.client |
34 |
37 |
|
|
35 |
38 |
# Load the bot background tasks |
# Load the bot background tasks |
36 |
39 |
for task in load_tasks(self, rootModule + '.tasks'): |
for task in load_tasks(self, rootModule + '.tasks'): |
37 |
|
self.comm.loop.create_task(task._run()) |
|
|
40 |
|
self.client.loop.create_task(task._run()) |
38 |
41 |
|
|
39 |
42 |
# Load the bot behaviour (reactions to events) |
# Load the bot behaviour (reactions to events) |
40 |
43 |
self.behaviour = Behaviour(load_reactions(self, |
self.behaviour = Behaviour(load_reactions(self, |
|
... |
... |
class Bot: |
48 |
51 |
"""Runs the bot until it is exited. """ |
"""Runs the bot until it is exited. """ |
49 |
52 |
# Listen to Discord's events (blocking call) |
# Listen to Discord's events (blocking call) |
50 |
53 |
self.print("Connecting to Discord...") |
self.print("Connecting to Discord...") |
51 |
|
self.comm.run() |
|
52 |
|
self.print("Botly exited.") |
|
|
54 |
|
self.client.run() |
|
55 |
|
if not self.askedToLeave: |
|
56 |
|
self.print('Botly exited without being asked to. Will reconnect.') |
|
57 |
|
# TODO: This might be incorrect, as perhaps we'd require reloading |
|
58 |
|
# the whole robot, as the event loop will be ended and tasks |
|
59 |
|
# wouldn't be there anymore. Maybe we should check for |
|
60 |
|
# disconnectiong somewhere else. |
|
61 |
|
self.live() |
|
62 |
|
else: |
|
63 |
|
self.print("Botly exited.") |
53 |
64 |
|
|
54 |
65 |
def print(self, message, msgtype='INFO'): |
def print(self, message, msgtype='INFO'): |
55 |
66 |
dt = datetime.datetime.now() |
dt = datetime.datetime.now() |
|
... |
... |
class Bot: |
68 |
79 |
@asyncio.coroutine |
@asyncio.coroutine |
69 |
80 |
def leave(self): |
def leave(self): |
70 |
81 |
"""Coroutine that disconnects the robot from Discord.""" |
"""Coroutine that disconnects the robot from Discord.""" |
71 |
|
yield from self.comm.logout() |
|
|
82 |
|
self.askedToLeave = True |
|
83 |
|
yield from self.client.logout() |
72 |
84 |
|
|
73 |
85 |
@asyncio.coroutine |
@asyncio.coroutine |
74 |
86 |
def say(self, channel, message): |
def say(self, channel, message): |
75 |
87 |
"""Coroutine that send a message to discord.""" |
"""Coroutine that send a message to discord.""" |
76 |
|
yield from self.comm.send_message(channel, message) |
|
|
88 |
|
yield from self.client.send_message(channel, message) |
77 |
89 |
|
|
78 |
90 |
def set_bot_info(self, user, server): |
def set_bot_info(self, user, server): |
79 |
91 |
"""This will be called once the bot is connected to Discord.""" |
"""This will be called once the bot is connected to Discord.""" |
File botly/task.py changed (mode: 100644) (index ca4f4d1..9acf28c) |
... |
... |
DAY_FRIDAY = 16 |
30 |
30 |
DAY_SATURDAY = 32 |
DAY_SATURDAY = 32 |
31 |
31 |
DAY_SUNDAY = 64 |
DAY_SUNDAY = 64 |
32 |
32 |
|
|
|
33 |
|
|
33 |
34 |
class TaskBase: |
class TaskBase: |
34 |
35 |
"""Base class for bot's background tasks""" |
"""Base class for bot's background tasks""" |
35 |
36 |
|
|
|
... |
... |
class TaskBase: |
82 |
83 |
self.bot = bot |
self.bot = bot |
83 |
84 |
self.knowledge = bot.knowledge |
self.knowledge = bot.knowledge |
84 |
85 |
self.settings = bot.settings |
self.settings = bot.settings |
85 |
|
self.comm = bot.comm |
|
|
86 |
|
self.client = bot.client |
|
87 |
|
self.comm = bot.client # alias because still in use in tasks |
86 |
88 |
|
|
87 |
89 |
self.maxDaysLookup=self.settings.get_int('MaximumTaskDaysLookup', '15') |
self.maxDaysLookup=self.settings.get_int('MaximumTaskDaysLookup', '15') |
88 |
90 |
self.minimumTimeout = bot.settings.get_int('MinimumTaskTimeout', '10') |
self.minimumTimeout = bot.settings.get_int('MinimumTaskTimeout', '10') |
|
... |
... |
class TaskBase: |
99 |
101 |
"""Will start the task scheduler. Do not overwrite.""" |
"""Will start the task scheduler. Do not overwrite.""" |
100 |
102 |
|
|
101 |
103 |
# Wait until the bot is ready before running the task |
# Wait until the bot is ready before running the task |
102 |
|
yield from self.comm.wait_until_ready() |
|
|
104 |
|
yield from self.client.wait_until_ready() |
103 |
105 |
|
|
104 |
|
while not self.comm.is_closed or not self.endTask: |
|
|
106 |
|
while not self.client.is_closed or not self.endTask: |
105 |
107 |
timeout = self._get_next_timeout() |
timeout = self._get_next_timeout() |
106 |
108 |
if timeout is None or timeout == 0: |
if timeout is None or timeout == 0: |
107 |
109 |
break |
break |
|
... |
... |
class TaskBase: |
111 |
113 |
# We'll wait till the task is allowed to run |
# We'll wait till the task is allowed to run |
112 |
114 |
yield from asyncio.sleep(timeout) |
yield from asyncio.sleep(timeout) |
113 |
115 |
# Was the communication after waiting for the trigger? |
# Was the communication after waiting for the trigger? |
114 |
|
if self.comm is None or self.comm.is_closed or self.endTask: |
|
|
116 |
|
if self.client is None or self.client.is_closed or self.endTask: |
115 |
117 |
break |
break |
116 |
118 |
# Execute the task |
# Execute the task |
117 |
119 |
yield from self.do() |
yield from self.do() |