-
Notifications
You must be signed in to change notification settings - Fork 1
/
script.py
212 lines (143 loc) · 5.22 KB
/
script.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
# MundusBot script #
from lib import utilities
from lib import mundusfuncs
# Always import custom just in case, even if file is empty
from lib import custom
from telegram.ext import CommandHandler, Updater, MessageHandler, Filters
version = 2.0 # BLOOD
# == Global Variables ==#
# Connection to Database
db = utilities.returnSetting('server_database')
db_connection = utilities.create_connection(db)
# Telegram Chat Token #
token = utilities.returnSetting('bot_token')
# Updater Object #
updater = Updater(token, use_context=True)
# Dispatcher #
dispatcher = updater.dispatcher
# ===== Commands =====#
def getUser(update, context):
if context.args == []:
username = utilities.formatUserName(update)
else:
username = context.args[0]
telegram_msg = utilities.catchDebug(
mundusfuncs.getUserInfo(username, db_connection))
context.bot.send_message(
chat_id=update.effective_chat.id, text=(telegram_msg))
def addUser(update, context):
username = utilities.formatUserName(update)
telegram_msg = utilities.catchDebug(
mundusfuncs.addUsertoDb(username, db_connection))
context.bot.send_message(
chat_id=update.effective_chat.id, text=(telegram_msg)
)
def addInfo(update, context):
username = utilities.formatUserName(update)
telegram_msg = utilities.catchDebug(
mundusfuncs.addData(username, context.args, db_connection))
context.bot.send_message(
chat_id=update.effective_chat.id, text=(telegram_msg)
)
def updateStatus(update, context):
username = utilities.formatUserName(update)
telegram_msg = utilities.catchDebug(mundusfuncs.updateUserStatus(
username, context.args, db_connection))
context.bot.send_message(
chat_id=update.effective_chat.id, text=(telegram_msg)
)
def getStatus(update, context):
if context.args == []:
username = utilities.formatUserName(update)
else:
username = context.args[0]
telegram_msg = utilities.catchDebug(
mundusfuncs.getUserStatus(username, db_connection))
context.bot.send_message(
chat_id=update.effective_chat.id, text=(telegram_msg)
)
# Help
def getHelp(update, context):
telegram_msg = utilities.catchDebug(mundusfuncs.fetchHelp())
context.bot.send_message(
chat_id=update.effective_chat.id, text=(telegram_msg)
)
# Get Gas
def getGas(update, context):
gas_result = utilities.catchDebug(mundusfuncs.returnGas())
context.bot.send_message(
chat_id=update.effective_chat.id, text=(gas_result))
# Get Homework
def getHomework(update, context):
telegram_msg = utilities.catchDebug(mundusfuncs.getHwk(db_connection))
context.bot.send_message(
chat_id=update.effective_chat.id, text=(telegram_msg)
)
def getUserlvl(update, context):
if context.args == []:
username = utilities.formatUserName(update)
else:
username = context.args[0]
telegram_msg = utilities.catchDebug(
mundusfuncs.getLevel(username, db_connection))
context.bot.send_message(
chat_id=update.effective_chat.id, text=(telegram_msg)
)
# Unknown Command
def unknownCmd(update, context):
response = 'Sorry, I didn\'t understand that command. Try /help to see a list of commands'
update.message.reply_text(text=response)
# ===== Admin Commands ===== #
def displayAllUsers(update, context):
username = utilities.formatUserName(update)
list_of_users = utilities.catchDebug(
mundusfuncs.displayAll(username, db_connection))
context.bot.send_message(
chat_id=update.effective_chat.id, text=(list_of_users)
)
def setHomework(update, context):
username = utilities.formatUserName(update)
telegram_msg = utilities.catchDebug(
mundusfuncs.setHwk(username, context.args, db_connection))
context.bot.send_message(
chat_id=update.effective_chat.id, text=(telegram_msg)
)
def setUserlvl(update, context):
username = utilities.formatUserName(update)
telegram_msg = utilities.catchDebug(
mundusfuncs.setLevel(username, context.args, db_connection))
context.bot.send_message(
chat_id=update.effective_chat.id, text=(telegram_msg)
)
# ===== Handlers ===== #
# Command Dictionary is always filled with default commands
command_dict = {
'getuser': getUser,
'adduser': addUser,
'updateurl': addInfo,
'setstatus': updateStatus,
'getstatus': getStatus,
'displayallusers': displayAllUsers,
'help': getHelp,
'gas': getGas,
'sethwk': setHomework,
'gethwk': getHomework,
'getlevel': getUserlvl,
'setlevel': setUserlvl
}
# Pulls the custom functions written by the user in ./lib/custom.py
if (utilities.returnSetting('custom_functions')):
for key in custom.custom_cmds:
command_dict[key] = custom.custom_cmds[key]
# Dispatch all commands found in the dictionary
for key in command_dict:
try:
print(f'Passing {key} to dispatcher')
cmd = CommandHandler(key, command_dict[key])
dispatcher.add_handler(cmd)
except Exception as e:
print(f'Something happened!{e}')
# Dispatch misc cmd handler seperately
dispatcher.add_handler(MessageHandler(Filters.command, unknownCmd))
# End of Line
updater.start_polling()