Skip to content

Discord client

Bases: Bot

Main Discord bot client for the Werewolf game.

This class is responsible for initializing the bot, wiring core components (registry, controller, gateways), and orchestrating the startup lifecycle (cogs and commands; the gateway events live in the GuildEvents cog).

Source code in sources/discord_api/client.py
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
class DiscordClient(commands.Bot):
    """Main Discord bot client for the Werewolf game.

    This class is responsible for initializing the bot, wiring core components
    (registry, controller, gateways), and orchestrating the startup lifecycle
    (cogs and commands; the gateway events live in the ``GuildEvents`` cog).
    """

    def __init__(
        self,
        *,
        sync_commands: bool = True,
        commands_guild_id: int | None = None,
        settings: GameSettings | None = None,
    ) -> None:
        """Initialize the Discord client and core dependencies.

        Sets up Discord intents, uses mentions as the text command prefix
        (the bot relies on slash commands), and instantiates the main
        application components.

        Args:
            sync_commands: Push the slash commands to Discord at startup.
                Syncing is rate limited, skip it when the commands did not
                change.
            commands_guild_id: Sync the slash commands on this guild only,
                where they are available at once (development), instead of
                globally where they take a while to propagate.
            settings: How the games are run, the production rules by default.
        """
        intents = discord.Intents.default()
        intents.members = True  # required for player/role management

        super().__init__(
            command_prefix=commands.when_mentioned,
            intents=intents,
        )

        self.sync_commands_enabled = sync_commands
        self.commands_guild_id = commands_guild_id

        self.registry = GameRegistry(self, settings)
        self.controller = WerewolfController(self.registry)
        self.role_gateway = RoleGateway()
        self.message_gateway = MessageGateway(self)

    async def setup_cogs(self) -> None:
        """Register all cogs: the gateway events and the narrator commands."""
        await self.add_cog(GuildEvents(self))
        await self.add_cog(NarratorCommands(self, self.controller))
        logger.info("Cogs added")

    async def sync_commands(self) -> None:
        """Synchronize slash commands with Discord, globally or on one guild."""
        if not self.sync_commands_enabled:
            logger.info("Slash commands sync skipped")
            return

        if self.commands_guild_id is None:
            await self.tree.sync()
            logger.info("Slash commands synced globally")
            return

        guild = discord.Object(id=self.commands_guild_id)
        self.tree.copy_global_to(guild=guild)
        await self.tree.sync(guild=guild)
        logger.info("Slash commands synced on guild %s", self.commands_guild_id)

    async def setup_hook(self) -> None:
        """Run the bot initialization sequence.

        This method is automatically called by discord.py before the bot
        connects to Discord. It orchestrates the full startup process:
        - loads cogs (event listeners and slash commands)
        - syncs slash commands

        No view is registered as persistent: the setup of each guild purges
        the lobby and posts fresh views, so no button outlives a restart.

        Raises:
            Exception: Re-raised if any initialization step fails.
        """
        try:
            await self.setup_cogs()
            await self.sync_commands()

            logger.info("Garulf bot ready")
        except Exception:
            logger.exception("Failed during bot setup")
            raise

__init__(*, sync_commands=True, commands_guild_id=None, settings=None)

Initialize the Discord client and core dependencies.

Sets up Discord intents, uses mentions as the text command prefix (the bot relies on slash commands), and instantiates the main application components.

Parameters:

Name Type Description Default
sync_commands bool

Push the slash commands to Discord at startup. Syncing is rate limited, skip it when the commands did not change.

True
commands_guild_id int | None

Sync the slash commands on this guild only, where they are available at once (development), instead of globally where they take a while to propagate.

None
settings GameSettings | None

How the games are run, the production rules by default.

None
Source code in sources/discord_api/client.py
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
def __init__(
    self,
    *,
    sync_commands: bool = True,
    commands_guild_id: int | None = None,
    settings: GameSettings | None = None,
) -> None:
    """Initialize the Discord client and core dependencies.

    Sets up Discord intents, uses mentions as the text command prefix
    (the bot relies on slash commands), and instantiates the main
    application components.

    Args:
        sync_commands: Push the slash commands to Discord at startup.
            Syncing is rate limited, skip it when the commands did not
            change.
        commands_guild_id: Sync the slash commands on this guild only,
            where they are available at once (development), instead of
            globally where they take a while to propagate.
        settings: How the games are run, the production rules by default.
    """
    intents = discord.Intents.default()
    intents.members = True  # required for player/role management

    super().__init__(
        command_prefix=commands.when_mentioned,
        intents=intents,
    )

    self.sync_commands_enabled = sync_commands
    self.commands_guild_id = commands_guild_id

    self.registry = GameRegistry(self, settings)
    self.controller = WerewolfController(self.registry)
    self.role_gateway = RoleGateway()
    self.message_gateway = MessageGateway(self)

setup_cogs() async

Register all cogs: the gateway events and the narrator commands.

Source code in sources/discord_api/client.py
62
63
64
65
66
async def setup_cogs(self) -> None:
    """Register all cogs: the gateway events and the narrator commands."""
    await self.add_cog(GuildEvents(self))
    await self.add_cog(NarratorCommands(self, self.controller))
    logger.info("Cogs added")

setup_hook() async

Run the bot initialization sequence.

This method is automatically called by discord.py before the bot connects to Discord. It orchestrates the full startup process: - loads cogs (event listeners and slash commands) - syncs slash commands

No view is registered as persistent: the setup of each guild purges the lobby and posts fresh views, so no button outlives a restart.

Raises:

Type Description
Exception

Re-raised if any initialization step fails.

Source code in sources/discord_api/client.py
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
async def setup_hook(self) -> None:
    """Run the bot initialization sequence.

    This method is automatically called by discord.py before the bot
    connects to Discord. It orchestrates the full startup process:
    - loads cogs (event listeners and slash commands)
    - syncs slash commands

    No view is registered as persistent: the setup of each guild purges
    the lobby and posts fresh views, so no button outlives a restart.

    Raises:
        Exception: Re-raised if any initialization step fails.
    """
    try:
        await self.setup_cogs()
        await self.sync_commands()

        logger.info("Garulf bot ready")
    except Exception:
        logger.exception("Failed during bot setup")
        raise

sync_commands() async

Synchronize slash commands with Discord, globally or on one guild.

Source code in sources/discord_api/client.py
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
async def sync_commands(self) -> None:
    """Synchronize slash commands with Discord, globally or on one guild."""
    if not self.sync_commands_enabled:
        logger.info("Slash commands sync skipped")
        return

    if self.commands_guild_id is None:
        await self.tree.sync()
        logger.info("Slash commands synced globally")
        return

    guild = discord.Object(id=self.commands_guild_id)
    self.tree.copy_global_to(guild=guild)
    await self.tree.sync(guild=guild)
    logger.info("Slash commands synced on guild %s", self.commands_guild_id)