-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzeus.py
214 lines (174 loc) · 5.58 KB
/
zeus.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
214
'''
Author: k1rk and charge
Copyright: Zeus Selfbot (c) inc.
'''
# -- standard libraries -- #
import sys
import os
from time import sleep
import logging
from pathlib import Path
import json
# -- 3rd party libararies -- #
import discord
from colorama import *
from discord.ext import commands
from pypresence import Presence
# -- local libraries -- #
from config import Loader
from embeds import Embeds
# -- Globals and Constants -- #
LOG_LEVEL = logging.INFO
LOG_DATE_FMT = '%d/%m/%Y %I:%M %p'
LOG_SEP_STRING = '\n-------------------------------\n'
LOG_FMT = LOG_SEP_STRING[1:] + 'Time: %(asctime)s\nLevel: %(levelname)s\nName: %(name)s\nData: %(message)s' + LOG_SEP_STRING
LOG_DIREC = Path('./logs')
SETTINGS_DIREC = Path('./settings')
CONFIG_FILE = SETTINGS_DIREC / 'zeus.config.json'
ERROR_FILE_NAME = LOG_DIREC / 'zeus.errors.log'
MESSAGE_LOG_FILE_NAME = LOG_DIREC / 'zeus.message-log.json'
CLIENT_ID = ''
zeus = discord.Client()
# -- main class -- #
class Zeus(commands.Bot):
'''
simple class that inherits from commands.Bot
'''
global banner
global ext
def __init__(self, filename):
self.rp = Presence(CLIENT_ID)
self.config_file_name = filename
self.config = None
self.embeds = None
self.i = 0
def ext(self):
zeus = commands.Bot(
command_prefix=self.config.bot.prefix,
self_bot=True
)
def load_config(self):
# load the config and load the embeds
self.config = Loader(self.config_file_name).load_config()
self.embeds = Embeds(self.config)
# def load_rp(self):
# check if presence is set to true in the config file
#if self.config.bot.presence:
# connect and update to config file values
# self.rp.connect()
#self.rp.update(
# state=self.config.rp.state,
# details=self.config.rp.details,
#large_image='big',
#small_image='small',
#small_text=self.config.rp.hover_small,
#large_text=self.config.rp.hover_big
#)
def init_error_n_message_log(self):
# -- init the error log file -- #
logging.getLogger('discord').setLevel(logging.ERROR)
logging.getLogger('requests').setLevel(logging.ERROR)
logging.getLogger('pypresence').setLevel(logging.ERROR)
# -- make sure direcotory exists -- #
if not LOG_DIREC.exists():
os.mkdir(LOG_DIREC)
elif not LOG_DIREC.is_dir():
os.remove(LOG_DIREC)
os.mkdir(LOG_DIREC)
# -- init the error file -- #
if not ERROR_FILE_NAME.exists():
open(ERROR_FILE_NAME, 'w+').close()
elif not ERROR_FILE_NAME.is_file():
os.remove(ERROR_FILE_NAME)
open(ERROR_FILE_NAME, 'w+').close()
# -- init the messsage log file -- #
if not MESSAGE_LOG_FILE_NAME.exists():
open(MESSAGE_LOG_FILE_NAME, 'w+').close()
elif not MESSAGE_LOG_FILE_NAME.is_file():
os.remove(MESSAGE_LOG_FILE_NAME)
open(MESSAGE_LOG_FILE_NAME, 'w+').close()
# set the logging config
logging.basicConfig(
level=LOG_LEVEL,
filename=ERROR_FILE_NAME,
datefmt=LOG_DATE_FMT,
format=LOG_FMT
)
def banner():
b = f'{Fore.CYAN}'
w = f'{Fore.WHITE}'
print(f'''
{b}Zeus {w}Loaded!
{w}══════════════════════════════════════════════
{b}╔═╗╔═╗╦ ╦╔═╗
{b}╔═╝║╣ ║ ║╚═╗
{b}╚═╝╚═╝╚═╝╚═╝
{w}══════════════════════════════════════════════
''')
def log_init(self):
with open(MESSAGE_LOG_FILE_NAME, 'w') as f:
json.dump({}, f, indent=2)
def new_log(self, data):
self.i += 1
# get data
with open(MESSAGE_LOG_FILE_NAME, 'r') as fo:
yes = json.load(fo)
# update
x = {f'message: {self.i}': data}
yes.update(x)
# rewrite
with open(MESSAGE_LOG_FILE_NAME, 'w') as f:
json.dump(yes, f, indent=2)
def get_cogs(self):
return [
f'cogs.{x[:-3]}' for x in os.listdir('./cogs')
if x.endswith('.py')
]
def _load(self):
for cog in self.get_cogs():
super().load_extension(cog)
def _unload(self):
for cog in self.get_cogs():
super().unload_extension(cog)
def refresh(self):
self._unload()
self._load()
def init(self):
# load config and init error log
self.load_config()
self.init_error_n_message_log()
self.log_init()
# call the clients init method with desired config
super().__init__(
description=self.config.bot.name,
command_prefix=self.config.bot.prefix,
self_bot=True
)
# load all the cogs
self._load()
#if os.name == 'nt':
# load rp
#self.load_rp()
def go(self):
super().remove_command('help')
super().run(self.config.bot.token, bot=False)
# -- create a new bot instance -- #
bot = Zeus(CONFIG_FILE)
bot.init()
# -- base commands for the bot -- #
@bot.command(name='reload', aliases=['refresh', 'reset', 'restart', 'onoff'])
async def reload(ctx):
# load config reload cogs and delete original message
bot.load_config()
if bot.config.bot.delete:
await ctx.message.delete()
bot.refresh()
# send confirmation
embed = discord.Embed(
title='**Reloaded**')
await ctx.send(
embed=embed
)
# -- start running the bot -- #
banner()
bot.go()