示例#1
0
    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)

            # The initial embed which shows what you searched for
            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)
            # List which will contain the results
            results=[]
            # fetch results from codegrepper
            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 less than 1 result stored, send notFoundEmbed
            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)
            # if there is exactly 1 result, send directly instead of using pages
            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)
            # if more than or equal to 2 results, make multiple embeds and send a menu
            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
                    )
                    # add the embed to resultList
                    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
                )
                # page related stuff
                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
        # if no argument is passed
        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)
示例#2
0
文件: Info.py 项目: AmeyWale/SleepBot
    async def howtoask(self, ctx, page=0, target: discord.Member = None):
        embedcolour = random.randint(0, 0xffffff)
        helpEmbed = discord.Embed(
            title="How to ask",
            description=
            "Here is a short explanation on how you should ask a question efficiently:",
            colour=embedcolour)
        helpEmbed.set_thumbnail(
            url=
            "https://res.cloudinary.com/zeusabhijeet/image/upload/v1607099122/SleepBot/Info%20Commands/ask_question.png"
        )
        if target != None:
            helpEmbed.set_author(name=target, icon_url=target.avatar_url)
        else:
            helpEmbed.set_author(name=ctx.message.author,
                                 icon_url=ctx.message.author.avatar_url)
        helpEmbed.add_field(
            name="No Hello",
            value=
            "It's alright if you don't greet. You can directly ask your question right away; it saves your's and the other person's time. More on that [here](https://www.nohello.com/).",
        )
        helpEmbed1 = discord.Embed(
            title="Asking a Code Related Question",
            description=
            """To ask a question on a code, refrain from sending screenshots or photos of the code as they are usually barely visible.
			Here are two ways you can share your code:""",
            colour=embedcolour)
        helpEmbed1.set_thumbnail(
            url=
            'https://res.cloudinary.com/zeusabhijeet/image/upload/v1607100500/SleepBot/Info%20Commands/howtoask_code.png'
        )
        helpEmbed1.add_field(name="1. Use an Online Code Sharing Service",
                             value="""
				Use these if the code you are sharing is larger than 12 lines.
				Below are some of the code sharing services that you can use: 
				[GitHub Gist](https://gist.github.com/), [JSfiddle](https://jsfiddle.net/), [Codepen](https://codepen.io/),	
				[Pastebin](https://pastebin.com/), [OnlineGDB](https://www.onlinegdb.com/), [repl.it](https://repl.it/), etc""",
                             inline=False)
        helpEmbed1.add_field(
            name="2. Use a Code Snippet",
            value=
            """Use these when the code you are sharing is less than or about 12 lines.
				To make a code snippet, encase your code between a pair of 3 backticks
				
				\```
				Like This
				\```
				
				So when a code is sent, it will look something like this:
```py
# python3 Hello World Program
print("Hello World!")
```

				You can read more about how Discord's Markdown works [here](https://gist.github.com/matthewzring/9f7bbfd102003963f9be7dbcf7d40e51).""",
            inline=False)
        if target != None:
            helpEmbed1.set_author(name=target, icon_url=target.avatar_url)
        else:
            helpEmbed1.set_author(name=ctx.message.author,
                                  icon_url=ctx.message.author.avatar_url)
        if target != None:
            await ctx.send("<@!{}>".format(target.id))
        if page == 1:
            await ctx.send(embed=helpEmbed)
        elif page == 2:
            await ctx.send(embed=helpEmbed1)
        else:
            menu = PaginatedMenu(ctx)
            menu.add_pages([helpEmbed, helpEmbed1])
            menu.set_timeout(30)
            menu.show_command_message()
            menu.persist_on_close()
            menu.show_page_numbers()
            menu.allow_multisession()
            await menu.open()