#discord #bevy-plugin #bevy #discord-api #http-client #send-receive #plugin

bevy-discord

A bevy plugin for sending and receiving discord messages

9 unstable releases (3 breaking)

new 0.5.0 Nov 30, 2024
0.5.0-beta.1 Nov 28, 2024
0.4.0 Nov 8, 2024
0.3.1 Oct 31, 2024
0.2.0-alpha.1 Jul 13, 2024

#718 in Web programming

Download history 36/week @ 2024-08-16 5/week @ 2024-09-20 4/week @ 2024-09-27 4/week @ 2024-10-04 159/week @ 2024-10-11 23/week @ 2024-10-18 198/week @ 2024-10-25 154/week @ 2024-11-01 148/week @ 2024-11-08 28/week @ 2024-11-15 74/week @ 2024-11-22 193/week @ 2024-11-29

462 downloads per month

MIT license

61KB
1.5K SLoC

Bevy Discord Plugin

GitHub License Crates.io Version CI

A very simple, bevy plugin that let you send and receive messages through bevy events.

Installation

Add bevy-discord as your dependency:

$ cargo add bevy-discord --features full

Quick Start

// examples/basic_bot.rs
use bevy::prelude::*;
use bevy_discord::serenity::all::*;
use bevy_discord::bot::{events::BMessage, DiscordBotConfig, DiscordBotPlugin};
use serde_json::json;

fn main() {
    // Configure the bot with necessary intents
    let config = DiscordBotConfig::default()
        .token("YOUR_BOT_TOKEN_HERE".to_string())
        .gateway_intents(
            GatewayIntents::GUILD_MESSAGES
                | GatewayIntents::MESSAGE_CONTENT
                | GatewayIntents::GUILDS,
        );

    App::new()
        .add_plugins(MinimalPlugins)
        .add_plugins(DiscordBotPlugin::new(config))
        .add_systems(Update, handle_messages)
        .run();
}

fn handle_messages(
    mut messages: EventReader<BMessage>,
    http: Res<bevy_discord::http::DiscordHttpResource>,
) {
    for message in messages.read() {
        // Skip messages from bots (including our own)
        if message.new_message.author.bot {
            continue;
        }

        let content = &message.new_message.content;
        let channel_id = message.new_message.channel_id;

        // Simple ping-pong command
        if content == "!ping" {
            let http = http.client();

            bevy_discord::runtime::tokio_runtime().spawn(async move {
                let _ = http
                    .send_message(
                        channel_id,
                        vec![],
                        &json!({
                            "content": "Pong! 🏓"
                        }),
                    )
                    .await;
            });
        }
    }
}

Example taken from examples/basic_bot.rs

Examples

The examples/ directory contains several example implementations:

To run an example:

$ cargo run --example basic_bot

Note: Remember to replace YOUR_BOT_TOKEN with your actual Discord bot token.

Features

This crate using powerful cargo features.

Feature Information
bot (includes http) Discord bot integration for Bevy applications.
http HTTP Client functionality for Discord API interactions.

All features are comes under full feature.

Limitations

Currently, the following Discord/Serenity features are not supported:

Feature Module
voice bot

Versions

This crate aims to track bevy's versions. It also follows the semver standard. Below is a chart which versions of this crate are compatible with which bevy version:

Version Bevy Version
0.2.x 0.13.x
0.3.x 0.13.x
0.4.x 0.14.x
0.5.x 0.15.x

Dependencies

~12–24MB
~324K SLoC