Skip to content

AS1100K/bevy-discord

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

Bevy Discord Plugin

GitHub License Crates.io Version CI

Note

The main branch is ahead working for 0.6 release

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

Basic ping-pong bot
use bevy::prelude::*;
use bevy_discord::bot::DiscordBotConfig;
use bevy_discord::events::bot::BMessage;
use bevy_discord::serenity::all::*;
use bevy_discord::DiscordPluginGroup;
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(bevy::log::LogPlugin {
            ..Default::default()
        })
        // Don't use `::new_only_bot` function in production code with feature `full` and `docsrs`
        // instead use `::new` with feature `bot`
        .add_plugins(DiscordPluginGroup::new_only_bot(config))
        .add_systems(Update, handle_messages)
        .run();
}

fn handle_messages(
    mut messages: EventReader<BMessage>,
    http: Option<Res<bevy_discord::res::DiscordHttpResource>>,
) {
    for message in messages.read() {
        if let Some(http) = &http {
            // 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

Basic Rich-presence bot
use bevy::log::tracing_subscriber::fmt::Subscriber;
use bevy::MinimalPlugins;
use bevy_app::{App, Update};
use bevy_discord::events::rich_presence::RichPresenceReady;
use bevy_discord::res::DiscordRichPresenceRes;
use bevy_discord::rich_presence::DiscordRichPresenceConfig;
use bevy_discord::{DiscordPluginGroup, DiscordSet};
use bevy_ecs::event::EventReader;
use bevy_ecs::prelude::{IntoSystemConfigs, Res};
use discord_sdk::activity::ActivityBuilder;
use discord_sdk::{OffsetDateTime, Subscriptions};

fn main() {
    // Initialize tracing subscriber
    let subscriber = Subscriber::builder()
        .with_max_level(tracing::Level::DEBUG)
        .finish();
    tracing::subscriber::set_global_default(subscriber).expect("setting default subscriber failed");

    let config = DiscordRichPresenceConfig {
        app: 1326097363395411968,
        subscriptions: Subscriptions::all(),
    };

    App::new()
        .add_plugins(MinimalPlugins)
        // Don't use `::new_only_rich_presence` function in production code with feature `full` and `docsrs`
        // instead use `::new` with feature `rich_presence`
        .add_plugins(DiscordPluginGroup::new_only_rich_presence(config))
        .add_systems(Update, rich_presence_ready.after(DiscordSet))
        .run();
}

fn rich_presence_ready(
    mut events: EventReader<RichPresenceReady>,
    rich_presence: Res<DiscordRichPresenceRes>,
) {
    for event in events.read() {
        println!(
            r#"
            version: {},
            user: {:?}
            "#,
            event.version, event.user
        );

        println!("setup_rich_presence");
        let current_date_time = OffsetDateTime::now_utc();
        let new_activity = ActivityBuilder::new()
            .state("bevy-discord")
            .details("Exploring example rich_presence.rs")
            .start_timestamp(current_date_time);

        let ds = rich_presence.discord.clone();
        bevy_discord::runtime::tokio_runtime().spawn(async move {
            let _ = ds
                .update_activity(new_activity)
                .await
                .expect("Failed to update the activity");
        });
    }
}

Example taken from examples/rich_presence.rs

Examples

The examples/ directory contains several example implementations:

To run an example:

$ cargo run --example <EXAMPLE_NAME> --features "full docsrs"

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.
rich_presence Discord Rich Presence Integration with Bevy. (v0.6)

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
0.6.x 0.?.x

Contributing

If you are planning to contribute to bevy-discord in any manner, please refer to CONTRIBUTING.md