mirror of
https://github.com/Cog-Creators/Red-DiscordBot.git
synced 2025-11-21 10:17:59 -05:00
Allow central storage of API keys (#2389)
This creates a central location to store external API tokens that can be used between cogs without requiring each cog to be loaded for it to work. A new set option for `[p]set api` is created to assist in forming bot readable API token locations. This also updates the Streams cog to utilize the central database. Tokens are moved from the old data locations in core cogs on load.
This commit is contained in:
committed by
Toby Harradine
parent
722aaa225b
commit
3f1d416526
@@ -27,6 +27,13 @@ class Image(commands.Cog):
|
||||
def __unload(self):
|
||||
self.session.detach()
|
||||
|
||||
async def initialize(self) -> None:
|
||||
"""Move the API keys from cog stored config to core bot config if they exist."""
|
||||
imgur_token = await self.settings.imgur_client_id()
|
||||
if imgur_token is not None and "imgur" not in await self.bot.db.api_tokens():
|
||||
await self.bot.db.api_tokens.set_raw("imgur", value={"client_id": imgur_token})
|
||||
await self.settings.imgur_client_id.clear()
|
||||
|
||||
@commands.group(name="imgur")
|
||||
async def _imgur(self, ctx):
|
||||
"""Retrieve pictures from Imgur.
|
||||
@@ -43,7 +50,7 @@ class Image(commands.Cog):
|
||||
"""
|
||||
url = self.imgur_base_url + "gallery/search/time/all/0"
|
||||
params = {"q": term}
|
||||
imgur_client_id = await self.settings.imgur_client_id()
|
||||
imgur_client_id = await ctx.bot.db.api_tokens.get_raw("imgur", default=None)
|
||||
if not imgur_client_id:
|
||||
await ctx.send(
|
||||
_(
|
||||
@@ -51,7 +58,7 @@ class Image(commands.Cog):
|
||||
).format(prefix=ctx.prefix)
|
||||
)
|
||||
return
|
||||
headers = {"Authorization": "Client-ID {}".format(imgur_client_id)}
|
||||
headers = {"Authorization": "Client-ID {}".format(imgur_client_id["client_id"])}
|
||||
async with self.session.get(url, headers=headers, params=params) as search_get:
|
||||
data = await search_get.json()
|
||||
|
||||
@@ -96,7 +103,7 @@ class Image(commands.Cog):
|
||||
await ctx.send_help()
|
||||
return
|
||||
|
||||
imgur_client_id = await self.settings.imgur_client_id()
|
||||
imgur_client_id = await ctx.bot.db.api_tokens.get_raw("imgur", default=None)
|
||||
if not imgur_client_id:
|
||||
await ctx.send(
|
||||
_(
|
||||
@@ -106,7 +113,7 @@ class Image(commands.Cog):
|
||||
return
|
||||
|
||||
links = []
|
||||
headers = {"Authorization": "Client-ID {}".format(imgur_client_id)}
|
||||
headers = {"Authorization": "Client-ID {}".format(imgur_client_id["client_id"])}
|
||||
url = self.imgur_base_url + "gallery/r/{}/{}/{}/0".format(subreddit, sort, window)
|
||||
|
||||
async with self.session.get(url, headers=headers) as sub_get:
|
||||
@@ -130,22 +137,24 @@ class Image(commands.Cog):
|
||||
|
||||
@checks.is_owner()
|
||||
@commands.command()
|
||||
async def imgurcreds(self, ctx, imgur_client_id: str):
|
||||
"""Set the Imgur Client ID.
|
||||
async def imgurcreds(self, ctx):
|
||||
"""Explain how to set imgur API tokens"""
|
||||
|
||||
To get an Imgur Client ID:
|
||||
1. Login to an Imgur account.
|
||||
2. Visit [this](https://api.imgur.com/oauth2/addclient) page
|
||||
3. Enter a name for your application
|
||||
4. Select *Anonymous usage without user authorization* for the auth type
|
||||
5. Set the authorization callback URL to `https://localhost`
|
||||
6. Leave the app website blank
|
||||
7. Enter a valid email address and a description
|
||||
8. Check the captcha box and click next
|
||||
9. Your Client ID will be on the next page.
|
||||
"""
|
||||
await self.settings.imgur_client_id.set(imgur_client_id)
|
||||
await ctx.send(_("The Imgur Client ID has been set!"))
|
||||
message = _(
|
||||
"To get an Imgur Client ID:\n"
|
||||
"1. Login to an Imgur account.\n"
|
||||
"2. Visit [this](https://api.imgur.com/oauth2/addclient) page\n"
|
||||
"3. Enter a name for your application\n"
|
||||
"4. Select *Anonymous usage without user authorization* for the auth type\n"
|
||||
"5. Set the authorization callback URL to `https://localhost`\n"
|
||||
"6. Leave the app website blank\n"
|
||||
"7. Enter a valid email address and a description\n"
|
||||
"8. Check the captcha box and click next\n"
|
||||
"9. Your Client ID will be on the next page.\n"
|
||||
"10. do `{prefix}set api imgur client_id,your_client_id`\n"
|
||||
).format(prefix=ctx.prefix)
|
||||
|
||||
await ctx.maybe_send_embed(message)
|
||||
|
||||
@commands.guild_only()
|
||||
@commands.command()
|
||||
|
||||
Reference in New Issue
Block a user