This repository has been archived by the owner on Dec 12, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCodeHelp.py
153 lines (142 loc) · 5.86 KB
/
CodeHelp.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
"""
Contains command for fetching code from CodeGrepper
"""
import discord
import random
from discord.ext import commands
from dpymenus import Page, PaginatedMenu
import aiohttp
import typing
import json
class CodeHelp(commands.Cog):
def __init__(self,client):
self.client = client
@commands.command(name = 'ask',
aliases = ['get', 'getcode'],
help = 'Fetch code from CodeGrepper, Takes in No. of results (default 3) and the question.')
async def ask(self,ctx, result_limit: typing.Optional[int] = 3, *, term: str=None):
embedColour = random.randint(0, 0xffffff)
if term!=None:
googlequery=term
q=googlequery.replace(" ","+")
cq=googlequery.replace(" ","%20")
searchurl='https://www.google.com/search?q='+q
originurl='https://www.codegrepper.com/search.php?q='+cq
# print(searchurl,q)
startEmbed = discord.Embed(
title ="You asked",
description =f'{term} \n [source]({originurl})',
colour=embedColour
)
startEmbed.set_author(name=ctx.message.author,icon_url=ctx.message.author.avatar_url)
# print(term)
results=[]
async with aiohttp.ClientSession() as session:
async with session.get('https://www.codegrepper.com/api/search.php', params ={"q":term}) as r :
result = await r.json()
results=result['answers']
answerEmbed=discord.Embed(
title='Answers',
colour=embedColour
)
# print(len(results),'length')
# embed.set_footer(text=f'{ctx.message}')
print(len(results))
if len(results)<1:
notFoundEmbed=discord.Embed(
title="Answer Not Found",
description=f'''[Search yourself]({searchurl})
\nYou can also contribute to this by installing [codegrepper](https://www.codegrepper.com/) extension and marking an answer when you find it
''',
colour=embedColour
)
await ctx.send(embed=startEmbed)
await ctx.send(embed=notFoundEmbed)
pass
elif len(results)==1:
print(len(results))
await ctx.send(embed=startEmbed)
data=results
resultList = []
for i in range(len(data)):
# print(i)
# print(i['answer'])
if i >= result_limit :
break
j=data[i]
ans = j['answer']
lang =j['language']
source=" "
source=j['source_url']
print(source,"source")
answer=f'```{lang}\n {ans}```'
answerEmbed=discord.Embed(
# name="name",
description=answer,
colour=embedColour
)
notGotEmbed=discord.Embed(
title=":frowning2: Did Not Find Your Answer?",
description=f'''[Search yourself]({searchurl})
\nYou can also contribute to this by installing [codegrepper](https://www.codegrepper.com/) extension and marking an answer when you find it
''',
colour=embedColour
)
await ctx.send(embed=answerEmbed)
await ctx.send(embed=notGotEmbed)
elif len(results)>=2:
await ctx.send(embed=startEmbed)
data=results
resultList = []
for i in range(len(data)):
# print(i)
# print(i['answer'])
if i >= result_limit :
break
j=data[i]
ans = j['answer']
lang =j['language']
source=" "
source=j['source_url']
print(source,"source")
answer=f'```{lang}\n {ans}```'
answerEmbed=discord.Embed(
# name="name",
description=answer,
colour=embedColour
)
resultList.append(answerEmbed)
#await ctx.send(embed=answerEmbed)
notGotEmbed=discord.Embed(
title=":frowning2: Did Not Find Your Answer?",
description=f'''[Search yourself]({searchurl})
\nYou can also contribute to this by installing [codegrepper](https://www.codegrepper.com/) extension and marking an answer when you find it
''',
colour=embedColour
)
menu = PaginatedMenu(ctx)
menu.add_pages(resultList)
menu.set_timeout(30)
menu.show_command_message()
menu.persist_on_close()
menu.show_page_numbers()
menu.show_skip_buttons()
menu.allow_multisession()
await menu.open()
print('menu opened')
await ctx.send(embed=notGotEmbed)
else:
pass
else:
noargEmbed=discord.Embed(
title="Ask Something, it can't be blank",
description='''
something expected
`?ask what you want to ask`
''',
colour=embedColour
)
await ctx.send(embed=noargEmbed)
# await ctx.send(answer)
def setup(client):
client.add_cog(CodeHelp(client))