1
1
# CactusBot!
2
2
3
3
from messages import MessageHandler
4
- from user import User
4
+ from beam import Beam
5
5
6
6
from os .path import exists
7
- from time import sleep
8
- from json import load
7
+ from sys import exit
8
+ from json import load , dump
9
9
from shutil import copyfile
10
+ from functools import reduce
10
11
11
12
from asyncio import get_event_loop , gather , async
12
13
13
14
from traceback import format_exc
15
+ from time import sleep
14
16
15
17
from models import Base , engine
16
18
38
40
"""
39
41
40
42
41
- class Cactus (MessageHandler , User ):
43
+ class Cactus (MessageHandler , Beam ):
42
44
started = False
43
45
connected = False
44
46
message_id = 0
@@ -48,12 +50,13 @@ def __init__(self, autorestart=True, **kwargs):
48
50
self .debug = kwargs .get ("DEBUG" , False )
49
51
self .autorestart = autorestart
50
52
self .config_file = kwargs .get ("config_file" , "data/config.json" )
53
+ self .stats_file = kwargs .get ("stats_file" , "data/stats.json" )
51
54
self .database = kwargs .get ("database" , "data/data.db" )
52
55
53
- def check_db (self ):
56
+ def _init_database (self , database ):
54
57
"""Ensure the database exists."""
55
58
56
- if exists (self . database ):
59
+ if exists (database ):
57
60
self .logger .info ("Found database." )
58
61
else :
59
62
self .logger .info ("Database wasn't found." )
@@ -67,22 +70,54 @@ def load_config(self, filename):
67
70
"""Load configuration."""
68
71
69
72
if exists (filename ):
70
- self .logger .info ("Config file was found. Loading..." )
73
+ self .logger .info ("Configuration file was found. Loading..." )
74
+ self .config_file = filename
71
75
with open (filename ) as config :
72
76
self .config = load (config )
73
- return True
77
+ return self . config
74
78
else :
75
- self .logger .warn ("Config file was not found. Creating..." )
79
+ self .logger .warn ("Configuration file was not found. Creating..." )
76
80
copyfile ("data/config-template.json" , filename )
77
81
self .logger .error (
78
- "Config created. Please enter information, and restart." )
79
- raise FileNotFoundError ("Config not found." )
82
+ "Configuration file created. Please enter values and restart." )
83
+ raise FileNotFoundError ("Configuration not found." )
84
+ exit ()
85
+
86
+ def load_stats (self , filename ):
87
+ if exists (filename ):
88
+ self .stats_file = filename
89
+ self .logger .info ("Statistics file was found. Loading..." )
90
+ with open (filename ) as stats :
91
+ self .stats = load (stats )
92
+ return self .stats
93
+ else :
94
+ self .logger .warn ("Statistics file was not found. Creating..." )
95
+ copyfile ("data/stats-template.json" , "data/stats.json" )
96
+ self .logger .info ("Statistics file created." )
97
+
98
+ def update_config (self , keys , value ):
99
+ with open (self .config_file , 'r' ) as config :
100
+ config_data = load (config )
101
+ reduce (lambda d , k : d [k ], keys .split ('.' )[:- 1 ], config_data )[
102
+ keys .split ('.' )[- 1 ]] = value
103
+ with open (self .config_file , 'w+' ) as config :
104
+ dump (config_data , config , indent = 4 , sort_keys = True )
105
+ self .config = config_data
106
+
107
+ def update_stats (self , keys , value ):
108
+ with open (self .stats_file , 'r' ) as stats :
109
+ stats_data = load (stats )
110
+ reduce (lambda d , k : d [k ], keys .split ('.' )[:- 1 ], stats_data )[
111
+ keys .split ('.' )[- 1 ]] = value
112
+ with open (self .config_file , 'w+' ) as config :
113
+ dump (stats_data , config , indent = 4 , sort_keys = True )
114
+ self .config = stats_data
80
115
81
116
def run (self , * args , ** kwargs ):
82
117
"""Run bot."""
83
118
84
119
self .logger .info (cactus_art )
85
- self .check_db ( )
120
+ self ._init_database ( self . database )
86
121
87
122
while self .autorestart or not self .started :
88
123
try :
@@ -91,11 +126,11 @@ def run(self, *args, **kwargs):
91
126
loop = get_event_loop ()
92
127
93
128
self .connected = bool (loop .run_until_complete (
94
- self .connect (self .channel_data ['id' ], self .bot_data ['id' ])
129
+ self .connect (self .channel_data ["id" ], self .bot_data ["id" ])
95
130
))
96
131
97
132
self .logger .info ("{}uccessfully connected to chat {}." .format (
98
- [' Uns' , 'S' ][self .connected ], self .channel_data ["token" ]
133
+ [" Uns" , "S" ][self .connected ], self .channel_data ["token" ]
99
134
))
100
135
101
136
if self .connected :
@@ -137,8 +172,8 @@ def _run(self, *args, **kwargs):
137
172
"""Bot execution code."""
138
173
139
174
if self .load_config (filename = self .config_file ):
140
- auth = { n : self .config [ n ] for n in ( "username" , "password" )}
141
- self .bot_data = self .login (** auth )
175
+ self .load_stats ( filename = self . stats_file )
176
+ self .bot_data = self .login (** self . config [ " auth" ] )
142
177
self .username = self .bot_data ["username" ]
143
178
self .bot_id = self .bot_data ["id" ]
144
179
self .logger .info ("Authenticated as: {}." .format (self .username ))
@@ -153,7 +188,9 @@ def _run(self, *args, **kwargs):
153
188
status = ["offline" , "online" ][self .channel_data ["online" ]]
154
189
))
155
190
191
+ self ._init_commands ()
192
+
156
193
157
194
if __name__ == "__main__" :
158
- cactus = Cactus (debug = "info " , autorestart = False )
195
+ cactus = Cactus (debug = "debug " , autorestart = False , log_to_file = True )
159
196
cactus .run ()
0 commit comments