diff --git a/discord-bot/.env.example b/discord-bot/.env.example index 4fcb23b3..5cd18fac 100644 --- a/discord-bot/.env.example +++ b/discord-bot/.env.example @@ -1,4 +1,4 @@ -TOKEN= +BOT_TOKEN= DECLARE_GLOBAL_COMMANDS= OWNER_IDS=[, ] PREFIX="./" diff --git a/discord-bot/bot/bot.py b/discord-bot/bot/bot.py index 4e3bd12c..a305946f 100644 --- a/discord-bot/bot/bot.py +++ b/discord-bot/bot/bot.py @@ -9,8 +9,9 @@ from bot.settings import Settings settings = Settings() +# TODO: Revisit cache settings bot = lightbulb.BotApp( - token=settings.token, + token=settings.bot_token, logs="DEBUG", prefix=settings.prefix, default_enabled_guilds=settings.declare_global_commands, diff --git a/discord-bot/bot/extensions/guild_settings.py b/discord-bot/bot/extensions/guild_settings.py index f5785b8d..1aba9f47 100644 --- a/discord-bot/bot/extensions/guild_settings.py +++ b/discord-bot/bot/extensions/guild_settings.py @@ -5,6 +5,7 @@ import lightbulb from aiosqlite import Connection from bot.db.schemas import GuildSettings from bot.utils import mention +from lightbulb.utils.permissions import permissions_in from loguru import logger plugin = lightbulb.Plugin("GuildSettings") @@ -62,6 +63,16 @@ async def log_channel(ctx: lightbulb.SlashContext) -> None: channel: hikari.TextableGuildChannel = ctx.options.channel conn: Connection = ctx.bot.d.db assert ctx.guild_id is not None # `guild_only` check + assert isinstance(channel, hikari.PermissibleGuildChannel) + + # Check if the bot can send messages in that channel + assert (me := ctx.bot.get_me()) is not None # non-None after `StartedEvent` + if (own_member := ctx.bot.cache.get_member(ctx.guild_id, me.id)) is None: + own_member = await ctx.bot.rest.fetch_member(ctx.guild_id, me.id) + perms = permissions_in(channel, own_member) + if perms & ~hikari.Permissions.SEND_MESSAGES: + await ctx.respond("I don't have permission to send messages in that channel.") + return await ctx.respond(f"Setting `log_channel` to {channel.mention}.") diff --git a/discord-bot/bot/settings.py b/discord-bot/bot/settings.py index 200ab54b..136c2b22 100644 --- a/discord-bot/bot/settings.py +++ b/discord-bot/bot/settings.py @@ -6,7 +6,7 @@ from pydantic import BaseSettings, Field class Settings(BaseSettings): """Settings for the bot.""" - token: str = Field(env="TOKEN", default="") + bot_token: str = Field(env="BOT_TOKEN", default="") declare_global_commands: int = Field(env="DECLARE_GLOBAL_COMMANDS", default=0) owner_ids: list[int] = Field(env="OWNER_IDS", default_factory=list) prefix: str = Field(env="PREFIX", default="./")