-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathutility.py
128 lines (109 loc) · 5.01 KB
/
utility.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
import discord
from discord.ext import commands
import yaml
import aiohttp
from embed import default_embed
session = aiohttp.ClientSession()
async def check_doc_exists(ctx, doc, version):
base = f'https://{doc}.roblox.com'
async with session.get(f'{base}/docs/json/{version}') as r:
# throws ClientConnectException if base is not a valid subdomain e.g. xx.roblox.com
if r.status != 200:
await ctx.send("Sorry, those docs don't exist.")
return
else:
data = await r.json()
return data, discord.Embed(description=base)
async def fetch_embed(filename: str, author: str):
with open(f'yaml/{filename}.yml') as file:
j = file.read()
d = yaml.load(j, Loader=yaml.FullLoader)
emb = discord.Embed.from_dict(d)
emb.set_author(name=author, icon_url='https://avatars1.githubusercontent.com/u/42101452?s=200&v=4')
return emb, d
class Utility(commands.Cog):
def __init__(self, bot):
self.bot = bot
@commands.command()
async def ping(self, ctx):
resp = await ctx.send('Pong! Loading...', delete_after=1.0)
diff = resp.created_at - ctx.message.created_at
totalms = 1000 * diff.total_seconds()
emb = default_embed('Pong!')
emb.add_field(name="Message response time", value=f"{totalms}ms") # Time for receive+process+reply cmd
emb.add_field(name="API latency", value=f"{(1000 * self.bot.latency):.1f}ms") # Official heartbeat latency
await ctx.send(embed=emb)
@commands.command(aliases=["libs", "libraries", "librarylist"])
async def list(self, ctx, lang: str):
_, yml = await fetch_embed('libs', 'Libraries')
libs = next(
(lang_lib for lang_lib in yml['list'] for lang_str in lang_lib['lang'] if lang.lower() == lang_str.lower()),
None)
if libs:
emb = default_embed(f'{libs["lang"][0]} libraries/frameworks')
for lib in libs['libs']:
# It can only get users/channels that have mutual servers with the bot
# use fetch_xx() if want to fetch users/channels that don't have mutual servers
ch = self.bot.get_channel(lib['chid'])
value = f"[Repo]({lib['url']}) {ch.mention} |"
for uid in lib['uid']:
user = self.bot.get_user(uid)
value += f' {user.mention}'
emb.add_field(name=lib['name'], value=value, inline=False)
await ctx.send(embed=emb)
else:
await ctx.send('Unknown language.')
@commands.command(aliases=["codeblocks"])
async def codeblock(self, ctx):
emb, _ = await fetch_embed('codeblock', 'Codeblocks')
await ctx.send(embed=emb)
@commands.command(aliases=["cookies"])
async def cookie(self, ctx):
emb, _ = await fetch_embed('cookie', 'Cookies')
await ctx.send(embed=emb)
@commands.command()
async def docs(self, ctx, doc: str, version: str):
data, embed = await check_doc_exists(ctx, doc, version)
if embed is None:
return
i = 0
embed.set_author(name=f'{doc.capitalize()} {version}',
icon_url="https://avatars1.githubusercontent.com/u/42101452?s=200&v=4")
for path in data['paths']:
for method in data['paths'][path]:
docs = data['paths'][path][method]
desc = f"""{docs['summary']}"""
embed.add_field(name=f"{method.upper()} {path}", value=desc, inline=True)
if i >= 25:
await ctx.send(embed=embed)
embed = discord.Embed(title=data['info']['title'])
i = 0
i += 1
await ctx.send(embed=embed)
@commands.command()
async def doc(self, ctx, doc: str, version: str, *, args):
data, embed = await check_doc_exists(ctx, doc, version)
if embed is None:
return
embed.set_author(name=f'{doc.capitalize()} {version}',
icon_url="https://avatars1.githubusercontent.com/u/42101452?s=200&v=4")
embed.set_footer(text=f'Keyword(s): {args}')
for path in data['paths']:
for method in data['paths'][path]:
docs = data['paths'][path][method]
if docs['summary'].find(args) != -1:
desc = f"""{docs['summary']}"""
embed.add_field(name=f"{method.upper()} {path}", value=desc, inline=True)
await ctx.send(embed=embed)
return
await ctx.send("Sorry, that keyword was not found in docs specified")
@commands.command(aliases=["apisites", "robloxapi", "references", "reference"])
async def api(self, ctx):
emb, _ = await fetch_embed('endpoints', 'References')
await ctx.send(embed=emb)
@commands.command()
async def resources(self, ctx):
emb, _ = await fetch_embed('resources', 'Resources')
await ctx.send(embed=emb)
def setup(bot):
bot.add_cog(Utility(bot))