~jojo/stalin-bot

3440e63ca816e7ce66957d7d8a6ab8a29cf63d82 — JoJo 10 days ago c2364c5
Don't play multiple "play"s over eachother & add cmd "stop"
1 files changed, 40 insertions(+), 17 deletions(-)

M src/main.rs
M src/main.rs => src/main.rs +40 -17
@@ 40,7 40,7 @@ impl EventHandler for Handler {
}

#[group]
#[commands(deafen, join, leave, mute, play, ping, undeafen, unmute)]
#[commands(help, deafen, join, leave, mute, play, stop, ping, undeafen, unmute)]
struct General;

#[tokio::main]


@@ 73,6 73,12 @@ async fn main() {
}

#[command]
async fn help(context: &Context, msg: &Message) -> CommandResult {
    check_msg(msg.channel_id.say(&context.http, "jag kan 'help', 'deafen', 'join', 'leave', 'mute', 'play <url>', 'stop', 'ping', 'undeafen', och 'unmute'").await);
    Ok(())
}

#[command]
#[only_in(guilds)]
async fn deafen(ctx: &Context, msg: &Message) -> CommandResult {
    let guild = msg.guild(&ctx.cache).await.unwrap();


@@ 222,30 228,26 @@ async fn ping(context: &Context, msg: &Message) -> CommandResult {

#[command]
#[only_in(guilds)]
async fn play(ctx: &Context, msg: &Message, mut args: Args) -> CommandResult {
async fn play(ctx: &Context, msg: &Message, args: Args) -> CommandResult {
    join_(ctx, msg).await?;
    let url = match args.single::<String>() {
        Ok(url) => url,
        Err(_) => {
    let query = match args.rest() {
        "" => {
            check_msg(
                msg.channel_id
                    .say(&ctx.http, "Must provide a URL to a video or audio")
                    .say(
                        &ctx.http,
                        "Must provide a query string for a source of video or audio",
                    )
                    .await,
            );

            return Ok(());
        }
        q if q.starts_with("http") => q.to_string(),
        q => "ytsearch:".to_string() + &q,
    };

    if !url.starts_with("http") {
        check_msg(
            msg.channel_id
                .say(&ctx.http, "Must provide a valid URL")
                .await,
        );

        return Ok(());
    }
    println!("play {}", query);

    let guild = msg.guild(&ctx.cache).await.unwrap();
    let guild_id = guild.id;


@@ 258,7 260,7 @@ async fn play(ctx: &Context, msg: &Message, mut args: Args) -> CommandResult {
    if let Some(handler_lock) = manager.get(guild_id) {
        let mut handler = handler_lock.lock().await;

        let source = match songbird::ytdl(&url).await {
        let source = match songbird::ytdl(&query).await {
            Ok(source) => source,
            Err(why) => {
                println!("Err starting source: {:?}", why);


@@ 269,7 271,7 @@ async fn play(ctx: &Context, msg: &Message, mut args: Args) -> CommandResult {
            }
        };

        handler.play_source(source);
        handler.play_only_source(source);

        check_msg(msg.channel_id.say(&ctx.http, "Playing song").await);
    } else {


@@ 285,6 287,27 @@ async fn play(ctx: &Context, msg: &Message, mut args: Args) -> CommandResult {

#[command]
#[only_in(guilds)]
async fn stop(ctx: &Context, msg: &Message) -> CommandResult {
    let guild = msg.guild(&ctx.cache).await.unwrap();
    let guild_id = guild.id;

    let manager = songbird::get(ctx)
        .await
        .expect("Songbird Voice client placed in at initialisation.")
        .clone();

    if let Some(handler_lock) = manager.get(guild_id) {
        let mut handler = handler_lock.lock().await;
        handler.stop();
        check_msg(msg.channel_id.say(&ctx.http, "Stopped playing").await);
    } else {
        check_msg(msg.channel_id.say(&ctx.http, "??? idiot").await);
    }
    Ok(())
}

#[command]
#[only_in(guilds)]
async fn undeafen(ctx: &Context, msg: &Message) -> CommandResult {
    let guild = msg.guild(&ctx.cache).await.unwrap();
    let guild_id = guild.id;