Skip to content

Commit

Permalink
Use correct prefixes when sending messages to owners (#3632)
Browse files Browse the repository at this point in the history
* use correct prefixes when sending messages to owners

* make internal util for this instead

* oops

* fix circular import

* let's add back the actual fix I made this PR for...

* fix wrong logger name

* improve log message
  • Loading branch information
Jackenmen authored Mar 12, 2020
1 parent 3ff127d commit 4afe1ff
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 31 deletions.
10 changes: 5 additions & 5 deletions redbot/cogs/mod/mod.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import asyncio
import logging
import re
from abc import ABC
from collections import defaultdict
Expand All @@ -8,6 +9,7 @@
from redbot.core import Config, modlog, commands
from redbot.core.bot import Red
from redbot.core.i18n import Translator, cog_i18n
from redbot.core.utils._internal_utils import send_to_owners_with_prefix_replaced
from .casetypes import CASETYPES
from .events import Events
from .kickban import KickBanMixin
Expand Down Expand Up @@ -104,14 +106,12 @@ async def _maybe_update_config(self):
await self.settings.guild(discord.Object(id=guild_id)).delete_repeats.set(val)
await self.settings.version.set("1.0.0") # set version of last update
if await self.settings.version() < "1.1.0":
prefixes = await self.bot.get_valid_prefixes()
prefix = re.sub(rf"<@!?{self.bot.user.id}>", f"@{self.bot.user.name}", prefixes[0])
msg = _(
"Ignored guilds and channels have been moved. "
"Please use `{prefix}moveignoredchannels` if "
"Please use `[p]moveignoredchannels` if "
"you were previously using these functions."
).format(prefix=prefix)
self.bot.loop.create_task(self.bot.send_to_owners(msg))
)
self.bot.loop.create_task(send_to_owners_with_prefix_replaced(self.bot, msg))
await self.settings.version.set(__version__)

@commands.command()
Expand Down
9 changes: 4 additions & 5 deletions redbot/cogs/streams/streams.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from redbot.core.bot import Red
from redbot.core import checks, commands, Config
from redbot.core.i18n import cog_i18n, Translator
from redbot.core.utils._internal_utils import send_to_owners_with_prefix_replaced
from redbot.core.utils.chat_formatting import escape, pagify

from .streamtypes import (
Expand Down Expand Up @@ -111,21 +112,19 @@ async def get_twitch_bearer_token(self) -> None:
try:
tokens["client_secret"]
except KeyError:
prefixes = await self.bot.get_valid_prefixes()
prefix = re.sub(rf"<@!?{self.bot.user.id}>", f"@{self.bot.user.name}", prefixes[0])
message = _(
"You need a client secret key to use correctly Twitch API on this cog.\n"
"Follow these steps:\n"
"1. Go to this page: https://dev.twitch.tv/console/apps.\n"
'2. Click "Manage" on your application.\n'
'3. Click on "New secret".\n'
"5. Copy your client ID and your client secret into:\n"
"`{prefix}set api twitch client_id <your_client_id_here> "
"`[p]set api twitch client_id <your_client_id_here> "
"client_secret <your_client_secret_here>`\n\n"
"Note: These tokens are sensitive and should only be used in a private channel "
"or in DM with the bot."
).format(prefix=prefix)
await self.bot.send_to_owners(message)
)
await send_to_owners_with_prefix_replaced(self.bot, message)
async with aiohttp.ClientSession() as session:
async with session.post(
"https://id.twitch.tv/oauth2/token",
Expand Down
34 changes: 14 additions & 20 deletions redbot/core/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,12 @@

from .rpc import RPCMixin
from .utils import common_filters
from .utils._internal_utils import send_to_owners_with_prefix_replaced

CUSTOM_GROUPS = "CUSTOM_GROUPS"
SHARED_API_TOKENS = "SHARED_API_TOKENS"

log = logging.getLogger("redbot")
log = logging.getLogger("red")

__all__ = ["RedBase", "Red", "ExitCodes"]

Expand Down Expand Up @@ -548,18 +549,6 @@ async def pre_flight(self, cli_flags):

last_system_info = await self._config.last_system_info()

async def notify_owners(content: str) -> None:
destinations = await self.get_owner_notification_destinations()
for destination in destinations:
prefixes = await self.get_valid_prefixes(getattr(destination, "guild", None))
prefix = re.sub(rf"<@!?{self.bot.user.id}>", f"@{self.bot.user.name}", prefixes[0])
try:
await destination.send(content.format(prefix=prefix))
except Exception as _exc:
log.exception(
f"I could not send an owner notification to ({destination.id}){destination}"
)

ver_info = list(sys.version_info[:2])
python_version_changed = False
LIB_PATH = cog_data_path(raw_name="Downloader") / "lib"
Expand All @@ -569,13 +558,14 @@ async def notify_owners(content: str) -> None:
shutil.rmtree(str(LIB_PATH))
LIB_PATH.mkdir()
self.loop.create_task(
notify_owners(
send_to_owners_with_prefix_replaced(
self,
"We detected a change in minor Python version"
" and cleared packages in lib folder.\n"
"The instance was started with no cogs, please load Downloader"
" and use `{prefix}cog reinstallreqs` to regenerate lib folder."
" and use `[p]cog reinstallreqs` to regenerate lib folder."
" After that, restart the bot to get"
" all of your previously loaded cogs loaded again."
" all of your previously loaded cogs loaded again.",
)
)
python_version_changed = True
Expand Down Expand Up @@ -603,11 +593,12 @@ async def notify_owners(content: str) -> None:

if system_changed and not python_version_changed:
self.loop.create_task(
notify_owners(
send_to_owners_with_prefix_replaced(
self,
"We detected a possible change in machine's operating system"
" or architecture. You might need to regenerate your lib folder"
" if 3rd-party cogs stop working properly.\n"
"To regenerate lib folder, load Downloader and use `{prefix}cog reinstallreqs`."
"To regenerate lib folder, load Downloader and use `[p]cog reinstallreqs`.",
)
)

Expand Down Expand Up @@ -1202,8 +1193,11 @@ async def wrapped_send(location, content=None, **kwargs):
try:
await location.send(content, **kwargs)
except Exception as _exc:
log.exception(
f"I could not send an owner notification to ({location.id}){location}"
log.error(
"I could not send an owner notification to %s (%s)",
location,
location.id,
exc_info=_exc,
)

sends = [wrapped_send(d, content, **kwargs) for d in destinations]
Expand Down
67 changes: 66 additions & 1 deletion redbot/core/utils/_internal_utils.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
from __future__ import annotations

import asyncio
import json
import logging
import os
import re
import shutil
import tarfile
from datetime import datetime
from pathlib import Path
from typing import List, Optional, Set, Union, TYPE_CHECKING
from typing import Awaitable, Callable, List, Optional, Set, Union, TYPE_CHECKING

import discord
from fuzzywuzzy import fuzz, process
Expand All @@ -16,8 +18,11 @@
from redbot.core.utils.chat_formatting import box

if TYPE_CHECKING:
from redbot.core.bot import Red
from redbot.core.commands import Command, Context

main_log = logging.getLogger("red")

__all__ = ("safe_delete", "fuzzy_command_search", "format_fuzzy_results", "create_backup")


Expand Down Expand Up @@ -200,3 +205,63 @@ async def create_backup(dest: Path = Path.home()) -> Optional[Path]:
for f in to_backup:
tar.add(str(f), arcname=str(f.relative_to(data_path)), recursive=False)
return backup_fpath


# this might be worth moving to `bot.send_to_owners` at later date


async def send_to_owners_with_preprocessor(
bot: Red,
content: str,
*,
content_preprocessor: Optional[
Callable[[Red, discord.abc.Messageable, str], Awaitable[str]]
] = None,
**kwargs,
):
"""
This sends something to all owners and their configured extra destinations.
This acts the same as `Red.send_to_owners`, with
one added keyword argument as detailed below in *Other Parameters*.
Other Parameters
----------------
content_preprocessor: Optional[Callable[[Red, discord.abc.Messageable, str], Awaitable[str]]]
Optional async function that takes
bot object, owner notification destination and message content
and returns the content that should be sent to given location.
"""
destinations = await bot.get_owner_notification_destinations()

async def wrapped_send(bot, location, content=None, preprocessor=None, **kwargs):
try:
if preprocessor is not None:
content = await preprocessor(bot, location, content)
await location.send(content, **kwargs)
except Exception as _exc:
main_log.error(
"I could not send an owner notification to %s (%s)",
location,
location.id,
exc_info=_exc,
)

sends = [wrapped_send(bot, d, content, content_preprocessor, **kwargs) for d in destinations]
await asyncio.gather(*sends)


async def send_to_owners_with_prefix_replaced(bot: Red, content: str, **kwargs):
"""
This sends something to all owners and their configured extra destinations.
This acts the same as `Red.send_to_owners`, with one addition - `[p]` in ``content`` argument
is replaced with a clean prefix for each specific destination.
"""

async def preprocessor(bot: Red, destination: discord.abc.Messageable, content: str) -> str:
prefixes = await bot.get_valid_prefixes(getattr(destination, "guild", None))
prefix = re.sub(rf"<@!?{bot.user.id}>", f"@{bot.user.name}", prefixes[0])
return content.replace("[p]", prefix)

await send_to_owners_with_preprocessor(bot, content, content_preprocessor=preprocessor)

0 comments on commit 4afe1ff

Please sign in to comment.