File botly/behaviour.py changed (mode: 100644) (index 5221f53..25f3a07) |
5 |
5 |
class Behaviour: |
class Behaviour: |
6 |
6 |
"""Behaviour class holds the reaction sorted by event name.""" |
"""Behaviour class holds the reaction sorted by event name.""" |
7 |
7 |
|
|
8 |
|
def __init__(self, reactions): |
|
|
8 |
|
def __init__(self, reactions, bot): |
9 |
9 |
self.reactions = {} |
self.reactions = {} |
10 |
10 |
# We will group reactions based on the event |
# We will group reactions based on the event |
11 |
11 |
# they react on. |
# they react on. |
12 |
|
print('Loading reactions into behaviour...') |
|
13 |
12 |
for reaction in reactions: |
for reaction in reactions: |
14 |
13 |
ename = reaction.eventName |
ename = reaction.eventName |
15 |
|
print("Loading reaction '" |
|
|
14 |
|
bot.print("Loading reaction '" |
16 |
15 |
+ reaction.moduleName |
+ reaction.moduleName |
17 |
16 |
+ "' into event table '" |
+ "' into event table '" |
18 |
17 |
+ ename + "'.") |
+ ename + "'.") |
File botly/bot.py changed (mode: 100644) (index 32af3d8..4b444c9) |
... |
... |
from botly.behaviour import Behaviour |
17 |
17 |
class Bot: |
class Bot: |
18 |
18 |
"""Main class that represents the bot. Will load various dependencies.""" |
"""Main class that represents the bot. Will load various dependencies.""" |
19 |
19 |
|
|
20 |
|
def __init__(self, rootModule, dbPath): |
|
|
20 |
|
def __init__(self, rootModule, dbPath, logFile=''): |
21 |
21 |
"""Initialization loads database, reactions and tasks from drive.""" |
"""Initialization loads database, reactions and tasks from drive.""" |
22 |
22 |
self.me = None |
self.me = None |
23 |
23 |
self.server = None |
self.server = None |
|
24 |
|
self.logPath = logFile |
24 |
25 |
|
|
25 |
26 |
# Load database and models |
# Load database and models |
26 |
27 |
Db.load(dbPath) |
Db.load(dbPath) |
|
... |
... |
class Bot: |
36 |
37 |
|
|
37 |
38 |
# Load the bot behaviour (reactions to events) |
# Load the bot behaviour (reactions to events) |
38 |
39 |
self.behaviour = Behaviour(load_reactions(self, |
self.behaviour = Behaviour(load_reactions(self, |
39 |
|
rootModule + '.reactions')) |
|
|
40 |
|
rootModule + '.reactions'), self) |
40 |
41 |
|
|
41 |
42 |
# Set the default channel that was saved in bot settings |
# Set the default channel that was saved in bot settings |
42 |
43 |
self.mainChannel = discord.Object( |
self.mainChannel = discord.Object( |
|
... |
... |
class Bot: |
45 |
46 |
def live(self): |
def live(self): |
46 |
47 |
"""Runs the bot until it is exited. """ |
"""Runs the bot until it is exited. """ |
47 |
48 |
# Listen to Discord's events (blocking call) |
# Listen to Discord's events (blocking call) |
48 |
|
print("Connecting to Discord...") |
|
|
49 |
|
self.print("Connecting to Discord...") |
49 |
50 |
self.comm.run() |
self.comm.run() |
50 |
|
print("\nBotly exited.") |
|
|
51 |
|
self.print("\nBotly exited.") |
51 |
52 |
|
|
52 |
53 |
def print(self, message, msgtype='INFO'): |
def print(self, message, msgtype='INFO'): |
53 |
|
print('[' + msgtype + ']' + message) |
|
|
54 |
|
m = '[' + msgtype + ']' |
|
55 |
|
if not message.startswith('['): |
|
56 |
|
m += ' ' |
|
57 |
|
m += message |
|
58 |
|
if self.logPath is not None and len(self.logPath): |
|
59 |
|
f = open(self.logPath, 'a') |
|
60 |
|
f.write(m + '\n') |
|
61 |
|
f.close() |
|
62 |
|
else: |
|
63 |
|
print(m) |
54 |
64 |
|
|
55 |
65 |
async def leave(self): |
async def leave(self): |
56 |
66 |
"""Coroutine that disconnects the robot from Discord.""" |
"""Coroutine that disconnects the robot from Discord.""" |
|
67 |
|
self.logFile.close() |
|
68 |
|
self.logFile = None |
57 |
69 |
await self.comm.logout() |
await self.comm.logout() |
58 |
70 |
|
|
59 |
71 |
async def say(self, channel, message): |
async def say(self, channel, message): |
File botly/reaction.py changed (mode: 100644) (index e27b1bf..4c0e2c3) |
... |
... |
def load_reactions(bot, reactionsParent): |
127 |
127 |
# Inject instance info in reaction object |
# Inject instance info in reaction object |
128 |
128 |
reaction.set_instance_info(bot) |
reaction.set_instance_info(bot) |
129 |
129 |
reaction.set_module_name(module) |
reaction.set_module_name(module) |
130 |
|
print('Loaded ' + str(len(reactions)) + ' reactions from drive.') |
|
|
130 |
|
bot.print('Loaded ' + str(len(reactions)) + ' reactions from drive.') |
131 |
131 |
return reactions |
return reactions |
132 |
132 |
|
|
133 |
133 |
|
|
File botly/task.py changed (mode: 100644) (index 0e0cfa8..b3fc3c8) |
... |
... |
def load_tasks(bot, tasksParent): |
206 |
206 |
# Inject instance info in task object |
# Inject instance info in task object |
207 |
207 |
task._set_instance_info(bot) |
task._set_instance_info(bot) |
208 |
208 |
task._set_module_name(module) |
task._set_module_name(module) |
209 |
|
print('Loaded ' + str(len(tasks)) + ' tasks from drive.') |
|
|
209 |
|
bot.print('Loaded ' + str(len(tasks)) + ' tasks from drive.') |
210 |
210 |
return tasks |
return tasks |
211 |
211 |
|
|
212 |
212 |
|
|