Skip to main content

Crate botrs

Crate botrs 

Source
Expand description

§BotRS - Rust QQ Guild Bot Framework

§Author: YinMo19

License: MIT Rust GitHub Crates.io

本实现不是 qqbot 的官方 rust 版本实现。

BotRS 是一个用 Rust 实现的 QQ 频道机器人框架,基于 QQ 频道机器人 API。它提供了类型安全、高性能、易于使用的接口来开发 QQ 频道机器人。

§快速开始

§安装

将以下内容添加到你的 Cargo.toml:

[dependencies]
botrs = "0.11.0"
tokio = { version = "1.0", features = ["full"] }
tracing = "0.1"
tracing-subscriber = "0.3"
async-trait = "0.1"

§基础示例

use botrs::{Client, Context, EventHandler, Intents, Token, Message};
use botrs::models::gateway::Ready;
use botrs::models::message::MessageParams;
use tracing::info;

struct MyBot;

#[async_trait::async_trait]
impl EventHandler for MyBot {
    async fn ready(&self, _ctx: Context, ready: Ready) {
        info!("Bot {} is ready!", ready.user.username);
    }

    async fn message_create(&self, ctx: Context, message: Message) {
        if message
            .author
            .as_ref()
            .and_then(|author| author.bot)
            .unwrap_or_default()
        {
            return;
        }

        if let Some(content) = &message.content {
            if content.trim() == "!ping" {
                info!("Received ping command from message ID: {:?}", message.id);

                // 使用新的参数结构 API
                let params = MessageParams::new_text("Pong! 🏓");
                if let Some(channel_id) = &message.channel_id {
                    if let Err(e) = ctx.send_message(channel_id, params).await {
                        info!("Failed to reply: {}", e);
                    }
                }
            }
        }
    }
}

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    // 创建令牌
    let token = Token::new("your_app_id", "your_secret");

    // 设置意图
    let intents = Intents::default();

    // 创建客户端
    let mut client = Client::new(token, intents, MyBot, false)?;

    // 启动机器人
    client.start().await?;

    Ok(())
}

我们强烈推荐你查看 完整文档 来进行开发。这个仓库基于官方网络接口行为和 Rust 的语言特性进行了深度开发。

Re-exports§

pub use client::Client;
pub use client::Context;
pub use client::EventHandler;
pub use error::BotError;
pub use error::Result;
pub use intents::Intents;
pub use models::gateway::Ready;
pub use signature::HEADER_SIGNATURE;
pub use signature::HEADER_TIMESTAMP;
pub use signature::generate;
pub use signature::verify;
pub use webhook::dispatch_ack;
pub use webhook::handle_http_callback;
pub use webhook::heartbeat_ack;
pub use webhook::validation_ack;
pub use models::*;

Modules§

client
Main client implementation for the QQ Guild Bot API.
error
Error types for the BotRS library.
forum
Forum-related functionality for QQ Bot
gateway
WebSocket gateway implementation for the QQ Guild Bot API.
http
HTTP client implementation for the QQ Guild Bot API.
intents
Intent flags for controlling which events the bot receives.
interaction
Interaction-related functionality for QQ Bot
manage
Management event functionality for QQ Bot
models
Data models for the BotRS library.
session_manager
Websocket session management helpers.
signature
Ed25519 signature helpers for interaction callbacks.
webhook
Interaction webhook helpers.

Structs§

Audio
Audio event data structure
AudioControl
Audio control structure for managing audio playback
BotApi
Bot API client for the QQ Guild Bot API.
MessageReaction
Message reaction DTO.
MessageReactionPager
Pager for message reaction users.
PublicAudio
Public audio event data structure for live channels
Reaction
Reaction structure representing emoji reactions to messages or posts
ReactionEmoji
Emoji structure for reactions
ReactionTarget
Reaction target structure
ReactionUsers
Reaction users response structure
Token
Represents the authentication token for a QQ Guild Bot.

Enums§

AudioStatus
Audio status enumeration
PublicAudioType
Public audio channel type
ReactionTargetType
Reaction target type enumeration

Constants§

DEFAULT_API_URL
Default API base URL for QQ Guild API
DEFAULT_TIMEOUT
Default timeout for HTTP requests in seconds
DEFAULT_WS_URL
Default WebSocket URL for QQ Guild API
SANDBOX_API_URL
Sandbox API base URL for testing
VERSION
The current version of the library

Functions§

start_access_token_refresh