Discord Helpers

This module contains helper functions and classes for interacting with Discord.

class bobbot.discord_helpers.BobBot(*args, **kwargs)[source]

Bases: Bot

Bob’s Discord bot.

CHANNELS: list[int] = [734222733113819168, 809279199667159062]

The channels the bot is active in.

active_channel: TextChannel | None = None

The active channel for the bot.

is_incognito: bool = False

Whether the bot is incognito (will not store memories).

is_obedient: bool = False

Whether the bot is obedient (encouraged to fulfill all requests).

is_on: bool = True

Whether the bot is on (will send messages).

speed: Speed = 'instant'

The typing speed of the bot.

class bobbot.discord_helpers.ManualHistory(history: list[str] = None)[source]

Bases: object

Generate message histories manually. Messages sent by bob should always start with “bob: “.

add_message(message: str) None[source]

Add a message (with any desired context) to the message history.

as_langchain_msgs() list[str][source]

Return the message history as Langchain messages.

as_string() str[source]

Return the full message history.

limit_messages(limit: int) None[source]

Limit the number of messages in the history.

class bobbot.discord_helpers.ParsedMessage(message: Message, is_deleted: bool = False)[source]

Bases: object

Represents a parsed Discord message.

as_string(with_author: bool = True, with_context: bool = True, with_reactions: bool = True, with_timestamp: bool = False) str[source]

Format the message as a string.

Parameters:
  • with_author – Whether to include the author.

  • with_context – Whether to include context info.

  • with_reactions – Whether to include reactions.

  • with_timestamp – Whether to include a timestamp.

Returns:

The formatted message.

property author: str

As a string.

property content: str

Text of the message.

property context: str

Whether the message is edited/deleted, plus if it’s a reply/command response.

property full_content: str

Includes content, attachment URLs, and stickers.

property id: int

The message ID.

is_deleted: bool = False

Whether the message was deleted.

property is_edited: bool

Whether the message was edited.

message: Message

The raw message.

property reactions: str

As a string.

property timestamp: str

Relative to now, as a string.

class bobbot.discord_helpers.Speed(value, names=<not given>, *values, module=None, qualname=None, type=None, start=1, boundary=None)[source]

Bases: Enum

Valid speeds of the bot.

DEFAULT = 'default'
INSTANT = 'instant'
class bobbot.discord_helpers.TextChannelHistory(channel: TextChannel)[source]

Bases: object

Tracks the history of a text channel, providing concise representations of messages and other events.

MAX_CONTENT_LENS: list[int] = [4096, 4096, 256, 256, 256, 256, 256, 256, 256, 256, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64]

The maximum lengths for message content, starting with the most recent.

MAX_MSGS: int = 20

The maximum number of messages to track.

as_langchain_msgs(bot_user: User, limit: int = 20, get_image: bool = True) list[BaseMessage][source]

Get a list of LangChain messages representing the history, up to the last limit messages.

Parameters:
  • bot_user – The bot user. Used to distinguish AIMessages from HumanMessages.

  • limit – The maximum number of messages to include.

  • get_image – Whether to include a single image URL in the most recent message (if present).

Returns:

A list of LangChain message objects, with only HumanMessages containing author/context info.

as_parsed_messages(limit: int = 20) list[ParsedMessage][source]

Get the channel’s message history, up to the last limit messages.

as_string(limit: int = 20, with_author: bool = True, with_context: bool = True, with_reactions: bool = True, with_timestamp: bool = False) str[source]

Get a string representation of the history, up to the last limit messages.

Parameters:
  • limit – The maximum number of messages to include.

  • with_author – Whether to include the author.

  • with_context – Whether to include context info.

  • with_reactions – Whether to include reactions.

  • with_timestamp – Whether to include a timestamp.

Returns:

A string representing the history.

async aupdate() None[source]

Update the history with the latest messages and events.

This method should be called before querying the history after every idle period.

channel: TextChannel

The channel being tracked.

clear_users_typing() None[source]

Clear all currently typing users.

get_users_typing() list[User][source]

Returns a list of users currently typing.

history: list[ParsedMessage]

Recent messages in the channel.

history_to_strings(transform: Callable[[ParsedMessage], str], limit: int = 20) list[str][source]

Get a list of truncated strings representing the history, up to the last limit messages.

Messages are truncated based on how recent they are, with older messages being truncated more.

Parameters:
  • transform – A function to convert a ParsedMessage to a string.

  • limit – The maximum number of messages to include.

Returns:

A list of strings representing the history.

is_typing: dict[User, datetime]

Users currently typing in the channel.

message_count: int

Counter for the total number of messages sent in the channel.

on_typing(user: User, when: datetime) None[source]

Handle a typing event.

async bobbot.discord_helpers.check_waiting_responses(channel: TextChannel) None[source]

Check if any waiting responses were answered.

async bobbot.discord_helpers.command_handler(channel: TextChannel, command: str, expect_response: bool = False, output_directly: bool = False, hide_output: bool = False, use_history: bool = True) str | None[source]

Handle a command from the current activity.

Commands are directions to Bob, with info or requests to be relayed to the user. If expect_response is True, the user’s response will be waited for and returned. Otherwise, the response given to the user is returned.

Parameters:
  • channel – The channel the command is associated with.

  • command – The command to give to Bob.

  • expect_response – Whether to wait for the user’s response.

  • output_directly – Whether to send the literal command directly to the user.

  • hide_output – Whether to avoid sending the message to the user (just return it instead).

  • use_history – Whether to include any channel history.

bobbot.discord_helpers.gen_command_handler(channel: TextChannel) Callable[source]

Generate a command handler for the given channel.

bobbot.discord_helpers.get_channel_history(channel: TextChannel) TextChannelHistory[source]

Get the history for a channel, creating it if it doesn’t exist.

bobbot.discord_helpers.get_users_in_channel(channel: DMChannel | TextChannel) list[User][source]

Get a list of all users in a Discord channel.

async bobbot.discord_helpers.lazy_send_message(channel: TextChannel, message_str: str, instant: bool = False, force: bool = False) bool[source]

Send a message to a channel with typing time. Cancels the send on new messages or others typing.

If the message is too long, it will be split into chunks before sending. If instant is True, the message will be sent instantly (regardless of the bot’s mode). If force is True, the message will not be cancelled. Note that empty messages still won’t be sent.

Parameters:
  • channel – The channel to send the message to.

  • message_str – The message to send.

  • instant – Whether to try to send the message instantly. May still be cancelled.

  • force – Whether to force the message to be sent.

Returns:

Whether the message was sent in full.

async bobbot.discord_helpers.on_message(message: Message, use_perplexity: bool = False, use_reasoning: bool = False)[source]

Respond to messages, using or not using smart search (Perplexity) and reasoning.

bobbot.discord_helpers.run_bot() None[source]

Run the bot. Blocks until the bot is stopped.