-
Notifications
You must be signed in to change notification settings - Fork 11
/
utils_embeds.py
362 lines (290 loc) · 17.2 KB
/
utils_embeds.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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
'''
Copyright (C) 2021-2022 Katelynn Cadwallader.
This file is part of Gatekeeper, the AMP Minecraft Discord Bot.
Gatekeeper is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3, or (at your option)
any later version.
Gatekeeper is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
License for more details.
You should have received a copy of the GNU General Public License
along with Gatekeeper; see the file COPYING. If not, write to the Free
Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA
02110-1301, USA.
'''
from __future__ import annotations
import logging
import discord
from discord.ext import commands
import DB
import AMP_Handler
import utils
class botEmbeds():
"""Gatekeeper Embeds/Banners"""
def __init__(self, client: commands.Bot = None):
self._client = client
self.logger = logging.getLogger(__name__)
self.logger.debug('Utils Bot Loaded')
self.DBHandler = DB.getDBHandler()
self.DB = self.DBHandler.DB # Main Database object
self.DBConfig = self.DBHandler.DBConfig
self.uBot = utils.botUtils(client)
self.AMPHandler = AMP_Handler.getAMPHandler()
self.AMPInstances = self.AMPHandler.AMP_Instances
self.AMPServer_Avatar_urls = []
def default_embedmsg(self, title, context: commands.Context, description=None, field=None, field_value=None) -> discord.Embed:
"""This Embed has only one Field Entry."""
embed = discord.Embed(title=title, description=description, color=0x808000) # color is RED
embed.set_author(name=context.author.display_name, icon_url=context.author.avatar)
embed.add_field(name=field, value=field_value, inline=False)
return embed
async def server_info_embed(self, server: AMP_Handler.AMP.AMPInstance, context: commands.Context) -> discord.Embed:
"""For Individual Server info embed replies"""
db_server = self.DB.GetServer(InstanceID=server.InstanceID)
server_name = db_server.InstanceName
if db_server.DisplayName != None:
server_name = db_server.DisplayName
embed = discord.Embed(title=f'__**{server_name}**__ - {[server.TargetName]}', color=0x00ff00, description=server.Description)
discord_role = db_server.Discord_Role
if discord_role != None:
discord_role = context.guild.get_role(int(db_server.Discord_Role)).name
avatar = await self.uBot.validate_avatar(db_server)
if avatar != None:
embed.set_thumbnail(url=avatar)
embed.add_field(name=f'Host:', value=str(db_server.Host), inline=False)
embed.add_field(name='Donator Only:', value=str(bool(db_server.Donator)), inline=True)
embed.add_field(name='Whitelist Open:', value=str(bool(db_server.Whitelist)), inline=True)
embed.add_field(name='Role:', value=str(discord_role), inline=False)
embed.add_field(name='Hidden', value=bool(db_server.Hidden), inline=True)
embed.add_field(name='Whitelist Hidden', value=bool(db_server.Whitelist_disabled), inline=True)
embed.add_field(name='Filtered Console:', value=str(bool(db_server.Whitelist)), inline=False)
embed.add_field(name='Console Filter Type:', value=bool(db_server.Console_Filtered_Type), inline=True)
if db_server.Discord_Console_Channel != None:
discord_channel = context.guild.get_channel(db_server.Discord_Console_Channel)
embed.add_field(name='Console Channel:', value=discord_channel.name, inline=False)
else:
embed.add_field(name='Console Channel:', value=db_server.Discord_Console_Channel, inline=False)
embed.add_field(name='Discord Chat Prefix:', value=str(db_server.Discord_Chat_Prefix), inline=False)
if db_server.Discord_Chat_Channel != None:
discord_channel = context.guild.get_channel(db_server.Discord_Chat_Channel)
embed.add_field(name='Chat Channel:', value=discord_channel.name, inline=True)
else:
embed.add_field(name='Chat Channel:', value=db_server.Discord_Chat_Channel, inline=True)
if db_server.Discord_Event_Channel != None:
discord_channel = context.guild.get_channel(db_server.Discord_Event_Channel)
embed.add_field(name='Event Channel:', value=discord_channel.name, inline=True)
else:
embed.add_field(name='Event Channel:', value=db_server.Discord_Event_Channel, inline=True)
embed.set_footer(text=f'InstanceID: {server.InstanceID}')
return embed
async def server_display_embed(self, server_list: list[DB.DBServer], banner_name: str, guild: discord.Guild = None, ) -> list[discord.Embed]:
"""Used for Banner Groups and Display"""
embed_list = []
for db_server in server_list:
if db_server == None:
self.DB.Remove_Server_from_BannerGroup(banner_groupname=banner_name, instanceID=db_server.InstanceID)
try:
server = self.AMPInstances[db_server.InstanceID]
except:
self.DB.Remove_Server_from_BannerGroup(banner_groupname=banner_name, instanceID=db_server.InstanceID)
# If no DB Server or the Server is Hidden; skip.
if db_server == None or db_server.Hidden == 1:
continue
instance_status = '\U0000274c Offline'
dedicated_status = 'Offline'
Users = None
User_list = None
# This is for the Instance
if server.Running:
instance_status = 'Online'
# ADS AKA Application status
if server._ADScheck() and server.ADS_Running:
dedicated_status = 'Online'
Users = server.getUsersOnline()
if len(server.getUserList()) >= 1:
User_list = (', ').join(server.getUserList())
embed_color = 0x71368a
if guild != None and db_server.Discord_Role != None:
db_server_role = guild.get_role(int(db_server.Discord_Role))
if db_server_role != None:
embed_color = db_server_role.color
server_name = server.FriendlyName
if server.DisplayName != None:
server_name = db_server.DisplayName
embed = discord.Embed(title=f'**======= {server_name} =======**', description=server.Description, color=embed_color)
# This is for future custom avatar support.
avatar = await self.uBot.validate_avatar(db_server)
if avatar != None:
embed.set_thumbnail(url=avatar)
embed.add_field(name='**Instance Status**:', value=instance_status, inline=False)
embed.add_field(name='**Dedicated Server Status**:', value=dedicated_status, inline=False)
embed.add_field(name='**Host**:', value=str(db_server.Host), inline=True)
embed.add_field(name='**Donator Only**:', value=str(bool(db_server.Donator)), inline=True)
embed.add_field(name='**Whitelist Open**:', value=str(bool(db_server.Whitelist)), inline=True)
if Users != None:
embed.add_field(name=f'**Players**:', value=f'{Users[0]}/{Users[1]}', inline=True)
else:
embed.add_field(name='**Player Limit**:', value=str(Users), inline=True)
embed.add_field(name='**Players Online**:', value=str(User_list), inline=False)
embed.set_footer(text=discord.utils.utcnow().strftime('%Y-%m-%d | %H:%M') + " UTC")
embed_list.append(embed)
return embed_list
async def server_status_embed(self, context: commands.Context, server: AMP_Handler.AMP.AMPInstance, TPS=None, Users=None, CPU=None, Memory=None, Uptime=None, Users_Online=None) -> discord.Embed:
"""This is the Server Status Embed Message"""
db_server = self.DB.GetServer(InstanceID=server.InstanceID)
if server.Running:
instance_status = 'Online'
else:
instance_status = 'Offline'
if server.ADS_Running:
server_status = 'Online'
else:
server_status = 'Offline'
embed_color = 0x71368a
if db_server.Discord_Role != None:
db_server_role = context.guild.get_role(int(db_server.Discord_Role))
if db_server_role != None:
embed_color = db_server_role.color
server_name = server.FriendlyName
if server.DisplayName != None:
server_name = db_server.DisplayName
embed = discord.Embed(title=f"{server_name} - [{server.TargetName}]", description=f'Instance Server Status: **{instance_status}**', color=embed_color)
avatar = await self.uBot.validate_avatar(db_server)
if avatar != None:
embed.set_thumbnail(url=avatar)
embed.add_field(name='**Dedicated Server Status**:', value=server_status, inline=True)
if db_server.Host != None:
embed.add_field(name=f'Host: ', value=db_server.Host, inline=True)
# embed.add_field(name='\u1CBC\u1CBC',value='\u1CBC\u1CBC',inline=False)
embed.add_field(name='Donator Only:', value=str(bool(db_server.Donator)), inline=True)
embed.add_field(name='Whitelist Open:', value=str(bool(db_server.Whitelist)), inline=True)
# embed.add_field(name='\u1CBC\u1CBC',value='\u1CBC\u1CBC',inline=False) #This Generates a BLANK Field entirely.
if server.ADS_Running:
embed.add_field(name='TPS', value=TPS, inline=True)
embed.add_field(name='Player Count', value=f'{Users[0]}/{Users[1]}', inline=True)
embed.add_field(name='Memory Usage', value=f'{Memory[0]}/{Memory[1]}', inline=True)
embed.add_field(name='CPU Usage', value=f'{CPU}/100%', inline=True)
#!UPTIME is disabled until AMP Impliments the feature.
#embed.add_field(name='Uptime', value=Uptime, inline=True)
embed.add_field(name='Players Online', value=Users_Online, inline=True)
embed.set_footer(text=f'InstanceID: {server.InstanceID}')
return embed
# Depreciated; no longer in use.
async def server_whitelist_embed(self, context: commands.Context, server: AMP_Handler.AMP.AMPInstance) -> discord.Embed:
"""Default Embed Reply for Successful Whitelist requests"""
db_server = self.DB.GetServer(InstanceID=server.InstanceID)
embed_color = 0x71368a
if db_server != None:
if db_server.Discord_Role != None:
db_server_role = context.guild.get_role(int(db_server.Discord_Role))
if db_server_role != None:
embed_color = db_server_role.color
User_list = None
if len(server.getUserList()) > 1:
User_list = (', ').join(server.getUserList())
server_name = server.FriendlyName
if server.DisplayName != None:
server_name = db_server.DisplayName
embed = discord.Embed(title=f'**======= {server_name} =======**', description=db_server.Description, color=embed_color)
avatar = await self.uBot.validate_avatar(db_server)
if avatar != None:
embed.set_thumbnail(url=avatar)
embed.add_field(name='**Host**:', value=str(db_server.Host), inline=True)
embed.add_field(name='Users Online:', value=str(User_list), inline=False)
return embed
def bot_settings_embed(self, context: commands.Context) -> discord.Embed:
"""Default Embed Reply for command /bot settings, please pass in a List of Dictionaries eg {'setting_name': 'value'}"""
embed = discord.Embed(title=f'**Bot Settings**', color=0x71368a)
embed.set_thumbnail(url=context.guild.icon)
embed.add_field(name='\u1CBC\u1CBC', value='\u1CBC\u1CBC', inline=False)
# This allows me to control which settings display first.
layout = ["bot_version",
"db_version",
"guild_id",
"moderator_role_id",
"permissions",
"message_timeout",
"banner_type",
"banner_auto_update",
"auto_whitelist",
"whitelist_wait_time",
"whitelist_request_channel",
"donator_role_id",
"donator_bypass"]
# Take our list and store it in a seperate list and lowercase the strings.
db_config_settingslist = [x.lower() for x in self.DBConfig.GetSettingList()]
for key in layout:
# If the key is not in the DB; skip.
if key not in db_config_settingslist:
continue
db_config_settingslist.remove(key)
value = self.DBConfig.GetSetting(key)
key = key.lower()
if key == 'auto_whitelist':
embed.add_field(name='Auto Whitelisting:', value=f'{"True" if value == 1 else "False"}')
elif key == 'whitelist_wait_time':
embed.add_field(name='Whitelist Wait Time:', value=f'{"Instantly" if value == 0 else (str(value) + " Minutes")} ', inline=False)
elif key == 'whitelist_request_channel':
if value != 'None':
value = context.guild.get_channel(value)
embed.add_field(name='Whitelist Request Channel:', value=f'{value.name.title() if value != None else "Not Set"}', inline=False)
elif key == 'message_timeout':
embed.add_field(name='Gatekeeper Reply Timeout', value=f'{value} Seconds', inline=False)
elif key == 'permissions':
if value == 0:
value = 'Default'
elif value == 1:
value = 'Custom'
embed.add_field(name='Permissions:', value=f'{value}', inline=True)
elif key == 'banner_type':
if value == 0:
value = 'Discord Embeds'
elif value == 1:
value = 'Custom Banner Images'
embed.add_field(name='Banner Type:', value=f'{value}', inline=False)
elif key == 'banner_auto_update':
embed.add_field(name='Banner Auto Update:', value=f'{"True" if value == 1 else "False"}', inline=True)
elif key == 'db_version':
embed.add_field(name='Database Version:', value=f'{value}', inline=True)
elif key == 'bot_version':
embed.add_field(name='Gatekeeper Version:', value=f'{value}', inline=True)
elif key == 'guild_id':
if self._client != None and value != 'None':
value = self._client.get_guild(value)
embed.add_field(name='Guild ID:', value=f'{value.name.title() if value != None else "Not Set"}', inline=False)
elif key == 'moderator_role_id':
if value != 'None':
value = context.guild.get_role(value)
embed.add_field(name='Moderator Role:', value=f'{value.name.title() if value != None else "Not Set"}', inline=True)
elif key == "donator_role_id":
if value != 'None':
value = context.guild.get_role(value)
embed.add_field(name='Donator Role ID:', value=f'{value.name.title() if value != None else "Not Set"}', inline=True)
elif key == 'donator_bypass':
embed.add_field(name='Donator Bypass:', value=f'{"True" if value == 1 else "False"}', inline=True)
# This iterates through the remaining keys of the Settings List and adds them to the Embed.
for key in db_config_settingslist:
value = self.DBConfig.GetSetting(key)
key = key.replace("_", " ").title() # Turns `auto_whitelist`` into `Auto Whitelist`
# For our possible bool entries (0, 1) to True and False respectively.
if type(value) == int:
embed.add_field(name=key, value=f'{"False" if value == 0 else "True"}', inline=False)
else:
embed.add_field(name=key, value=value, inline=False)
return embed
def user_info_embed(self, db_user: DB.DBUser, discord_user: discord.User) -> discord.Embed:
embed = discord.Embed(title=f'{discord_user.name}', description=f'**Discord ID**: {discord_user.id}', color=discord_user.color)
embed.set_thumbnail(url=discord_user.avatar.url)
embed.add_field(name='In Database:', value=f'{"True" if db_user != None else "False"}')
if db_user != None:
if db_user.MC_IngameName != None:
embed.add_field(name='Minecraft IGN:', value=f'{db_user.MC_IngameName}', inline=False)
if db_user.MC_UUID != None:
embed.add_field(name='Minecraft UUID:', value=f'{db_user.MC_UUID}', inline=True)
if db_user.SteamID != None:
embed.add_field(name='Steam ID:', value=f'{db_user.SteamID}', inline=False)
if db_user.Role != None:
embed.add_field(name='Permission Role:', value=f'{db_user.Role}', inline=False)
return embed