https://github.com/amberlee2427/brian-bot

A Discord bot for D&D servers with OpenAI integration, character sheet management, dice rolling, and more. Built with Python and designed for security and ease of use.

https://github.com/amberlee2427/brian-bot

Science Score: 26.0%

This score indicates how likely this project is to be science-related based on various indicators:

  • CITATION.cff file
  • codemeta.json file
    Found codemeta.json file
  • .zenodo.json file
    Found .zenodo.json file
  • DOI references
  • Academic publication links
  • Committers with academic emails
  • Institutional organization owner
  • JOSS paper metadata
  • Scientific vocabulary similarity
    Low similarity (14.8%) to scientific vocabulary

Keywords

character-sheet dice dice-roller dice-rolls discord-bot discord-bots dnd dnd-tools dnd5e dnd5e-tools open-source openai opensource python python-3 python3
Last synced: 5 months ago · JSON representation

Repository

A Discord bot for D&D servers with OpenAI integration, character sheet management, dice rolling, and more. Built with Python and designed for security and ease of use.

Basic Info
  • Host: GitHub
  • Owner: AmberLee2427
  • License: mit
  • Language: Python
  • Default Branch: main
  • Homepage:
  • Size: 780 KB
Statistics
  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • Open Issues: 0
  • Releases: 2
Topics
character-sheet dice dice-roller dice-rolls discord-bot discord-bots dnd dnd-tools dnd5e dnd5e-tools open-source openai opensource python python-3 python3
Created 9 months ago · Last pushed 9 months ago
Metadata Files
Readme Contributing License Security

README.md

Brian Bot

A helpful Discord assistant built with Python and OpenAI. Brian is designed to be a secure, efficient, and friendly bot that can help manage and interact with your Discord server.

A cute imp wearing a nametag that says "Brian"

Features

  • Natural Conversations: Chat with Brian using natural language
  • Channel Summarization: Get quick summaries of channel discussions
  • Message Search: Find specific messages across configured channels
  • Security First: Built with security best practices including rate limiting and input sanitization
  • Multi-bot Friendly: Properly handles conversations with other bots

Setup

Prerequisites

  1. Create a Discord Bot:

    • Go to Discord Developer Portal
    • Click "New Application" and give it a name
    • Add the Brain1.png or Brain2.png image as Brian's profile pic
    • Go to the "Installation" tab and select None for the Install link
    • Go to the "Bot" tab and turn of the "PUBLIC BOT" toggle
    • Under "Privileged Gateway Intents", enable:
      • MESSAGE CONTENT INTENT
      • SERVER MEMBERS INTENT
    • Copy the bot token and save it somewhere secure (you'll need this for the .env file)
    • Go to OAuth2 > URL Generator
    • Select scopes: bot and applications.commands
    • Select bot permissions:
      • Read Messages/View Channels
      • Send Messages
      • Read Message History
      • Add Reactions
      • Use Slash Commands
    • Copy the generated URL and paste in into a new tab to invite the bot to your server
  2. Get an OpenAI API Key:

    • Go to OpenAI Platform
    • Sign up or log in
    • Go to API Keys section
    • Click "Create new secret key"
    • Copy the API key (you'll need this for the .env file)
    • Note: The API key starts with "sk-" and is used for billing
    • Note: You will likely need to prepay for tokens. Add a small amount of credit e.g. $5. You may be able to sell you data to OpenAI for $120 of credits a month, but not until you are on the appropriate member tier. So pay for a few calls and then consider selling your D&D anticts for effectively unlimited API usage.
    • Note: Monitor this account for unexpected activity and reset your tokens if you are suspicious.

Local Development

  1. Fork and Clone the Repository: Fork this repository to your GitHub account by clicking the "Fork" button at the top of the page.

Clone the repo locally: bash git clone https://github.com/yourusername/brian-bot.git cd brian-bot We recommend you "Watch" the repo in order to be notified of updates - Click the "Watch" button at the top of the repository - Select "Custom" to configure notifications - Enable notifications for: - "Releases" (when new versions are published) - "Discussions" (for community support) - "Security alerts" (for important security updates) - Click "Apply" to save your preferences

  1. Create a .env file: In the same directory, create a file named .env and add your secret keys: ``` # Required DISCORDTOKEN=yourdiscordbottokengoeshere OPENAIAPIKEY=youropenaiapikeygoes_here

# Optional (with defaults) SESSIONNOTESCHANNEL=your-session-notes-channel-name # Default: session-notes DATADIR=characters # Default: characters COMMANDPREFIX=! # Default: ! MODELNAME=gpt-4 # Default: gpt-4 MAXTOKENSFORRESPONSE=1500 # Default: 1500 RATELIMITMENTIONS=5 # Default: 5 mentions per minute RATELIMITCOMMANDS=10 # Default: 10 commands per minute RATELIMITWINDOW=60 # Default: 60 seconds

# Channel and Role Configuration SEARCHABLECHANNELIDS=123456789012345678,876543210987654321 # Comma-separated list of channel IDs for !find command ALLOWEDROLES=123456789012345678,876543210987654321 # Comma-separated list of role IDs that can use restricted commands ADMINROLES=123456789012345678,876543210987654321 # Comma-separated list of role IDs with admin privileges ```

After creating the .env file, secure it with: bash chmod 600 .env This ensures only your user can read and write to the file.

  1. Install Dependencies: bash pip install -r requirements.txt

  2. Run the Bot: bash python main.py

Railway Deployment

  1. Create Required Accounts:

  2. Fork and Clone:

    • Fork this repository to your GitHub account by clicking the "Fork" button at the top of the page
    • We recommend you "Watch" the repo in order to be notified of updates
      • Click the "Watch" button at the top of the repository
      • Select "Custom" to configure notifications
      • Enable notifications for:
      • "Releases" (when new versions are published)
      • "Discussions" (for community support)
      • "Security alerts" (for important security updates)
      • Click "Apply" to save your preferences
  3. Deploy to Railway:

    • Create a new project in Railway
    • Connect your GitHub account if not already connected
    • Select your forked repository
      • If the repo isn't showing up under the GitHub options:
      • Scroll to the bottom of the page and select "Empty Project"
      • Select "Add a service" > "GitHub Repo" > "Configure GitHub App" >
      • You may need to Authenticate to continue
      • Under "Repository Access", select either:
        • "All repositiories" or
        • "Only select repositories" and choose "brain-bot" from the dropdown list
      • "Save"
    • Add the following environment variables in Railway:
      • Where:
      • In "Architecture" view in your project space, click on the brain-bot box
      • Select the "Variables" tab
      • New Variable
      • Paste or type in the relevant content.
      • Required:
      • DISCORD_TOKEN: Your Discord bot token
      • OPENAI_API_KEY: Your OpenAI API key
      • Optional (with defaults):
      • SESSION_NOTES_CHANNEL: The name of your session notes channel (without the #)
      • DATA_DIR: (Optional, recommended) Custom directory for character sheets
      • COMMAND_PREFIX: Bot command prefix (default: !)
      • MODEL_NAME: OpenAI model to use (default: gpt-4)
      • MAX_TOKENS_FOR_RESPONSE: Maximum tokens for AI responses (default: 1500)
      • RATE_LIMIT_MENTIONS: Mentions allowed per minute (default: 5)
      • RATE_LIMIT_COMMANDS: Commands allowed per minute (default: 10)
      • RATE_LIMIT_WINDOW: Rate limit window in seconds (default: 60)
      • Channel and Role Configuration:
      • SEARCHABLE_CHANNEL_IDS: Comma-separated list of channel IDs for the !find command
      • ALLOWED_ROLES: Comma-separated list of role IDs that can use restricted commands
      • ADMIN_ROLES: Comma-separated list of role IDs with admin privileges
      • Secure the Token and API Key variables by selecting the triple dot and Seal, if the option is available.
  4. Configure the Bot:

    • The bot will automatically deploy when you push to the main branch (this could take a few minutes)
    • Monitor the deployment in the Railway dashboard
    • Check the logs for any issues
  5. Updating the Bot:

    • Push changes to your repository or "Sync Fork" with the main repo
    • Railway will automatically redeploy
    • Monitor the deployment status in the dashboard

Setting Up Persistent Storage (Optional but Recommended)

To ensure your character sheets persist across deployments and restarts, you can set up a storage service on Railway:

  1. Add Storage Service:

    • In your Railway project dashboard, click "New"
    • Select "Storage" from the service options
    • Choose a name for your storage service (e.g., "brian-storage")
  2. Configure Storage:

    • Once created, Railway will provide a mount path
    • Add this environment variable to your bot service: DATA_DIR=/data/characters
    • This will store character sheets in the persistent storage volume
  3. Verify Setup:

    • Deploy your bot
    • Create a character sheet using !coin or by mentioning Brian
    • Check the /data/characters directory in your storage service
    • Your character sheets should persist even if the bot restarts

Note: If you don't set up persistent storage, character sheets will be stored in the bot's ephemeral filesystem and will be lost when the bot restarts or redeploys.

Usage

  • Chat: Mention @Brian in a message to start a conversation
  • Summarize: Use !summarize #channel-name to get a summary of that channel
  • Recap: Use !recap for a quick summary of your session notes channel
  • Find: Use !find your search query to look for messages in configured channels

Security Features

  • Rate limiting to prevent abuse
  • Input sanitization for all user inputs
  • Role-based access control
  • API key validation
  • Secure error handling
  • Message length limits

Troubleshooting

  1. Bot not responding:

    • Check if the bot is online
    • Verify API keys in .env or Railway environment variables
    • Check bot permissions in Discord
  2. Commands not working:

    • Ensure the bot has necessary permissions
    • Check if you have the required roles
    • Verify channel permissions
  3. Rate limiting:

    • Wait 60 seconds before trying again
    • Contact server admin if persistent
  4. Railway Deployment Issues:

    • Check Railway logs for errors
    • Verify environment variables are set correctly
    • Ensure the Procfile and runtime.txt are present
    • Check if the build process completed successfully

Contributing

  1. Fork the repository
  2. Create a feature branch
  3. Commit your changes
  4. Push to the branch
  5. Create a Pull Request

Please read CONTRIBUTING.md for details on our code of conduct and the process for submitting pull requests.

Security

If you discover any security-related issues, please email [your-email] instead of using the issue tracker.

License

This project is licensed under the MIT License - see the LICENSE file for details.

Acknowledgments

  • OpenAI for the GPT API
  • Discord.py for the Discord API wrapper
  • All contributors who have helped improve Brian

Release Notes

Initial release of Brian Bot, a Discord bot for D&D servers with the following features:

  • Natural language conversations with OpenAI integration
  • Character sheet management
  • Dice rolling
  • Channel summarization
  • Message search
  • Session notes tracking
  • Rate limiting and security features

Key Features: - Default character sheet template - Persistent storage support - Configurable through environment variables - Railway deployment support - Comprehensive documentation

This is the first stable release of Brian Bot. All features are fully functional and documented.

Owner

  • Name: Amber
  • Login: AmberLee2427
  • Kind: user
  • Location: New Zealand

GitHub Events

Total
  • Release event: 2
  • Watch event: 1
  • Issue comment event: 1
  • Public event: 1
  • Push event: 12
  • Pull request event: 2
  • Create event: 2
Last Year
  • Release event: 2
  • Watch event: 1
  • Issue comment event: 1
  • Public event: 1
  • Push event: 12
  • Pull request event: 2
  • Create event: 2

Committers

Last synced: 8 months ago

All Time
  • Total Commits: 50
  • Total Committers: 1
  • Avg Commits per committer: 50.0
  • Development Distribution Score (DDS): 0.0
Past Year
  • Commits: 50
  • Committers: 1
  • Avg Commits per committer: 50.0
  • Development Distribution Score (DDS): 0.0
Top Committers
Name Email Commits
Amber a****s@g****m 50

Issues and Pull Requests

Last synced: 8 months ago

All Time
  • Total issues: 0
  • Total pull requests: 1
  • Average time to close issues: N/A
  • Average time to close pull requests: about 1 hour
  • Total issue authors: 0
  • Total pull request authors: 1
  • Average comments per issue: 0
  • Average comments per pull request: 1.0
  • Merged pull requests: 0
  • Bot issues: 0
  • Bot pull requests: 0
Past Year
  • Issues: 0
  • Pull requests: 1
  • Average time to close issues: N/A
  • Average time to close pull requests: about 1 hour
  • Issue authors: 0
  • Pull request authors: 1
  • Average comments per issue: 0
  • Average comments per pull request: 1.0
  • Merged pull requests: 0
  • Bot issues: 0
  • Bot pull requests: 0
Top Authors
Issue Authors
Pull Request Authors
  • AmberLee2427 (2)
Top Labels
Issue Labels
Pull Request Labels

Dependencies

requirements.txt pypi
  • discord.py >=2.3.2
  • httpx ==0.25.2
  • openai >=1.3.0
  • python-dotenv >=1.0.0
  • tiktoken >=0.5.1