Rename bot.db as bot._config (#2967)

* Rename `bot.db` as `bot._config`

  - Continues work towards strong version guarantees
  - Added methods for cog use for a few things which were previously
  only accessible via direct access.
  - Retained private use in a few internal use locations, though most
  methods were updated away from this.
  - Updated documentation for shared api token users

* changelog

* more detail

* docstring fixes

* Apparently, I forgot to commit something I had locally

  - + a copy/paste failue in the changelog

* *sigh*:

* *sigh*
This commit is contained in:
Michael H
2019-09-01 15:42:28 -04:00
committed by GitHub
parent d86cc7a854
commit 6075c5bde0
17 changed files with 321 additions and 227 deletions

View File

@@ -296,7 +296,7 @@ class Audio(commands.Cog):
else:
dur = lavalink.utils.format_time(player.current.length)
embed = discord.Embed(
colour=(await self._get_embed_colour(notify_channel)),
colour=(await self.bot.get_embed_color(notify_channel)),
title=_("Now Playing"),
description=description,
)
@@ -328,7 +328,8 @@ class Audio(commands.Cog):
if notify_channel:
notify_channel = self.bot.get_channel(notify_channel)
embed = discord.Embed(
colour=(await self._get_embed_colour(notify_channel)), title=_("Queue ended.")
colour=(await self.bot.get_embed_colour(notify_channel)),
title=_("Queue ended."),
)
await notify_channel.send(embed=embed)
@@ -346,7 +347,7 @@ class Audio(commands.Cog):
if message_channel:
message_channel = self.bot.get_channel(message_channel)
embed = discord.Embed(
colour=(await self._get_embed_colour(message_channel)),
colour=(await self.bot.get_embed_color(message_channel)),
title=_("Track Error"),
description="{}\n**[{}]({})**".format(
extra, player.current.title, player.current.uri
@@ -3834,14 +3835,12 @@ class Audio(commands.Cog):
return False
async def _check_api_tokens(self):
spotify = await self.bot.db.api_tokens.get_raw(
"spotify", default={"client_id": "", "client_secret": ""}
)
youtube = await self.bot.db.api_tokens.get_raw("youtube", default={"api_key": ""})
spotify = await self.bot.get_shared_api_tokens("spotify")
youtube = await self.bot.get_shared_api_tokens("youtube")
return {
"spotify_client_id": spotify["client_id"],
"spotify_client_secret": spotify["client_secret"],
"youtube_api": youtube["api_key"],
"spotify_client_id": spotify.get("client_id", ""),
"spotify_client_secret": spotify.get("client_secret", ""),
"youtube_api": youtube.get("api_key", ""),
}
async def _check_external(self):
@@ -4081,13 +4080,6 @@ class Audio(commands.Cog):
except discord.errors.NotFound:
pass
async def _get_embed_colour(self, channel: discord.abc.GuildChannel):
# Unfortunately we need this for when context is unavailable.
if await self.bot.db.guild(channel.guild).use_bot_color():
return channel.guild.me.color
else:
return self.bot.color
async def _get_eq_reaction(self, ctx, message, emoji):
try:
reaction, user = await self.bot.wait_for(
@@ -4323,10 +4315,9 @@ class Audio(commands.Cog):
return {"Authorization": "Basic %s" % auth_header.decode("ascii")}
async def _request_token(self):
self.client_id = await self.bot.db.api_tokens.get_raw("spotify", default={"client_id": ""})
self.client_secret = await self.bot.db.api_tokens.get_raw(
"spotify", default={"client_secret": ""}
)
tokens = await self.bot.get_shared_api_tokens("spotify")
self.client_id = tokens.get("client_id", "")
self.client_secret = tokens.get("client_secret", "")
payload = {"grant_type": "client_credentials"}
headers = self._make_token_auth(
self.client_id["client_id"], self.client_secret["client_secret"]

View File

@@ -47,9 +47,9 @@ def check_global_setting_admin():
return True
if ctx.channel.permissions_for(author).manage_guild:
return True
admin_roles = set(await ctx.bot.db.guild(ctx.guild).admin_role())
admin_role_ids = await ctx.bot.get_admin_role_ids(ctx.guild.id)
for role in author.roles:
if role.id in admin_roles:
if role.id in admin_role_ids:
return True
else:
return await ctx.bot.is_owner(author)

View File

@@ -28,8 +28,9 @@ class Image(commands.Cog):
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})
if imgur_token is not None:
if not await self.bot.get_shared_api_tokens("imgur"):
await self.bot.set_shared_api_tokens(client_id=imgur_token)
await self.settings.imgur_client_id.clear()
@commands.group(name="imgur")
@@ -48,7 +49,7 @@ class Image(commands.Cog):
"""
url = self.imgur_base_url + "gallery/search/time/all/0"
params = {"q": term}
imgur_client_id = await ctx.bot.db.api_tokens.get_raw("imgur", default=None)
imgur_client_id = (await ctx.bot.get_shared_api_tokens("imgur")).get("client_id")
if not imgur_client_id:
await ctx.send(
_(
@@ -56,7 +57,7 @@ class Image(commands.Cog):
).format(prefix=ctx.prefix)
)
return
headers = {"Authorization": "Client-ID {}".format(imgur_client_id["client_id"])}
headers = {"Authorization": "Client-ID {}".format(imgur_client_id)}
async with self.session.get(url, headers=headers, params=params) as search_get:
data = await search_get.json()
@@ -101,7 +102,7 @@ class Image(commands.Cog):
await ctx.send_help()
return
imgur_client_id = await ctx.bot.db.api_tokens.get_raw("imgur", default=None)
imgur_client_id = (await ctx.bot.get_shared_api_tokens("imgur")).get("client_id")
if not imgur_client_id:
await ctx.send(
_(
@@ -111,7 +112,7 @@ class Image(commands.Cog):
return
links = []
headers = {"Authorization": "Client-ID {}".format(imgur_client_id["client_id"])}
headers = {"Authorization": "Client-ID {}".format(imgur_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:
@@ -164,7 +165,7 @@ class Image(commands.Cog):
await ctx.send_help()
return
giphy_api_key = await ctx.bot.db.api_tokens.get_raw("GIPHY", default=None)
giphy_api_key = (await ctx.bot.get_shared_api_tokens("GIPHY")).get("api_key")
if not giphy_api_key:
await ctx.send(
_("An API key has not been set! Please set one with `{prefix}giphycreds`.").format(
@@ -174,7 +175,7 @@ class Image(commands.Cog):
return
url = "http://api.giphy.com/v1/gifs/search?&api_key={}&q={}".format(
giphy_api_key["api_key"], keywords
giphy_api_key, keywords
)
async with self.session.get(url) as r:
@@ -197,7 +198,7 @@ class Image(commands.Cog):
await ctx.send_help()
return
giphy_api_key = await ctx.bot.db.api_tokens.get_raw("GIPHY", default=None)
giphy_api_key = (await ctx.bot.get_shared_api_tokens("GIPHY")).get("api_key")
if not giphy_api_key:
await ctx.send(
_("An API key has not been set! Please set one with `{prefix}giphycreds`.").format(
@@ -207,7 +208,7 @@ class Image(commands.Cog):
return
url = "http://api.giphy.com/v1/gifs/random?&api_key={}&tag={}".format(
giphy_api_key["api_key"], keywords
giphy_api_key, keywords
)
async with self.session.get(url) as r:

View File

@@ -82,27 +82,27 @@ class Streams(commands.Cog):
async def move_api_keys(self):
"""Move the API keys from cog stored config to core bot config if they exist."""
tokens = await self.db.tokens()
youtube = await self.bot.db.api_tokens.get_raw("youtube", default={})
twitch = await self.bot.db.api_tokens.get_raw("twitch", default={})
youtube = await self.bot.get_shared_api_tokens("youtube")
twitch = await self.bot.get_shared_api_tokens("twitch")
for token_type, token in tokens.items():
if token_type == "YoutubeStream" and "api_key" not in youtube:
await self.bot.db.api_tokens.set_raw("youtube", value={"api_key": token})
await self.bot.set_shared_api_tokens("youtube", api_key=token)
if token_type == "TwitchStream" and "client_id" not in twitch:
# Don't need to check Community since they're set the same
await self.bot.db.api_tokens.set_raw("twitch", value={"client_id": token})
await self.bot.set_shared_api_tokens("twitch", client_id=token)
await self.db.tokens.clear()
@commands.command()
async def twitchstream(self, ctx: commands.Context, channel_name: str):
"""Check if a Twitch channel is live."""
token = await self.bot.db.api_tokens.get_raw("twitch", default={"client_id": None})
token = (await self.bot.get_shared_api_tokens("twitch")).get("client_id")
stream = TwitchStream(name=channel_name, token=token)
await self.check_online(ctx, stream)
@commands.command()
async def youtubestream(self, ctx: commands.Context, channel_id_or_name: str):
"""Check if a YouTube channel is live."""
apikey = await self.bot.db.api_tokens.get_raw("youtube", default={"api_key": None})
apikey = await self.bot.get_shared_api_tokens("youtube")
is_name = self.check_name_or_id(channel_id_or_name)
if is_name:
stream = YoutubeStream(name=channel_id_or_name, token=apikey)
@@ -273,7 +273,7 @@ class Streams(commands.Cog):
async def stream_alert(self, ctx: commands.Context, _class, channel_name):
stream = self.get_stream(_class, channel_name)
if not stream:
token = await self.bot.db.api_tokens.get_raw(_class.token_name, default=None)
token = await self.bot.get_shared_api_tokens(_class.token_name)
is_yt = _class.__name__ == "YoutubeStream"
if is_yt and not self.check_name_or_id(channel_name):
stream = _class(id=channel_name, token=token)
@@ -651,8 +651,8 @@ class Streams(commands.Cog):
pass
else:
raw_stream["_messages_cache"].append(msg)
token = await self.bot.db.api_tokens.get_raw(_class.token_name, default=None)
if token is not None:
token = await self.bot.get_shared_api_tokens(_class.token_name)
if token:
raw_stream["token"] = token
streams.append(_class(**raw_stream))