Fix behavior of is_owner for team applications and put all owner IDs in one attribute (#3793)

* blah

* you idiot

* Me likey Danny's way

* Add a warning when bot has no owner set
This commit is contained in:
jack1142 2020-05-29 00:03:23 +02:00 committed by GitHub
parent a9acb80132
commit 05ec73266c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 30 additions and 22 deletions

View File

@ -80,7 +80,6 @@ class RedBase(
self._shutdown_mode = ExitCodes.CRITICAL self._shutdown_mode = ExitCodes.CRITICAL
self._cli_flags = cli_flags self._cli_flags = cli_flags
self._config = Config.get_core_conf(force_registration=False) self._config = Config.get_core_conf(force_registration=False)
self._co_owners = cli_flags.co_owner
self.rpc_enabled = cli_flags.rpc self.rpc_enabled = cli_flags.rpc
self.rpc_port = cli_flags.rpc_port self.rpc_port = cli_flags.rpc_port
self._last_exception = None self._last_exception = None
@ -154,8 +153,16 @@ class RedBase(
if "command_prefix" not in kwargs: if "command_prefix" not in kwargs:
kwargs["command_prefix"] = prefix_manager kwargs["command_prefix"] = prefix_manager
if cli_flags.owner and "owner_id" not in kwargs: if "owner_id" in kwargs:
kwargs["owner_id"] = cli_flags.owner raise RuntimeError("Red doesn't accept owner_id kwarg, use owner_ids instead.")
self._owner_id_overwrite = cli_flags.owner
if "owner_ids" in kwargs:
kwargs["owner_ids"] = set(kwargs["owner_ids"])
else:
kwargs["owner_ids"] = set()
kwargs["owner_ids"].update(cli_flags.co_owner)
if "command_not_found" not in kwargs: if "command_not_found" not in kwargs:
kwargs["command_not_found"] = "Command {} not found.\n{}" kwargs["command_not_found"] = "Command {} not found.\n{}"
@ -538,8 +545,10 @@ class RedBase(
init_global_checks(self) init_global_checks(self)
init_events(self, cli_flags) init_events(self, cli_flags)
if self.owner_id is None: if self._owner_id_overwrite is None:
self.owner_id = await self._config.owner() self._owner_id_overwrite = await self._config.owner()
if self._owner_id_overwrite is not None:
self.owner_ids.add(self._owner_id_overwrite)
i18n_locale = await self._config.locale() i18n_locale = await self._config.locale()
i18n.set_locale(i18n_locale) i18n.set_locale(i18n_locale)
@ -709,23 +718,20 @@ class RedBase(
------- -------
bool bool
""" """
if user.id in self._co_owners: if user.id in self.owner_ids:
return True return True
ret = False ret = False
if not self._app_owners_fetched:
if self.owner_id:
return self.owner_id == user.id
elif self.owner_ids:
return user.id in self.owner_ids
elif not self._app_owners_fetched:
app = await self.application_info() app = await self.application_info()
if app.team: if app.team:
if self._use_team_features: if self._use_team_features:
self.owner_ids = ids = {m.id for m in app.team.members} ids = {m.id for m in app.team.members}
self.owner_ids.update(ids)
ret = user.id in ids ret = user.id in ids
else: elif self._owner_id_overwrite is None:
self.owner_id = owner_id = app.owner.id owner_id = app.owner.id
self.owner_ids.add(owner_id)
ret = user.id == owner_id ret = user.id == owner_id
self._app_owners_fetched = True self._app_owners_fetched = True
@ -1168,8 +1174,7 @@ class RedBase(
await self.wait_until_red_ready() await self.wait_until_red_ready()
destinations = [] destinations = []
opt_outs = await self._config.owner_opt_out_list() opt_outs = await self._config.owner_opt_out_list()
team_ids = () if not self._use_team_features else self.owner_ids for user_id in self.owner_ids:
for user_id in set((self.owner_id, *self._co_owners, *team_ids)):
if user_id not in opt_outs: if user_id not in opt_outs:
user = self.get_user(user_id) user = self.get_user(user_id)
if user and not user.bot: # user.bot is possible with flags and teams if user and not user.bot: # user.bot is possible with flags and teams

View File

@ -58,10 +58,9 @@ def init_events(bot, cli_flags):
if app_info.team: if app_info.team:
if bot._use_team_features: if bot._use_team_features:
bot.owner_ids = {m.id for m in app_info.team.members} bot.owner_ids.update(m.id for m in app_info.team.members)
else: elif bot._owner_id_overwrite is None:
if bot.owner_id is None: bot.owner_ids.add(app_info.owner.id)
bot.owner_id = app_info.owner.id
bot._app_owners_fetched = True bot._app_owners_fetched = True
try: try:
@ -184,6 +183,10 @@ def init_events(bot, cli_flags):
if invite_url: if invite_url:
print("\nInvite URL: {}\n".format(invite_url)) print("\nInvite URL: {}\n".format(invite_url))
if not bot.owner_ids:
# we could possibly exit here in future
log.warning("Bot doesn't have any owner set!")
bot._color = discord.Colour(await bot._config.color()) bot._color = discord.Colour(await bot._config.color())
bot._red_ready.set() bot._red_ready.set()
if outdated_red_message: if outdated_red_message:

View File

@ -171,7 +171,7 @@ def red(config_fr):
Config.get_core_conf = lambda *args, **kwargs: config_fr Config.get_core_conf = lambda *args, **kwargs: config_fr
red = Red(cli_flags=cli_flags, description=description, dm_help=None, owner_id=None) red = Red(cli_flags=cli_flags, description=description, dm_help=None, owner_ids=set())
yield red yield red