-
Notifications
You must be signed in to change notification settings - Fork 4
/
dev.py
567 lines (495 loc) · 23 KB
/
dev.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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
import datetime
import traceback
import discord
import requests
from colorama import Fore , Style
# Common imports that can be used by the debugger.
import json
import gc
import datetime
import time
import traceback
import re
import io
import asyncio
import random
import subprocess
from bs4 import BeautifulSoup
import urllib
import psutil
from discord.ext import commands
from discord.ext.commands import check
import os
import textwrap
from contextlib import redirect_stdout
import sys
import assets.otp_assets
import assets.reactor
import subprocess
from subprocess import Popen, PIPE
import shlex
from dislash import slash_command, ActionRow, Button, ButtonStyle, Option, OptionType
import assets.funcs as funcs
def restart_bot(mode):
if mode ==1:
os.execv(sys.executable, ['python'] + sys.argv)
if mode==2:
os.system('busybox reboot')
def devc(ctx):
with open("storage/dev.json") as f:
dev_users_list = json.load(f)
if ctx.author.id not in dev_users_list:
return False
return True
class Dev(commands.Cog):
def __init__(self, client):
self.bot = client
self._last_result = None
@staticmethod
def cleanup_code(content):
"""Automatically removes code blocks from the code."""
# remove ```py\n```
if content.startswith("```") and content.endswith("```"):
return "\n".join(content.split("\n")[1:-1])
# remove `foo`
return content.strip("` \n")
@slash_command(description="The main command check that you are a dev soul",options=[
Option("user", "Enter the user, optional", OptionType.USER, required=False)])
async def devtest(self,inter,user=None):
if not user:
user=inter.author
with open("storage/dev.json") as f:
dev_users_list = json.load(f)
if user.id not in dev_users_list:
await inter.send(
embed=discord.Embed(
title="<:Warn:922468797108080660> Permission Denied",
description="Dude! that's not a dev",
color=discord.Color.red(),
)
)
else:
owner='True <:owner:946288312220536863>' if user.id==900992402356043806 else False
await inter.send(
embed=discord.Embed(
title="<a:Tick:922450348730355712> Verified",
description=f"Found entry in Database.\n {user.mention} is a developer \n The lord? {owner}",
color=funcs.theme_color,
)
)
@slash_command(description="Lists Some poor souls having dev access for me",aliases=['devlist'])
@check(devc)
async def listdev(self,inter):
x = discord.Embed(color=funcs.theme_color)
x.set_author(icon_url='https://cdn.discordapp.com/emojis/993859165233631314.webp',name="Developer Access")
with open("storage/dev.json") as f:
dev_users_list = json.load(f)
pa =1
description=''
for user in dev_users_list:
e = self.bot.get_user(user)
mention=e.mention
description+=f"{pa}. {mention}\n"
#x.add_field(name="_ _",value=e,inline=False)
pa +=1
x.description=description
x.set_footer(text=f"Total users : {pa-1}")
await inter.send(embed=x)
@slash_command(description="Lists Some users who cant even use me",aliases=['blist','blacklisted'])
@check(devc)
async def listblack(self,inter):
x = discord.Embed(color=funcs.theme_color)
x.set_author(icon_url='https://cdn.discordapp.com/emojis/993859165233631314.webp',name="Blacklisted Users")
with open("storage/black.json") as f:
dev_users_list = json.load(f)
pa =1
for user in dev_users_list:
try:
e = self.bot.get_user(user)
x.add_field(name=e,value=e.id,inline=False)
except:
x.add_field(name='?',value="Couldn't find a user with that id ",inline=False)
pa +=1
x.set_footer(text=f"Total users : {pa-1}")
await inter.send(embed=x)
@slash_command(description="Dont have silly juicy ideas used for adding some more devs (imagine my owner is too weak)",options=[
Option("user", "Enter the user you want to give Dev access", OptionType.USER,required=True)])
@check(devc)
async def adddev(self,inter, user : None):
with open("storage/dev.json") as f:
dev_users_list = json.load(f)
if user.id not in dev_users_list and not user.bot:
dev_users_list.append(user.id)
with open("storage/dev.json", "w+") as f:
json.dump(dev_users_list, f)
x = discord.Embed(description=f"{user.mention} has been added!",color=funcs.theme_color)
x.set_author(icon_url='https://cdn.discordapp.com/emojis/993859165233631314.webp',name="Developer Added")
await inter.send(embed=x)
else:
await inter.send('See Developer , either you are adding a dev again as dev or a bot (bots arent that much cool)')
@slash_command(description="Remove an user from developer access",options=[
Option("user", "Mention person from which you want dev powers snatched 👊🏻", OptionType.USER,required=True)])
@check(devc)
async def removedev(self,inter, user : None):
with open("storage/dev.json") as f:
dev_users_list = json.load(f)
if user.id in dev_users_list:
dev_users_list.remove(user.id)
else:
await inter.send(f"{user.mention} is not in the list, so they cannot be removed!")
return
with open("storage/dev.json", "w+") as f:
json.dump(dev_users_list, f)
x = discord.Embed(description=f"{user.mention} has been removed!",color=funcs.theme_color)
x.set_author(icon_url='https://cdn.discordapp.com/emojis/993859165233631314.webp',name="Developer Removed")
await inter.send(embed=x)
@commands.command(aliases=["m","evaldev","deveval","eval"],help="Execuete some stuff")
@check(devc)
async def python_shell(self, ctx, *, code):
if ctx.guild.id !=912569937116147772:
re = discord.Embed(
title="Unverified Server",
description=f"{ctx.author.mention} This command is now only for the main server which is Tessa Bot Developers",
color=discord.Color.blue(),
)
await ctx.send(embed=re)
re.title=" Attempt of using deveval outside TBD"
re.description=f"{ctx.author.mention} Tried to use dev eval outside TBD but failed \n Requested Code \n `{code}`"
devchannel=self.bot.get_channel(979345665081610271)
return await devchannel.send(embed=re)
env = {
"ctx": ctx,
"discord": discord,
"commands": commands,
"client":ctx.bot,
"__import__": __import__,
"guild":ctx.guild,
"last_result":self._last_result
}
code = code.replace("```py", "")
code = code.replace("```", "")
if "client.http.token" in code:
return await ctx.reply(f"You can't take my token , huh {ctx.author.name}")
splitcode = []
for line in code.splitlines():
splitcode.append(line)
try:
compile(splitcode[len(splitcode)-1],"<stdin>","eval")
splitcode[len(splitcode)-1] = f"return {splitcode[len(splitcode)-1]}"
except:
pass
parsedcode = []
for line in splitcode:
parsedcode.append(" "+line)
parsedcode = "\n".join(parsedcode)
fn = f"async def eval_fn():\n{parsedcode}"
exec(fn,env)
try:
output = (await eval("eval_fn()",env))
ecolor = 0x00ff00
except Exception as error:
output = error.__class__.__name__+": "+str(error)
ecolor = 0xe60000
self._last_result = "Last Result\n"+str(output)
embed = discord.Embed(title="Eval",description="```\n"+str(output)+"\n```",colour=ecolor,timestamp=ctx.message.created_at)
embed.set_author(name=ctx.author.display_name,icon_url=ctx.author.avatar_url)
await ctx.reply(embed=embed)
@slash_command(description="Eval Code",options=[
Option("code", "Code ", OptionType.STRING,required=True)],aliases=["m","evaldev","deveval","eval"],help="Evaluate code")
@check(devc)
async def python_shells(self, inter, *, code):
await python_shell(self,inter,code)
@slash_command(description="Restart Bot",options=[
Option("mode", "Modes of Restart , 1 and 2", OptionType.INTEGER)],help='Restart Bot',aliases=['reboot'])
@check(devc)
async def restart(self,inter,mode:int=1):
listd=[1,2]
if mode not in listd:
dd = discord.Embed(title='Invalid Input',description=f' Invalid Mode = {mode} \n Accepted mode {listd}',color=discord.Color.red())
await inter.send(embed=dd)
return
await inter.reply('Restart Starting... Mental Peace')
otp_success = await assets.reactor.reactor(inter, self.bot, 'Do you want to continue the restart ', color=0x607d8b,usr=inter.author)
# random otp generator. if user enters correct otp, returns true. else, returns false
if not otp_success:
return
if len(self.bot.voice_clients)>0:
await inter.send(embed=discord.Embed(description=f"There are {len(self.bot.voice_clients)} servers listening to music through Tessarect, Do you wanna restart?"))
otp_success = await assets.reactor.reactor(inter, self.bot, 'Do you want to continue the restart process while people are listening to me ', color=0x607d8b,usr=ctx.author)
if not otp_success:
return
modedict={1:'Simple restart',2:'Advanced Restart'}
e = discord.Embed(title='Restarting Started',description=f'**Mode = {mode}**\n{modedict[mode]}',color=discord.Color.dark_teal())
e.add_field(name="Progress",value='11%')
x = await inter.send(embed=e)
await asyncio.sleep(1)
e.set_field_at(0,name="Progress",value='33%')
e.color=discord.Color.blurple()
await x.edit(embed=e)
await asyncio.sleep(1)
e.set_field_at(0,name="Progress",value='66%')
e.add_field(name="Status",value='Unloading Cogs')
e.color=discord.Color.blue()
await x.edit(embed=e)
for filename in os.listdir("./cogs"):
try:
self.bot.unload_extension(f"cogs.{filename[:-3]}")
except Exception as ex:
print(f'{filename} - {ex}')
e.set_field_at(0,name="Progress",value='99%')
e.set_field_at(1,name="Status",value='Changing Status')
e.color=discord.Color.dark_blue()
await asyncio.sleep(1)
e.set_field_at(1,name="Status",value='Restarted')
e.color=discord.Color.blue()
await x.edit(embed=e)
await self.bot.change_presence(
status=discord.Status.idle,
activity=discord.Activity(
type=discord.ActivityType.watching,
name= f"🚀 System Reboot "
))
with open("./storage/reboot.json", "w") as rebootFile:
json.dump(inter.message.channel.id, rebootFile)
restart_bot(mode)
@slash_command(name='reload', description="Reload all/one of the bot's cogs.\nThis is Dev-only, so don't have any Devny ideas.",options=[
Option("cog", "Enter the cogname ", OptionType.STRING)
# By default, Option is optional
# Pass required=True to make it a required arg
] )
@check(devc)
async def reload(self, inter, cog=None):
await inter.reply('Reloading')
if 1==1:
if not cog:
embed = discord.Embed(title="Reloading cogs!",color=discord.Color.blue())
for ext in os.listdir("./cogs/"):
if ext.endswith(".py") and not ext.startswith("_"):
try:
self.bot.unload_extension(f"cogs.{ext[:-3]}")
self.bot.load_extension(f"cogs.{ext[:-3]}")
embed.add_field(
name=f"Reloaded: `{ext}`", value='\uFEFF', inline=False)
except Exception as e:
embed.add_field(
name=f"Failed to reload: `{ext}`", value=str(e), inline=False)
await asyncio.sleep(0.5)
await inter.send(embed=embed)
return
embed = discord.Embed(
title=f"Reloading {cog}!")
#embed.color=discord.Color.green()
ext = f"{cog.lower()}.py"
if not os.path.exists(f"./cogs/{ext}"):
embed.add_field(
name=f"Failed to reload: `{ext}`", value="This cog does not exist.", inline=False)
embed.color=discord.Color.red()
elif ext.endswith(".py") and not ext.startswith("_"):
try:
self.bot.unload_extension(f"cogs.{ext[:-3]}")
self.bot.load_extension(f"cogs.{ext[:-3]}")
embed.description=f"Reloaded: `{ext}`"
embed.color=discord.Color.dark_theme()
except Exception:
desired_trace = traceback.format_exc()
if len(desired_trace) > 2500 :
print(desired_trace)
embed.description=f"Failed to reload: `{ext}`Too Big error , printed instead"
else:
embed.description=f"Failed to reload: `{ext}`"
embed.color=discord.Color.red()
await inter.send(embed=embed)
@commands.command(name='load', description='Loads a cog. Mention the python file\'s name as `cog_file_name`')
@check(devc)
async def load_cog(self, ctx, cog_file_name):
embed = discord.Embed(title=f"Loading Cog {cog_file_name}.py!",
timestamp=ctx.message.created_at)
if os.path.exists(f"./cogs/{cog_file_name}.py"):
try:
self.bot.load_extension(f"cogs.{cog_file_name}")
embed.add_field(
name=f"Loaded: `{cog_file_name}.py`", value='\uFEFF', inline=False)
#embed.color=discord.Color.green()
except Exception as e:
embed.add_field(
name=f"Failed to load: `{cog_file_name}.py`", value=str(e), inline=False)
# embed.color=discord.Color.red()
await ctx.send(embed=embed)
@commands.command(name='unload', description="Unloads a cog. Mention the python file\'s name as `cog_file_name`")
@check(devc)
async def unload_cog(self, ctx, cog_file_name):
embed = discord.Embed(title=f"Unloading Cog {cog_file_name}.py!", color=discord.Color.random(),
timestamp=ctx.message.created_at)
if os.path.exists(f"./cogs/{cog_file_name}.py"):
try:
self.bot.unload_extension(f"cogs.{cog_file_name}")
embed.add_field(
name=f"Unloaded: `{cog_file_name}.py`", value='\uFEFF', inline=False)
embed.color=discord.Color.green()
except Exception as e:
embed.add_field(
name=f"Failed to unload: `{cog_file_name}.py`", value=str(e), inline=False)
embed.color=discord.Color.red()
await ctx.send(embed=embed)
@slash_command(name="gitpull",description="Pull stuff from my github repo")
@commands.guild_only()
@check(devc)
async def update(self, inter):
print(sys.argv)
print(sys.path)
with open("./storage/update.txt", "w") as output:
subprocess.call(["git", "pull"], stdout=output)
with open("./storage/update.txt", "r") as output:
file = discord.File(output)
embed=discord.Embed(title="Git Pull ",color=funcs.theme_color)
embed.description=str(output)
await inter.send(embed=embed, file=file)
await asyncio.sleep(1)
try:
os.remove("./storage/update.txt")
except:
pass
@commands.command(name="loadjsk")
@check(devc)
async def loadjsk(self, ctx):
async with ctx.typing():
try:
self.bot.load_extension("jishaku")
except commands.ExtensionAlreadyLoaded:
pass
except commands.ExtensionNotFound:
em =discord.Embed(description="Couldnt not Load jsk")
return await ctx.send(embed=em)
em =discord.Embed(description="Loaded")
await ctx.send(embed=em)
@commands.command(name="unloadjsk")
@check(devc)
async def unloadjsk(self, ctx):
async with ctx.typing():
try:
self.bot.unload_extension("jishaku")
except commands.ExtensionNotLoaded:
pass
except commands.ExtensionNotFound:
return await ctx.send("Could not unload!")
await ctx.send("Unloaded!")
@commands.command(help="Blacklist some idiot from using me")
@check(devc)
async def blacklist(self,ctx, user : discord.Member,*,reason:str):
with open("storage/black.json") as f:
users_list = json.load(f)
if user.id==ctx.author.id:
return await ctx.send('You cant blacklisted yourself')
if user.id not in users_list :
users_list.append(user.id)
e=discord.Embed(title="Blacklisted User!",description=f":white_medium_square: Member {user.mention}\n:white_medium_square:Reason: {reason}\n :white_medium_square: Developer {ctx.author.mention}",color=discord.Color.red())
e.set_author(name=user.name,icon_url=user.avatar_url)
logschannel=self.bot.get_channel(979345665081610271)
await logschannel.send(embed=e)
with open("storage/black.json", "w+") as f:
json.dump(users_list, f)
await ctx.send(f"{user.mention} has been added!")
else:
await ctx.send('See Developer , either you are adding a blacklisted user again ')
@commands.command(help="Unblacklist some users if they are behaving nice")
@check(devc)
async def unblacklist(self,ctx, user : discord.Member):
with open("storage/black.json") as f:
users_list = json.load(f)
if user.id in users_list:
users_list.remove(user.id)
e=discord.Embed(title="UnBlacklisted User!",description=f":white_medium_square: Member {user.mention}\n :white_medium_square: Developer {ctx.author.mention}",color=discord.Color.gold())
e.set_author(name=ctx.author.name,icon_url=ctx.author.avatar_url)
logschannel=self.bot.get_channel(979345665081610271)
await logschannel.send(embed=e)
else:
await ctx.send(f"{user.mention} is not in the list, so they cannot be removed!")
return
with open("storage/black.json", "w+") as f:
json.dump(users_list, f)
await ctx.send(f"{user.mention} has been removed!")
@check(devc)
@commands.command(hidden=True,help="get info on all my guilds , add False to get Compact Info")
async def servers(self,ctx,compact=True):
activeservers = self.bot.guilds
if not compact:
for guild in activeservers:
name=str(guild.name)
description=str(guild.description)
owner=str(guild.owner)
_id = str(guild.id)
region=str(guild.region)
memcount=str(guild.member_count)
icon = str(guild.icon_url)
ver = str(ctx.guild.verification_level)
embed=discord.Embed(
title=name +" Server Information",
description=description,
color=discord.Color.blue()
)
embed.set_thumbnail(url=icon)
embed.add_field(name="Owner",value=owner,inline=True)
embed.add_field(name="Server Id",value=_id,inline=True)
embed.add_field(name="Region",value=region,inline=True)
embed.add_field(name="Member Count",value=memcount,inline=True)
embed.add_field(name="Verification Level",value=ver,inline=True)
await ctx.send(embed=embed)
else:
embed=discord.Embed(title="My Guilds",color=discord.Color.dark_theme())
for guild in activeservers:
embed.add_field(name=guild,value=f"({str(guild.id)}) -{str(guild.member_count)} **({str(guild.owner)})**",inline=False)
await ctx.send(embed=embed)
@check(devc)
@commands.command()
async def reply(self,ctx,member:discord.User,*,content:str):
embed = discord.Embed(timestamp=ctx.message.created_at,description=content, color=0x8df2db)
embed.set_footer(text=f"Sent by {ctx.author}")
try:
await member.send(embed=embed)
except:
await ctx.send(embed=discord.Embed(title="User has DM's Disabled",color=discord.Color.red()))
return
await ctx.send(embed= discord.Embed(title=f"<:user:941986233574367253> DM SENT <:sucess:935052640449077248> ", description=f"Your Message was Successfully Sent.", timestamp=ctx.message.created_at, color=0x02e7e7))
@check(devc)
@commands.command(hidden=True)
async def invservers(self,ctx):
invites = []
em=discord.Embed(color=discord.Color.dark_theme())
for guild in self.bot.guilds:
for c in guild.text_channels:
if c.permissions_for(guild.me).create_instant_invite: # make sure the bot can actually create an invite
invite = await c.create_invite()
em.add_field(name=guild , value=invite)
break
print(invites) # stop iterating over guild.text_channels, since you only need one invite per guild
await ctx.send(embed=em)
@commands.group()
@commands.check(devc)
async def change(self, ctx):
if ctx.invoked_subcommand is None:
await ctx.send_help(str(ctx.command))
@change.command(name="nickname")
@commands.check(devc)
async def change_nickname(self, ctx, *, name: str = None):
""" Change nickname. """
try:
await ctx.guild.me.edit(nick=name)
if name:
await ctx.send(f"Successfully changed nickname to **{name}**")
else:
await ctx.send("Successfully removed nickname")
except Exception as err:
await ctx.send(err)
@change.command(name="username")
@commands.check(devc)
async def change_username(self, ctx, *, name: str):
""" Change username. """
try:
await self.bot.user.edit(username=name)
await ctx.send(f"Successfully changed username to **{name}**")
devchannel=self.bot.get_channel(979345665081610271)
await devchannel.send(f"{ctx.author} successfully changed my username to **{name}**")
except discord.HTTPException as err:
await ctx.send(err)
def setup(client):
client.add_cog(Dev(client))