added embed scroll in count: ls
This commit is contained in:
		@@ -1,9 +1,17 @@
 | 
				
			|||||||
 | 
					use core::time::Duration;
 | 
				
			||||||
use serenity::{
 | 
					use serenity::{
 | 
				
			||||||
 | 
					    collector::component_interaction_collector::ComponentInteractionCollectorBuilder,
 | 
				
			||||||
    framework::standard::{macros::command, Args, CommandResult},
 | 
					    framework::standard::{macros::command, Args, CommandResult},
 | 
				
			||||||
    model::prelude::*,
 | 
					    futures::StreamExt,
 | 
				
			||||||
 | 
					    model::{
 | 
				
			||||||
 | 
					        channel::ReactionType,
 | 
				
			||||||
 | 
					        interactions::{ButtonStyle, InteractionData},
 | 
				
			||||||
 | 
					        prelude::*,
 | 
				
			||||||
 | 
					    },
 | 
				
			||||||
    prelude::*,
 | 
					    prelude::*,
 | 
				
			||||||
    utils::Colour,
 | 
					    utils::Colour,
 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
 | 
					use tokio_postgres::Row;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#[command]
 | 
					#[command]
 | 
				
			||||||
pub async fn kitna(ctx: &Context, msg: &Message, args: Args) -> CommandResult {
 | 
					pub async fn kitna(ctx: &Context, msg: &Message, args: Args) -> CommandResult {
 | 
				
			||||||
@@ -24,16 +32,14 @@ pub async fn kitna(ctx: &Context, msg: &Message, args: Args) -> CommandResult {
 | 
				
			|||||||
            format!("select name from words where '{}' ~ reg", query).as_str(),
 | 
					            format!("select name from words where '{}' ~ reg", query).as_str(),
 | 
				
			||||||
            &[],
 | 
					            &[],
 | 
				
			||||||
        )
 | 
					        )
 | 
				
			||||||
        .await
 | 
					        .await?;
 | 
				
			||||||
        .expect("helper query to select count failed");
 | 
					 | 
				
			||||||
    if query_helper.is_empty() {
 | 
					    if query_helper.is_empty() {
 | 
				
			||||||
        query_helper = db
 | 
					        query_helper = db
 | 
				
			||||||
            .query(
 | 
					            .query(
 | 
				
			||||||
                format!("select name from words where name='{}'", query).as_str(),
 | 
					                format!("select name from words where name='{}'", query).as_str(),
 | 
				
			||||||
                &[],
 | 
					                &[],
 | 
				
			||||||
            )
 | 
					            )
 | 
				
			||||||
            .await
 | 
					            .await?;
 | 
				
			||||||
            .expect("helper query to select count failed");
 | 
					 | 
				
			||||||
        if query_helper.is_empty() {
 | 
					        if query_helper.is_empty() {
 | 
				
			||||||
            msg.reply(
 | 
					            msg.reply(
 | 
				
			||||||
                ctx,
 | 
					                ctx,
 | 
				
			||||||
@@ -58,8 +64,7 @@ pub async fn kitna(ctx: &Context, msg: &Message, args: Args) -> CommandResult {
 | 
				
			|||||||
                format!("select count from user{} where name='{}'", id, name).as_str(),
 | 
					                format!("select count from user{} where name='{}'", id, name).as_str(),
 | 
				
			||||||
                &[],
 | 
					                &[],
 | 
				
			||||||
            )
 | 
					            )
 | 
				
			||||||
            .await
 | 
					            .await?
 | 
				
			||||||
            .expect("cant select the count")
 | 
					 | 
				
			||||||
            .get(0);
 | 
					            .get(0);
 | 
				
			||||||
        reply = reply + &format!("\n{} count for you: {}", name, query_result);
 | 
					        reply = reply + &format!("\n{} count for you: {}", name, query_result);
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
@@ -196,6 +201,46 @@ pub async fn change(ctx: &Context, msg: &Message, args: Args) -> CommandResult {
 | 
				
			|||||||
    Ok(())
 | 
					    Ok(())
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					macro_rules! make_embed {
 | 
				
			||||||
 | 
					    ($e: expr, $cur: expr, $group: expr) => {{
 | 
				
			||||||
 | 
					        $e = $e
 | 
				
			||||||
 | 
					            .title(format!("List of words: Page {}", $cur))
 | 
				
			||||||
 | 
					            .color(Colour::TEAL);
 | 
				
			||||||
 | 
					        for row in $group {
 | 
				
			||||||
 | 
					            let idx: i32 = row.get(0);
 | 
				
			||||||
 | 
					            let name: String = row.get(1);
 | 
				
			||||||
 | 
					            let owner_id: String = row.get(3);
 | 
				
			||||||
 | 
					            $e = $e.field(
 | 
				
			||||||
 | 
					                format!("{}. {}", idx, name),
 | 
				
			||||||
 | 
					                format!(" by <@{}>", owner_id),
 | 
				
			||||||
 | 
					                true,
 | 
				
			||||||
 | 
					            );
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					        $e
 | 
				
			||||||
 | 
					    }};
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					macro_rules! make_terminal_components {
 | 
				
			||||||
 | 
					    ($c: expr, $terminal: expr ) => {{
 | 
				
			||||||
 | 
					        $c.create_action_row(|ar| {
 | 
				
			||||||
 | 
					            ar.create_button(|b| {
 | 
				
			||||||
 | 
					                b.style(ButtonStyle::Primary)
 | 
				
			||||||
 | 
					                    .label("Prev")
 | 
				
			||||||
 | 
					                    .emoji(ReactionType::Unicode("\u{2B05}".to_string()))
 | 
				
			||||||
 | 
					                    .custom_id("prev")
 | 
				
			||||||
 | 
					                    .disabled($terminal == "first")
 | 
				
			||||||
 | 
					            })
 | 
				
			||||||
 | 
					            .create_button(|b| {
 | 
				
			||||||
 | 
					                b.style(ButtonStyle::Primary)
 | 
				
			||||||
 | 
					                    .label("Next")
 | 
				
			||||||
 | 
					                    .emoji(ReactionType::Unicode("\u{27A1}".to_string()))
 | 
				
			||||||
 | 
					                    .custom_id("next")
 | 
				
			||||||
 | 
					                    .disabled($terminal == "last")
 | 
				
			||||||
 | 
					            })
 | 
				
			||||||
 | 
					        })
 | 
				
			||||||
 | 
					    }};
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#[command]
 | 
					#[command]
 | 
				
			||||||
pub async fn ls(ctx: &Context, msg: &Message, _: Args) -> CommandResult {
 | 
					pub async fn ls(ctx: &Context, msg: &Message, _: Args) -> CommandResult {
 | 
				
			||||||
    let data_read = ctx.data.read().await;
 | 
					    let data_read = ctx.data.read().await;
 | 
				
			||||||
@@ -208,31 +253,69 @@ pub async fn ls(ctx: &Context, msg: &Message, _: Args) -> CommandResult {
 | 
				
			|||||||
        msg.reply(ctx, "No words stored").await?;
 | 
					        msg.reply(ctx, "No words stored").await?;
 | 
				
			||||||
        return Ok(());
 | 
					        return Ok(());
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    msg.channel_id
 | 
					    let groups: Vec<&[Row]> = rows.chunks(5).collect();
 | 
				
			||||||
        .send_message(ctx, |mut m| {
 | 
					    let mut cur = 1;
 | 
				
			||||||
            let mut a: u32 = 1;
 | 
					
 | 
				
			||||||
            for group in rows.chunks(5) {
 | 
					    let message = msg
 | 
				
			||||||
                m = m.embed(|mut e| {
 | 
					        .channel_id
 | 
				
			||||||
                    e = e
 | 
					        .send_message(ctx, |m| {
 | 
				
			||||||
                        .title(format!("List of words: Page {}", a))
 | 
					            m.embed(|mut e| make_embed!(e, cur, groups[cur - 1]))
 | 
				
			||||||
                        .color(Colour::TEAL);
 | 
					                .components(|c| make_terminal_components!(c, "first"))
 | 
				
			||||||
                    a += 1;
 | 
					 | 
				
			||||||
                    for row in group {
 | 
					 | 
				
			||||||
                        let idx: i32 = row.get(0);
 | 
					 | 
				
			||||||
                        let name: String = row.get(1);
 | 
					 | 
				
			||||||
                        let _reg: String = row.get(2);
 | 
					 | 
				
			||||||
                        let owner_id: String = row.get(3);
 | 
					 | 
				
			||||||
                        e = e.field(
 | 
					 | 
				
			||||||
                            format!("{}. {}", idx, name),
 | 
					 | 
				
			||||||
                            format!(" by <@{}>", owner_id),
 | 
					 | 
				
			||||||
                            true,
 | 
					 | 
				
			||||||
                        );
 | 
					 | 
				
			||||||
                    }
 | 
					 | 
				
			||||||
                    e
 | 
					 | 
				
			||||||
                })
 | 
					 | 
				
			||||||
            }
 | 
					 | 
				
			||||||
            m
 | 
					 | 
				
			||||||
        })
 | 
					        })
 | 
				
			||||||
        .await?;
 | 
					        .await?;
 | 
				
			||||||
 | 
					    let mut collector = ComponentInteractionCollectorBuilder::new(&ctx)
 | 
				
			||||||
 | 
					        .timeout(Duration::from_secs(90))
 | 
				
			||||||
 | 
					        .author_id(msg.author.id)
 | 
				
			||||||
 | 
					        .message_id(message.id)
 | 
				
			||||||
 | 
					        .await;
 | 
				
			||||||
 | 
					    while let Some(interaction) = collector.next().await {
 | 
				
			||||||
 | 
					        if let InteractionData::MessageComponent(component) = interaction.data.as_ref().unwrap() {
 | 
				
			||||||
 | 
					            match component.custom_id.as_ref() {
 | 
				
			||||||
 | 
					                "next" => {
 | 
				
			||||||
 | 
					                    if cur != groups.len() {
 | 
				
			||||||
 | 
					                        let _ = interaction
 | 
				
			||||||
 | 
					                            .create_interaction_response(&ctx, |r| {
 | 
				
			||||||
 | 
					                                r.kind(InteractionResponseType::UpdateMessage)
 | 
				
			||||||
 | 
					                                    .interaction_response_data(|m| {
 | 
				
			||||||
 | 
					                                        cur += 1;
 | 
				
			||||||
 | 
					                                        m.create_embed(|mut e| make_embed!(e, cur, groups[cur - 1]))
 | 
				
			||||||
 | 
					                                            .components(|c| {
 | 
				
			||||||
 | 
					                                                make_terminal_components!(
 | 
				
			||||||
 | 
					                                                    c,
 | 
				
			||||||
 | 
					                                                    if cur == groups.len() {
 | 
				
			||||||
 | 
					                                                        "last"
 | 
				
			||||||
 | 
					                                                    } else {
 | 
				
			||||||
 | 
					                                                        "mid"
 | 
				
			||||||
 | 
					                                                    }
 | 
				
			||||||
 | 
					                                                )
 | 
				
			||||||
 | 
					                                            })
 | 
				
			||||||
 | 
					                                    })
 | 
				
			||||||
 | 
					                            })
 | 
				
			||||||
 | 
					                            .await;
 | 
				
			||||||
 | 
					                    }
 | 
				
			||||||
 | 
					                }
 | 
				
			||||||
 | 
					                "prev" => {
 | 
				
			||||||
 | 
					                    if cur != 1 {
 | 
				
			||||||
 | 
					                        let _ = interaction
 | 
				
			||||||
 | 
					                            .create_interaction_response(&ctx, |r| {
 | 
				
			||||||
 | 
					                                r.kind(InteractionResponseType::UpdateMessage)
 | 
				
			||||||
 | 
					                                    .interaction_response_data(|m| {
 | 
				
			||||||
 | 
					                                        cur -= 1;
 | 
				
			||||||
 | 
					                                        m.create_embed(|mut e| make_embed!(e, cur, groups[cur - 1]))
 | 
				
			||||||
 | 
					                                            .components(|c| {
 | 
				
			||||||
 | 
					                                                make_terminal_components!(
 | 
				
			||||||
 | 
					                                                    c,
 | 
				
			||||||
 | 
					                                                    if cur == 1 { "first" } else { "mid" }
 | 
				
			||||||
 | 
					                                                )
 | 
				
			||||||
 | 
					                                            })
 | 
				
			||||||
 | 
					                                    })
 | 
				
			||||||
 | 
					                            })
 | 
				
			||||||
 | 
					                            .await;
 | 
				
			||||||
 | 
					                    }
 | 
				
			||||||
 | 
					                }
 | 
				
			||||||
 | 
					                _ => {}
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
    Ok(())
 | 
					    Ok(())
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -39,6 +39,7 @@ struct General;
 | 
				
			|||||||
#[prefix = "count"]
 | 
					#[prefix = "count"]
 | 
				
			||||||
#[commands(kitna, add, rm, change, ls)]
 | 
					#[commands(kitna, add, rm, change, ls)]
 | 
				
			||||||
struct Count;
 | 
					struct Count;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#[group]
 | 
					#[group]
 | 
				
			||||||
#[commands(challenge)]
 | 
					#[commands(challenge)]
 | 
				
			||||||
struct Minigames;
 | 
					struct Minigames;
 | 
				
			||||||
@@ -92,7 +93,6 @@ async fn main() {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
        let db_url: String = env::var("DB_URL").expect("DB_URL not found");
 | 
					        let db_url: String = env::var("DB_URL").expect("DB_URL not found");
 | 
				
			||||||
        println!("{}", db_url);
 | 
					 | 
				
			||||||
        let (db_client, conn) = tokio_postgres::connect(&db_url, tokio_postgres::NoTls)
 | 
					        let (db_client, conn) = tokio_postgres::connect(&db_url, tokio_postgres::NoTls)
 | 
				
			||||||
            .await
 | 
					            .await
 | 
				
			||||||
            .expect("cant connect bha");
 | 
					            .expect("cant connect bha");
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user