Skip to main content

A discord bot framework!

Project description

Betterbot

A modern, event-driven bot framework for Python, designed for simplicity and flexibility.

Install

Install via pip:

pip install betterbot

How to Use

Import the Setup module and initialize your bot:

from betterbot import Setup

Example Bot Script

import asyncio
import json
from betterbot import Setup, Events, Intents

# Load bot token from configuration
with open("config.json") as f:
    config = json.load(f)

TOKEN = config["TOKEN"]

class Bot:
    def __init__(self):
        self.bot = Setup(TOKEN, intents=Intents.all)
        self.bot.debug = True  # Enable detailed debug logs
        self.rest = self.bot.rest_client
        self.bot_username = None
        self.event = self.bot.event

    async def main(self):
        # Register event handlers
        await self.event.register(Events.BOT_READY, self.on_ready) # You can register multiple handlers per event.
        await self.event.register(Events.MESSAGE_CREATE, self.on_message)
        await self.bot.start()

    # -----------------------
    # Event Handlers
    # -----------------------
    async def on_ready(self, username, es):
        self.bot_username = username
        print(f"Logged in as {username}")

        # Create a guild slash command

        # Es stands for Event-Send use it in response to events/command events 
        await es.create_guild_command(
            self.test_command,
            guild_id=GUILD_ID,
            name="testrest",
            description="some example REST endpoints"
        )

    async def on_message(self, username, content, channel_id):
        if username == self.bot_username:
            return
        print(f"{username}: {content}")
        if content.lower() == "example rest":
            await self.test_rest_methods()

    # -----------------------
    # Slash Command Handler
    # -----------------------
    async def test_command(self, es):
        # Es is the prefered way to respond to events you can do it manually but that is not recommended
        # Immediately respond to the interaction
        await es.respond_message(content="Hello, it is starting!")
        await self.test_rest_methods()

    async def test_rest_methods(self):
        # -----------------------
        # Channel Methods
        # -----------------------
        await self.rest.send_message(CHANNEL_ID, "Hello from Betterbot REST test!")

        messages = await self.rest.get_messages(CHANNEL_ID)
        if messages:
            msg_id = messages[0]["id"]
            await self.rest.typing(CHANNEL_ID)
            await self.rest.add_reaction(CHANNEL_ID, msg_id, "👍")
            await self.rest.remove_reaction(CHANNEL_ID, msg_id, "👍")
            await self.rest.pin_message(CHANNEL_ID, msg_id)
            await self.rest.unpin_message(CHANNEL_ID, msg_id)

        # -----------------------
        # Guild Methods
        # -----------------------
        guild_info = await self.rest.get_guild_info(GUILD_ID)
        members = await self.rest.get_guild_members(GUILD_ID, limit=5)
        roles = await self.rest.get_guild_roles(GUILD_ID)

        print("REST test complete!")

# -----------------------
# Run Bot
# -----------------------
asyncio.run(Bot().main())

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

betterbot-6.0.2.tar.gz (37.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

betterbot-6.0.2-py3-none-any.whl (58.1 kB view details)

Uploaded Python 3

File details

Details for the file betterbot-6.0.2.tar.gz.

File metadata

  • Download URL: betterbot-6.0.2.tar.gz
  • Upload date:
  • Size: 37.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.3

File hashes

Hashes for betterbot-6.0.2.tar.gz
Algorithm Hash digest
SHA256 d24e2e6b2837dbbb1f1d5347355ddd6cca6ed51a95bdab1543798727fcd0bdd6
MD5 19be23c0b6260ba46192abbe3d8bafb3
BLAKE2b-256 ce880aea62e7ac15d68fc6e0e18d7b5743d91675c6eca36130d5a5a759df1f0f

See more details on using hashes here.

File details

Details for the file betterbot-6.0.2-py3-none-any.whl.

File metadata

  • Download URL: betterbot-6.0.2-py3-none-any.whl
  • Upload date:
  • Size: 58.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.3

File hashes

Hashes for betterbot-6.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 07f5488442b44f8fc0bf1fc6458c6a6ae9ce7394cba8287eb52532244cc82cc6
MD5 8bc7a413ee5c8a88f69362840bca613e
BLAKE2b-256 e963757c5d5877878cbc11d908b9474e23451e32a6e6bf86d9a6b46af530a1dc

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page