Skip to content

Commit 95c7d18

Browse files
Add ownership control of the bot via a config key (#19)
* Fix dockerfile still looking for bot.py * add ownership control via config key, and black and isort
1 parent 458068f commit 95c7d18

File tree

8 files changed

+57
-11
lines changed

8 files changed

+57
-11
lines changed

config.template.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
prefix = '>>'
2+
owner_ids = [123, 456, 789] # user or role ids, optional
3+
24
[TOKENS]
35
bot = ''
46
idevision = ''

constants/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,5 @@
2222
"""
2323
from .constants import *
2424

25+
2526
GUILD_ID: int = 490948346773635102

constants/constants.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
"""
2323
from ._meta import CONSTANTS
2424

25+
2526
__all__ = (
2627
"Roles",
2728
"Colours",

core/utils/formatters.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
"""
2323
from discord.utils import escape_markdown
2424

25+
2526
__all__ = ("to_codeblock",)
2627

2728

modules/__init__.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import pathlib
22
from pkgutil import ModuleInfo, iter_modules
33

4+
45
_ext: list[ModuleInfo] = []
56
_ext.extend(
67
[
@@ -15,9 +16,7 @@
1516
_ext.extend(
1617
[
1718
module
18-
for module in iter_modules(
19-
[str(private_path.absolute())], prefix=__package__ + ".private."
20-
)
19+
for module in iter_modules([str(private_path.absolute())], prefix=__package__ + ".private.")
2120
if not module.name.rsplit(".")[-1].startswith("_")
2221
]
2322
)

modules/admin.py

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,53 @@
2020
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
2121
SOFTWARE.
2222
"""
23+
import logging
24+
25+
from discord.ext import commands
26+
2327
import core
28+
from constants import GUILD_ID
29+
from core.context import Context
30+
31+
32+
LOGGER = logging.getLogger(__name__)
33+
34+
35+
class Administration(commands.Cog):
36+
def __init__(self, bot: core.Bot, /) -> None:
37+
self.bot: core.Bot = bot
38+
if owners := core.CONFIG.get("owner_ids"):
39+
LOGGER.info("[Ownership] Setting new owners to:\n%s", ", ".join([str(o) for o in owners]))
40+
bot.loop.create_task(self.populate_owners(owners))
41+
42+
async def cog_check(self, ctx: Context) -> bool: # type: ignore # maybecoro override woes
43+
return await self.bot.is_owner(ctx.author)
44+
45+
async def populate_owners(self, owner_ids: list[int]) -> None:
46+
await self.bot.wait_until_ready()
47+
48+
new_owners: list[int] = []
49+
for id_ in owner_ids:
50+
user = self.bot.get_user(id_)
51+
if user:
52+
LOGGER.info("[Ownership] New User found for owner: %s (%s)", str(user), user.id)
53+
new_owners.append(id_)
54+
continue
55+
56+
guild = self.bot.get_guild(GUILD_ID)
57+
if not guild:
58+
continue
59+
60+
role = guild.get_role(id_)
61+
if role:
62+
LOGGER.info("[Ownership] New Role found for owner: %s (%s)", str(role), role.id)
63+
new_owners += [m.id for m in role.members]
64+
continue
65+
66+
if new_owners:
67+
self.bot.owner_id = None
68+
self.bot.owner_ids = set(new_owners)
2469

2570

2671
async def setup(bot: core.Bot) -> None:
27-
pass
72+
await bot.add_cog(Administration(bot))

modules/github.py

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -46,18 +46,16 @@
4646

4747

4848
class GitHub(core.Cog):
49-
5049
def __init__(self, bot: core.Bot) -> None:
5150
self.bot = bot
5251
self.code_highlight_emoji = "📃"
5352
self.highlight_timeout = 10
5453

5554
def _strip_content_path(self, url: str) -> str:
56-
file_path = url[len(GITHUB_BASE_URL):]
55+
file_path = url[len(GITHUB_BASE_URL) :]
5756
return file_path
5857

5958
async def format_highlight_block(self, url: str, line_adjustment: int = 10) -> dict[str, str | int] | None:
60-
6159
try:
6260
highlighted_line = int(url.split("#L")[1]) # separate the #L{n} highlight
6361
except IndexError:
@@ -106,7 +104,7 @@ async def format_highlight_block(self, url: str, line_adjustment: int = 10) -> d
106104

107105
else:
108106
# if we hit the end of the file, just write an empty string
109-
display_str = ("{} {}\n" if line_list.get(key) is not None else "")
107+
display_str = "{} {}\n" if line_list.get(key) is not None else ""
110108
msg += display_str.format(curr_line_no, line_list.get(key))
111109

112110
key += 1
@@ -149,9 +147,7 @@ async def on_message(self, message: discord.Message) -> None:
149147

150148
def check(reaction: discord.Reaction, user: discord.User) -> bool:
151149
return (
152-
reaction.emoji == self.code_highlight_emoji and
153-
user != self.bot.user and
154-
message.id == reaction.message.id
150+
reaction.emoji == self.code_highlight_emoji and user != self.bot.user and message.id == reaction.message.id
155151
)
156152

157153
try:

types_/config.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ class Snekbox(TypedDict):
2626

2727
class Config(TypedDict):
2828
prefix: str
29+
owner_ids: NotRequired[list[int]]
2930
TOKENS: Tokens
3031
DATABASE: Database
3132
LOGGING: Logging

0 commit comments

Comments
 (0)