Skip to main content

Async Discord bot framework without discord.py

Project description

diserve

diserve — асинхронная Python-библиотека для создания Discord-ботов без discord.py и других SDK-обёрток. Фреймворк работает напрямую с Discord Gateway и Discord REST API через aiohttp.

Возможности

  • Полностью асинхронная архитектура на asyncio
  • Префиксные команды через @bot.command()
  • Slash-команды через @bot.slash_command() с авто-генерацией options по сигнатуре
  • События @bot.event (on_ready, on_message, on_command_error и другие)
  • Контекст команд ctx с reply, send, defer, followup
  • Встроенные модели User, Message, Channel, Guild
  • Embed-система
  • UI-компоненты: кнопки, select menu, action row
  • Modal и text input
  • Intents, Activity, Color
  • Система расширений (cogs/extensions)
  • Цветной CLI: diserve init, diserve run, diserve version

Установка

Установка из PyPI

pip install diserve

Далее добавьте пакет diserve в ваш проект.

Быстрый старт через CLI

1. Инициализировать проект

diserve init --name bottable --prefix "." --token "TOKEN"

Будет создана папка bottable/ с базовым bot.py и pyproject.toml.

2. Запуск

cd bottable
diserve run

3. Версия CLI

diserve version

Ожидаемый формат:

diserve  v0.1.4

Полный пример бота

import diserve
from diserve import Bot, Command, Embed, Color

bot = diserve.Bot(
    prefix="!",
    intents=diserve.Intents.all(),
    activity=diserve.Activity(
        type=diserve.ActivityType.watching,
        name="the world burn",
    ),
)

@bot.command()
async def ping(ctx: Command):
    await ctx.reply("Pong!")

@bot.slash_command(name="hello", description="Says hello to the user")
async def hello(ctx: Command):
    await ctx.reply(f"Hello, {ctx.author.mention}!")

@bot.slash_command(name="sum", description="Складывает два числа")
async def sum(ctx: Command, a: int, b: int):
    await ctx.reply(f"{a} + {b} = {a + b}")

@bot.slash_command(name="embed", description="Sends an embed message")
async def embed(ctx: Command):
    emb = Embed(
        title="This is an embed",
        description="This is the description of the embed",
        color=Color.blue(),
    )
    emb.add_field(name="Field 1", value="Value 1", inline=False)
    emb.set_footer(text="Footer text")
    await ctx.reply(embed=emb)

@bot.command()
async def clear(ctx: Command, amount: int):
    await ctx.channel.purge(limit=amount)

@bot.event
async def on_ready():
    print(f"Logged in as {bot.user.username} (ID: {bot.user.id})")
    print(f"Guilds: {bot.guild_count} | Commands: {len(bot.commands)}")
    print(f"Latency: {bot.latency * 1000:.0f}ms")

@bot.event
async def on_command_error(ctx, error):
    if ctx is not None:
        await ctx.reply(f"Ошибка: {error}")

bot.run("TOKEN")

Архитектура проекта

diserve/
├── client/
│   ├── bot.py
│   ├── gateway.py
│   └── http.py
├── models/
│   ├── user.py
│   ├── guild.py
│   ├── message.py
│   └── channel.py
├── commands/
│   ├── command.py
│   ├── slash.py
│   └── context.py
├── ui/
│   ├── embed.py
│   ├── modal.py
│   └── components.py
├── utils/
│   ├── color.py
│   ├── intents.py
│   └── activity.py
├── ext/
│   └── cogs.py
└── cli/
    └── main.py

Подробно: как создать бота на базе diserve

Шаг 1. Создайте приложение в Discord Developer Portal

  1. Откройте Discord Developer Portal
  2. Нажмите New Application
  3. Создайте Bot
  4. Скопируйте токен
  5. Включите необходимые Privileged Gateway Intents при необходимости
  6. Сгенерируйте Invite URL с нужными scope/permissions

Шаг 2. Подготовьте окружение

python -m venv .venv
source .venv/bin/activate
pip install diserve

Шаг 3. Создайте файл bot.py

Минимум:

import diserve

bot = diserve.Bot(prefix="!", intents=diserve.Intents.all())

@bot.command()
async def ping(ctx):
    await ctx.reply("Pong!")

bot.run("TOKEN")

Шаг 4. Добавьте события

@bot.event
async def on_ready():
    print("ready")

@bot.event
async def on_message(message):
    print(message.content)

Шаг 5. Добавьте slash-команды

diserve регистрирует global slash-команды при запуске бота.

@bot.slash_command(name="ban", description="Ban user")
async def ban(ctx, user_id: int, reason: str = "-"):
    await ctx.reply(f"Ban request for {user_id} with reason: {reason}")

Правила автогенерации типов аргументов:

  • str -> STRING
  • int -> INTEGER
  • bool -> BOOLEAN
  • float -> NUMBER

Шаг 6. Работа с ctx

ctx содержит:

  • ctx.author
  • ctx.channel
  • ctx.guild
  • ctx.message
  • ctx.bot

Методы:

  • await ctx.reply(...)
  • await ctx.send(...)
  • await ctx.defer(...)
  • await ctx.followup(...)

Шаг 7. Embed

embed = diserve.Embed(title="Статус", description="Все работает", color=diserve.Color.green())
embed.add_field(name="Shard", value="0")
embed.set_thumbnail("https://example.com/img.png")
await ctx.reply(embed=embed)

Шаг 8. UI-компоненты

from diserve import Button, SelectMenu, SelectOption, ActionRow

row = ActionRow(components=[
    Button(label="Нажми", custom_id="btn:click", style=1),
])

menu = SelectMenu(
    custom_id="menu:main",
    options=[
        SelectOption(label="A", value="a"),
        SelectOption(label="B", value="b"),
    ],
)

Шаг 9. Modal

from diserve import Modal, TextInput

modal = Modal(
    custom_id="feedback",
    title="Обратная связь",
    components=[
        TextInput(custom_id="text", label="Ваше сообщение", style=2),
    ],
)

Шаг 10. Purge сообщений

@bot.command()
async def clear(ctx, amount: int):
    await ctx.channel.purge(limit=amount)

Шаг 11. Cogs/Extensions

bot.load_extension("mybot.extensions.admin")
bot.unload_extension("mybot.extensions.admin")

Пример extension-модуля:

def setup(bot):
    @bot.command()
    async def hi(ctx):
        await ctx.reply("hi")

def teardown(bot):
    pass

Шаг 12. Обработка ошибок

@bot.event
async def on_command_error(ctx, error):
    if ctx:
        await ctx.reply(f"Ошибка команды: {error}")

Рекомендации для production

  • Не храните токен в коде, используйте переменные окружения
  • Ограничьте intents только нужными
  • Логируйте исключения и события
  • Добавляйте retry/backoff вокруг нестабильных сетевых операций
  • Разделяйте команды по расширениям
  • Проверяйте права пользователя перед опасными операциями

Публичное API

import diserve
from diserve import (
    Bot,
    Command,
    Embed,
    Color,
    Intents,
    Activity,
    ActivityType,
    Button,
    SelectMenu,
    SelectOption,
    ActionRow,
    Modal,
    TextInput,
)

Лицензия

Проект распространяется по лицензии вашего репозитория.

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

diserve-0.1.4.tar.gz (18.2 kB view details)

Uploaded Source

Built Distribution

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

diserve-0.1.4-py3-none-any.whl (21.4 kB view details)

Uploaded Python 3

File details

Details for the file diserve-0.1.4.tar.gz.

File metadata

  • Download URL: diserve-0.1.4.tar.gz
  • Upload date:
  • Size: 18.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.10

File hashes

Hashes for diserve-0.1.4.tar.gz
Algorithm Hash digest
SHA256 0cd1d124f46c0ee6ccc70514477dc375a9f1ab8fa7568622c06f434f0e24ecd5
MD5 00aaac86153b5b5eb91b4026be5092da
BLAKE2b-256 db2c9a14fb44ee778a92247824ff684342a56673e840b280ba77bf96aba1b134

See more details on using hashes here.

File details

Details for the file diserve-0.1.4-py3-none-any.whl.

File metadata

  • Download URL: diserve-0.1.4-py3-none-any.whl
  • Upload date:
  • Size: 21.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.10

File hashes

Hashes for diserve-0.1.4-py3-none-any.whl
Algorithm Hash digest
SHA256 b5e70df00e558cb637412003ac120ef81312ef77c0dbddeef663fad0118ec34c
MD5 b34233d2d06e5179d78c2f928853ba44
BLAKE2b-256 ceb69df135b88757da4429ccf181d244e913069e155b1a1ef892d5a8fc328d43

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