-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
213 lines (172 loc) · 5.58 KB
/
main.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
213
"""
main.py
the main script for the program
run `py main.py` on windows or
`python3 main.py` for other os
"""
import logging
import datetime
import traceback
import os
import multiprocessing
import subprocess
import inspect
import discord
# import discord_slash
import env
import client
import version
import cmds
verbs = {
key: value for key, value in inspect.getmembers(
cmds, inspect.isfunction
) if not key.startswith("_")
}
# aliases
verbs["version"] = verbs["ver"]
verbs["help"] = verbs["chelp"]
verbs["ccalc"] = verbs["calc"]
verbs["f"] = verbs["returnf"]
@client.client.event
async def on_ready():
"""
on_ready()
event handler for when the client is ready
"""
logging.info(
"\t %s We have logged in as %s", datetime.datetime.now(), client.client.user
)
logging.debug(
"\t %s Changing discord presence"
)
await client.client.change_presence(
activity=discord.Activity(
type=discord.ActivityType.watching,
name="for commands",
)
)
async def process_message(message) -> list:
"""
process_message()
processes a message
"""
# declear variables
content = []
# ignore bots
# if message.author.bot:
# return []
# t-bot
if env.DEBUG_STATUS:
if message.content.lower().startswith("<@596544931359883274>"):
content = message.content.replace("<@596544931359883274>", "", 1).split()
elif message.content.lower().startswith("<@!596544931359883274>"):
content = message.content.replace("<@!596544931359883274>", "", 1).split()
elif message.content.lower().startswith("t!"):
content = message.content.replace("t!", "", 1).split()
# d-bot
else:
if message.content.lower().startswith("<@681294773629485071>"):
content = message.content.replace("<@681294773629485071>", "", 1).split()
elif message.content.lower().startswith("<@!681294773629485071>"):
content = message.content.replace("<@!681294773629485071>", "", 1).split()
elif message.content.lower().startswith("d!"):
content = message.content.replace("d!", "", 1).split()
# return if content
if content:
return content
# dms
if isinstance(message.channel, discord.channel.DMChannel):
return message.content.split()
@client.client.event
async def on_message(message):
"""
on_message()
event handler for when a message is sent
"""
# process message
content = await process_message(message)
# return if no message
if not content:
return
# return if no command
if len(content) < 1:
return
# log command
logging.info(
"\t%s %s: %s", datetime.datetime.now(), message.author, content
)
# get command
verb = content[0].lower()
nouns = content[1:]
# try command
try:
with message.channel.typing():
output = await verbs[verb](message, *nouns)
except Exception:
output = traceback.format_exc().replace(os.getcwd(), "")
# write to file
if not isinstance(message.channel, discord.channel.DMChannel):
with open(
os.path.join("db-database", "errors", str(message.id)),
"w",
encoding="utf-8",
) as file:
file.write('\n'.join([
str(datetime.datetime.now()),
': '.join([str(message.guild.id), str(message.guild.name)]),
': '.join([str(message.channel.id), str(message.channel.name)]),
': '.join([str(message.author.id), str(message.author)]),
str(output),
]))
# log error
logging.warning(
"\t%s \n%s: %s", datetime.datetime.now(), message.author, output
)
# try to send output
try:
await message.channel.send(output)
except Exception:
await message.channel.send(traceback.format_exc().replace(os.getcwd(), ""))
def main():
"""
main()
main function of the program
"""
logging.info("\t%s Starting database server and database sync", datetime.datetime.now())
# windows
if os.name == "nt":
multiprocessing.Process(
target=subprocess.run,
args=(("bash", "sync.sh"),),
daemon=True,
).start()
# unix
else:
multiprocessing.Process(
target=subprocess.run,
args=(("bash", "sync.sh"),),
daemon=True,
).start()
logging.info(
"\t%s main starting with discord version %s", datetime.datetime.now(), discord.__version__
)
logging.info(
"\t%s main starting with bot version %s", datetime.datetime.now(), version.version
)
client.client.run(env.TOKEN) # Start bot
# Nothing will run after that
# log critical
logging.critical(
"\t\033[41m%s\033[0m discord client has ended", datetime.datetime.now()
)
logging.critical(
"\t\033[41m%s\033[0m ending program", datetime.datetime.now()
)
if __name__ == '__main__':
logging.basicConfig(level=logging.INFO)
logging.debug("\t%s This is a debug message log", datetime.datetime.now())
logging.info("\t%s This is an info message log", datetime.datetime.now())
logging.warning("\t\033[33m%s\033[0m This is a warning message log", datetime.datetime.now())
logging.error("\t\033[31m%s\033[0m This is an error message log", datetime.datetime.now())
logging.critical("\t\033[41m%s\033[0m This is a critical message log", datetime.datetime.now())
main()