-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutil.py
More file actions
163 lines (135 loc) · 4.88 KB
/
util.py
File metadata and controls
163 lines (135 loc) · 4.88 KB
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
import numpy as np
import aiohttp,discord,os,requests,config
from discord.ext import commands
import random as rand
from resql import ReSql
from aiohttp import ClientSession
from urllib.parse import quote,unquote
from typing import Union
db = ReSql()
async def banner(user_id:int, size:int=1024):
sizes = [64, 128, 256, 512, 1024, 2048]
if not size in sizes:
return "Invalid size. Valid sizes: 64, 128, 256, 512, 1024, 2048"
async with aiohttp.ClientSession() as cs:
async with cs.get(f"https://api.popcat.xyz/banners/{user_id}") as r:
data = await r.json()
res = data['banner'].replace("?size=1024", "")
if res == "None":
return res
else:
return f"{res}?size={size}"
def enlarge(emote, size:int=None):
id = emote.split(":")[2].split(">")[0]
sizes = [64, 128, 256, 512, 1024, 2048]
url = f"https://cdn.discordapp.com/emojis/{id}.png?size={size}"
if (size is None):
return f"https://cdn.discordapp.com/emojis/{id}.png"
if (emote.startswith("<a")):
url = url.replace("png", "gif")
if not (size in sizes):
return("Invalid size. Valid sizes: 64, 128, 256, 512, 1024, 2048")
return url
async def getNSFW(endpoint:str):
key = os.getenv("key")
try:
async with aiohttp.ClientSession() as cs:
async with cs.get(f"https://api.gofaizen.repl.co/nsfw/{endpoint}?key={key}") as r:
data = await r.json()
return data['url']
except Exception as e:
raise e
def writeFile(src:str,content:any):
"""Writes {content} in the file passed in {src}. If {src} doesn't exist, it makes the file"""
try:
with open(src,"w") as deeznuts:
deeznuts.write(content)
except FileNotFoundError:
return (f"No such file or directory: '{src}'")
def createFile(name:str):
"""Creates the file with {name}."""
with open(name,"w") as deeznuts:
deeznuts.write("")
def readFile(src:str):
"""Reads the contents of the given file"""
try:
with open(src,"r") as deeznuts:
return deeznuts.read()
except FileNotFoundError:
return (f"No such file or directory: '{src}'")
except discord.errors.HTTPException:
return ("The file does not have any content to read")
def replaceText(text,text_to_replace,new_text):
return str(text).replace(str(text_to_replace), str(new_text))
def randomText(*choices):
return rand.choice(str(choices))
def random(min, max):
return np.random.randint(min, max)
async def jsonRequest(url, property=None):
async with ClientSession() as cs:
async with cs.get(url) as r:
data = await r.json()
if property==None:
return dict(data)
else:
return data[property]
def textSplit(string:str, separator:str):
try:
muhlist = string.split(separator)
db.insert("split", [each.strip() for each in muhlist])
except:
db.insert("split",None)
def splitText(index:int):
try:
index -= 1
return db.get("split")[index]
except KeyError:
return
finally:
db.insert("split",[])
def joinSplitText(separator:str):
return separator.join(db.get("split"))
def getTextSplitLength():
try:
return len(db.get("split"))
finally:
db.insert("split",[])
def url(method:str, string:str):
if (method == "encode"): return quote(string)
if (method == "decode"): return unquote(string)
encodeURIComponent = lambda string: quote(string)
decodeURIComponent = lambda string: unquote(string)
def isNaN(text):
key = os.environ["key"]
r = requests.get(f"https://api.gofaizen.repl.co/misc/isnan?num={text}&key={key}")
data = r.json()
return bool(data["isNaN"])
async def help_embed(ctx:commands.Context,command:str,example,usage,description,cooldown:int="None",aliases:str="None"):
embed = discord.Embed(description=f"""**Description:** {description} \n**Cooldown:** {cooldown} \n**Usage:** `{usage}` \n**Aliases:** {aliases} \n**Example:** \n{example}""", color=ctx.author.color).set_author(name=f"Command: py!{command}", icon_url=ctx.author.avatar_url).set_footer(text="<> = Argument required | [] = Argument optional")
await ctx.send(embed=embed)
def clean_code(content):
if content.startswith("```") and content.endswith("```"):
return "\n".join(content.split("\n")[1:])[:-3]
else:
return content
def display_stdout(content):
from prettyprinter import pprint
import io, contextlib
stdout = io.StringIO()
with contextlib.redirect_stdout(stdout):
pprint(content)
return stdout.getvalue()
pprint(content)
def status_code(url: str):
import requests
return requests.get(url.strip()).status_code
async def color_test(ctx,color: Union[int,discord.colour.Color],filler_text: str=config.lispum):
embed = discord.Embed(
description=filler_text,
color=color
)
await ctx.reply(embed=embed)
def user_avatar(ctx,user: Union[int,discord.User] = "png", size=None):
sizes = [64, 128, 256, 512, 1024, 2048]
if not (size in sizes):
raise ValueError(f'bad argument: "{size}". Must be {", ".join(sizes)}')