-
Notifications
You must be signed in to change notification settings - Fork 1
/
backup.py
268 lines (245 loc) · 13.1 KB
/
backup.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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
import discord
import asyncio
import sys
import json
import os
import datetime
import time
import pymongo
import subprocess
from typing import Union
from pretty_help import PrettyHelp
from discord.ext import commands
from src.storage import config
from src.checks.message_check import check_reply
from discord.ext.commands.core import _convert_to_bool
from src.helpers.storage_helper import DataHelper
from traceback import format_exc, print_tb
from src.helpers.mongo_helper import MongoDB
from src.storage.token import token # token.py is just one variable - token = "token"
class UtilsBot(commands.Bot):
def __init__(self):
# Initialises the actual commands.Bot class
intents = discord.Intents.all()
intents.members = True
super().__init__(command_prefix=self.determine_prefix, description=config.description,
loop=asyncio.get_event_loop(), intents=intents, case_insensitive=True,
help_command=PrettyHelp(color=discord.Colour.blue()))
self.guild = None
self.error_channel = None
self.data = DataHelper()
self.database_handler = None
self.latest_joins = {}
self.restart_event: Union[asyncio.Event, None] = None
self.mongo: Union[MongoDB, None] = None
self.restart_waiter_lock = asyncio.Lock()
self.restart_waiters = 0
async def get_guild_prefix(self, guild: discord.Guild):
if self.mongo is None:
return ""
guild_document = await self.mongo.find_by_id(self.mongo.discord_db.guilds, guild.id)
return guild_document.get("prefix", "")
async def determine_prefix(self, bot, message):
if not hasattr(message, "guild") or message.guild is None:
return commands.when_mentioned_or(config.bot_prefix, "u" + config.bot_prefix)(bot, message)
if self.mongo is None:
return f"u{config.bot_prefix}"
guild_document = await self.mongo.find_by_id(self.mongo.discord_db.guilds, message.guild.id)
if guild_document is None or guild_document.get("prefix") is None:
music_cog: commands.Cog = self.get_cog("Music")
if music_cog is not None:
for command in music_cog.get_commands():
possible_command = [command.name] + command.aliases
possible_command = [config.bot_prefix + x for x in possible_command]
if message.content.split(" ")[0] in possible_command:
return commands.when_mentioned_or("u" + config.bot_prefix)(bot, message)
return commands.when_mentioned_or(config.bot_prefix, "u" + config.bot_prefix)(bot, message)
else:
guild_prefix = guild_document.get("prefix")
return commands.when_mentioned_or(guild_prefix, "u" + config.bot_prefix)(bot, message)
async def get_latest_joins(self):
for guild in self.guilds:
members = await self.get_sorted_members(guild)
self.latest_joins[guild.id] = members
async def get_sorted_members(self, guild):
members = await guild.fetch_members(limit=None).flatten()
members = [member for member in members if not member.bot]
sorting_members = {member: (member,
member.joined_at.replace(tzinfo=datetime.timezone.utc)) for member in members}
for member in members:
earliest_message = await self.mongo.discord_db.messages.find_one({"user_id": member.id,
"guild_id": guild.id},
sort=[("created_at", pymongo.ASCENDING)])
if earliest_message is not None:
message_time = earliest_message.get("created_at").replace(tzinfo=datetime.timezone.utc)
if message_time < member.joined_at.replace(tzinfo=datetime.timezone.utc):
sorting_members[member] = (member, message_time)
member.joined_at = message_time
members = list(sorting_members.values())
members.sort(key=lambda x: x[1])
members = [member[0] for member in members]
members = [user for user in members if user.joined_at is not None]
return members
async def ask_boolean(self, to_reply_to: Union[discord.Message, discord.abc.Messageable], user: discord.User,
question: Union[str, discord.Embed]):
if isinstance(to_reply_to, discord.Message):
if isinstance(question, str):
sent_message = await to_reply_to.reply(question)
else:
sent_message = await to_reply_to.reply(embed=question)
else:
if isinstance(question, str):
sent_message = await to_reply_to.send(question)
else:
sent_message = await to_reply_to.send(embed=question)
try:
replied_message = await self.wait_for("message", check=check_reply(user), timeout=15.0)
if not _convert_to_bool(replied_message.content):
return False
return sent_message
except asyncio.TimeoutError:
if isinstance(question, str):
await sent_message.edit(content="No valid response detected in time, or explicit 'no' detected. "
"Request cancelled.")
else:
await sent_message.edit(embed=self.create_error_embed("No valid response detected in time, or explicit "
"'no' detected. "
"Request cancelled."))
return False
# The following embeds are just to create embeds with the correct colour in fewer words.
@staticmethod
def create_error_embed(text):
embed = discord.Embed(title="Error", description=text, colour=discord.Colour.red(),
timestamp=datetime.datetime.utcnow())
return embed
@staticmethod
def create_processing_embed(title, text):
embed = discord.Embed(title=title, description=text, colour=discord.Colour.dark_orange(),
timestamp=datetime.datetime.utcnow())
return embed
@staticmethod
def create_completed_embed(title, text):
embed = discord.Embed(title=title, description=text, colour=discord.Colour.green(),
timestamp=datetime.datetime.utcnow())
return embed
@staticmethod
def restart():
sys.exit(1)
@staticmethod
def completed_restart_write(channel_id, message_id, title, text):
with open("restart_info.json", 'w') as file:
file.write(json.dumps([channel_id, message_id, title, text, config.version_number]))
def get_bot():
bot = UtilsBot()
@bot.event
async def on_ready():
print("Ready!")
for extension_name, extension in bot.extensions.items():
bot.unload_extension(extension_name)
bot.mongo = MongoDB()
bot.guild = bot.get_guild(config.monkey_guild_id)
bot.error_channel = bot.get_channel(config.error_channel_id)
for extension_name in config.extensions:
print("Loading cog named {}...".format(extension_name))
bot.load_extension("src.cogs.{}".format(extension_name))
print("Loaded cog {}!".format(extension_name))
if os.path.exists("restart_info.json"):
with open("restart_info.json", 'r') as file:
channel_id, message_id, title, text, old_version_num = json.loads(file.read())
original_msg = await bot.get_channel(channel_id).fetch_message(message_id)
embed = bot.create_completed_embed(title, text)
embed.add_field(name="New Version: {}".format(config.version_number),
value="Previous Version: {}".format(old_version_num))
last_commit_message = subprocess.check_output(["git", "log", "-1", "--pretty=%s"]).decode("utf-8").strip()
embed.set_footer(text=last_commit_message)
await original_msg.edit(embed=embed)
os.remove("restart_info.json")
await bot.get_latest_joins()
# noinspection PyUnusedLocal
@bot.event
async def on_error(method, *args, **kwargs):
try:
embed = discord.Embed(title="MonkeyUtils experienced an error when running.", colour=discord.Colour.red())
embed.description = format_exc()[:2000]
print(format_exc())
await bot.error_channel.send(embed=embed)
# bot.restart()
except Exception as e:
print("Error in sending error to discord. Error was {}".format(format_exc()))
print("Error sending to discord was {}".format(e))
@bot.event
async def on_command_error(ctx: commands.Context, error: commands.CommandError):
if ctx.kwargs.get("resolved", False):
return
if isinstance(error, commands.CommandInvokeError):
if isinstance(error.original, discord.errors.Forbidden):
await ctx.author.send(embed=bot.create_error_embed("You ran the command `{}`, but I don't "
"have permission to send "
"messages in that channel!".format(ctx.command)))
return
print(type(error.original))
if isinstance(error, commands.BotMissingPermissions):
missing = [perm.replace('_', ' ').replace('guild', 'server').title() for perm in error.missing_perms]
if len(missing) > 2:
perms_formatted = '{}, and {}'.format(", ".join(missing[:-1]), missing[-1])
else:
perms_formatted = ' and '.join(missing)
try:
await ctx.reply(
f"In order to run these commands, I need the following permission(s): {perms_formatted}")
except discord.errors.Forbidden:
await ctx.author.send(embed=bot.create_error_embed("You ran the command `{}`, but I don't "
"have permission to send "
"messages in that channel!".format(ctx.command)))
return
if isinstance(error, commands.CommandNotFound) or isinstance(error, commands.DisabledCommand):
return
if isinstance(error, commands.CheckFailure):
try:
await ctx.send(embed=bot.create_error_embed("You don't have permission to do that, {}.".
format(ctx.message.author.mention)))
return
except discord.errors.Forbidden:
await ctx.author.send(embed=bot.create_error_embed("You ran the command `{}`, but I don't "
"have permission to send "
"messages in that channel!".format(ctx.command)))
return
try:
embed = discord.Embed(title="MonkeyUtils experienced an error in a command.", colour=discord.Colour.red())
embed.description = format_exc()[:2000]
embed.add_field(name="Command passed error", value=str(error))
if ctx.message.application is not None and ctx.message.application.get("original_content") is not None:
embed.add_field(name="Context", value=ctx.message.application.get("original_content"))
else:
embed.add_field(name="Context", value=ctx.message.content)
print_tb(error.__traceback__)
if hasattr(error, "original"):
print_tb(error.original.__traceback__)
guild_error_channel_id = await bot.mongo.discord_db.channels.find_one({"guild_id": ctx.guild.id,
"error_channel": True})
if guild_error_channel_id is None:
guild_error_channel_id = config.error_channel_id
else:
guild_error_channel_id = guild_error_channel_id.get("_id", None)
error_channel = bot.get_channel(guild_error_channel_id)
if error_channel is None:
error_channel = bot.get_channel(config.error_channel_id)
await error_channel.send(embed=embed)
await ctx.reply(embed=embed)
# bot.restart()
except Exception as e:
print("Error in sending error to discord. Error was {}".format(error))
print("Error sending to discord was {}".format(e))
return bot
if __name__ == '__main__':
utils_bot = get_bot()
try:
utils_bot.run(token)
except discord.errors.LoginFailure:
time.sleep(18000)
exit()
except discord.errors.ConnectionClosed:
if not asyncio.get_event_loop().is_closed():
asyncio.get_event_loop().close()
print("Connection Closed, exiting...")
exit()