Skip to content

YinMo19/botrs

BotRS - Rust QQ Bot Framework

Author: YinMo19

License: MIT Rust GitHub Crates.io

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

BotRS 是一个用 Rust 实现的 QQ 机器人框架,基于 QQ 频道机器人 API 与当前开放平台接口,覆盖频道、私信、群和 C2C 消息场景。它提供类型安全、异步、session-first 的接口来开发 QQ 机器人。

快速开始

安装

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

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

基础示例

use botrs::{ChannelReplySession, Client, EventHandler, Intents, ReadySession, Token};
use tracing::info;

struct MyBot;

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

    async fn message_create(&self, mut session: ChannelReplySession) {
        let message = session.message().clone();
        if message.author.bot {
            return;
        }

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

            if let Err(e) = session.reply("Pong! 🏓").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::new().with_public_guild_messages();

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

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

    Ok(())
}

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

About

用 Rust 实现的 QQ 官方机器人框架

Topics

Resources

License

Code of conduct

Contributing

Stars

Watchers

Forks

Packages

 
 
 

Contributors

Languages