slashy

Crates.ioslashy
lib.rsslashy
version0.1.2
sourcesrc
created_at2021-05-31 19:50:20.456075
updated_at2023-02-01 18:47:59.680948
descriptionAn alternative command framework for serenity
homepagehttps://github.com/maddymakesgames/slashy
repositoryhttps://github.com/maddymakesgames/slashy
max_upload_size
id404423
size66,732
Madeline (maddymakesgames)

documentation

README

Slashy

This is a wip command frame work for Serenity that allows commands to be registered for both normal text commands and Discord's new slash commands

Instead of taking Serenity's attribute style of command registering, we register commands via a tree of possible arguments with different subcommands being executed at different branches.

Basic Command

use slashy::{
    command,
    commands::CommandResult,
    framework::{CommandContext, Framework},
    serenity::{prelude::GatewayIntents, Client},
    settings::Settings,
    subcommand,
};

command! {
    ping,
    "ping pong",
    pong,
    [
        optional String text | "text to echo"
    ]
}

#[subcommand]
async fn pong(ctx: &CommandContext) -> CommandResult {
    ctx.send_str(&format!("pong {:?}", ctx.get_arg("text")))
        .await?;
    Ok(())
}

#[tokio::main]
async fn main() {
    let token = std::env::var("DISCORD_TOKEN").expect("token");
    let app_id = std::env::var("APPLICATION_ID")
        .expect("app_id")
        .parse()
        .expect("app_id parse");

    // Create the slashy framework
    let settings = Settings {
        prefixes: vec!["!"],
        auto_register: true,
        auto_delete: true,
        slash_command_guilds: vec![],
    };
    let framework = Framework::new(settings, app_id, token.clone())
        .await
        .command::<PING_COMMAND>();

    // Login with a bot token from the environment
    let mut client = Client::builder(
        token,
        GatewayIntents::GUILD_MESSAGES | GatewayIntents::MESSAGE_CONTENT,
    )
    .event_handler(framework)
    .await
    .expect("Error creating client");

    // start listening for events by starting a single shard
    if let Err(why) = client.start().await {
        println!("An error occurred while running the client: {why:?}");
    }
}

Commit count: 37

cargo fmt