diff options
| author | Ay355 <[email protected]> | 2021-07-22 07:02:42 -0700 |
|---|---|---|
| committer | GitHub <[email protected]> | 2021-07-22 10:02:42 -0400 |
| commit | fc51736b34df3c37c06781ae948b18efb71616fa (patch) | |
| tree | 4e4e485b8085e6ef1ad7c166a1b578dfc8d212de /examples | |
| parent | Fix incorrect typehint in send_message (diff) | |
| download | discord.py-fc51736b34df3c37c06781ae948b18efb71616fa.tar.xz discord.py-fc51736b34df3c37c06781ae948b18efb71616fa.zip | |
Add a new view example for link buttons
Diffstat (limited to 'examples')
| -rw-r--r-- | examples/views/link.py | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/examples/views/link.py b/examples/views/link.py new file mode 100644 index 00000000..2516d538 --- /dev/null +++ b/examples/views/link.py @@ -0,0 +1,39 @@ +from discord.ext import commands + +import discord +from urllib.parse import quote_plus + +class GoogleBot(commands.Bot): + def __init__(self): + super().__init__(command_prefix=commands.when_mentioned_or('$')) + + async def on_ready(self): + print(f'Logged in as {self.user} (ID: {self.user.id})') + print('------') + + +# Define a simple View that gives us a google link button. +# We take in `query` as the query that the command author requests for +class Google(discord.ui.View): + def __init__(self, query: str): + super().__init__() + # we need to quote the query string to make a valid url. Discord will raise an error if it isn't valid. + query = quote_plus(query) + url = f'https://www.google.com/search?q={query}' + + # Link buttons cannot be made with the decorator + # Therefore we have to manually create one. + # We add the quoted url to the button, and add the button to the view. + self.add_item(discord.ui.Button(label='Click Here', url=url)) + + +bot = GoogleBot() + + +async def google(ctx: commands.Context, *, query: str): + """Returns a google link for a query""" + await ctx.send(f'Google Result for: `{query}`', view=Google(query)) + + +bot.run('token') |