ol_rusty/src/commands/utils.rs
Tom 5caf820dc0 Update poise version (with refactor)
set some command to ephemeral
Add roles command
Do some refactoring and minor changes to commands
2023-04-07 19:06:14 +02:00

236 lines
7.3 KiB
Rust

use std::vec;
use crate::{Context, Error};
use poise::serenity_prelude::{self as serenity, ChannelId};
pub async fn high_tier(ctx: Context<'_>) -> bool {
let ChannelId(chan_id) = ctx.channel_id();
match chan_id {
117992911781494787 => return true, // private general
332337657113739265 => return true, // Testing
_ => (),
}
match ctx.serenity_context().cache.guild_channel(ctx.channel_id()) {
None => return false,
Some(chan) => match chan.parent_id {
None => return false,
Some(cat_id) => match cat_id {
ChannelId(547551264498515978) => return true, // MS high tier
ChannelId(884698356360818708) => return true, // Private server
_ => return false,
},
},
}
}
pub fn paginator(input: Vec<String>, chunk_size: usize, join_string: String) -> Vec<String> {
if input.len() == 0 {
return vec![];
}
let mut result: Vec<String> = vec![];
let mut part: String = "".to_string();
let filler = &join_string.chars().count();
for i in input {
if part.chars().count() + i.chars().count() + filler >= chunk_size {
result.push(part);
part = i.to_string();
} else {
part.push_str(&join_string);
part.push_str(&i.to_string());
}
}
result.push(part);
return result;
}
pub async fn paginate_string(ctx: Context<'_>, pages: Vec<String>) -> Result<(), Error> {
let uuid_command = ctx.id().to_string();
let page_count = pages.len();
match pages.len() {
0 => {
ctx.say("No data found :(").await?;
return Ok(());
}
1 => {
ctx.say(pages.get(0).unwrap()).await?;
return Ok(());
}
_ => {}
};
ctx.send(|m| {
m.content(format!(
"{}\n\nPage: {}/{}",
pages.get(0).unwrap(),
1,
page_count
))
.components(|c| {
c.create_action_row(|ar| {
ar.create_button(|b| {
b.style(serenity::ButtonStyle::Primary)
.label("Previous page")
.custom_id(format!("{}_previous", uuid_command))
});
ar.create_button(|b| {
b.style(serenity::ButtonStyle::Primary)
.label("Next page")
.custom_id(format!("{}_next", uuid_command))
});
ar.create_button(|b| {
b.style(serenity::ButtonStyle::Secondary)
.label("Reset")
.custom_id(format!("{}_close", uuid_command))
})
})
})
})
.await?;
// let interaction1 = if let ReplyHandle::Application { http, interaction } = msg.unwrap(){Some(interaction)} else {None};
// let interaction = interaction1.unwrap();
let mut page = 0;
while let Some(mci) = serenity::CollectComponentInteraction::new(ctx)
// .author_id(ctx.author().id)
.channel_id(ctx.channel_id())
.timeout(std::time::Duration::from_secs(1200))
// .filter(move |mci| mci.data.custom_id == uuid_command.to_string())
.await
{
if !mci.data.custom_id.contains(&uuid_command) {
continue;
}
if mci.data.custom_id.contains("_previous") {
if page == 0 {
page = page_count - 1;
} else {
page = (page - 1) % page_count;
}
} else if mci.data.custom_id.contains("_next") {
page = (page + 1) % page_count;
} else if mci.data.custom_id.contains("_close") {
page = 0;
}
let mut msg = mci.message.clone();
msg.edit(ctx, |m| {
m.content(format!(
"{}\n\nPage: {}/{}",
pages.get(page).unwrap(),
page + 1,
page_count
))
})
.await?;
mci.create_interaction_response(ctx, |ir| {
ir.kind(serenity::InteractionResponseType::DeferredUpdateMessage)
})
.await?;
}
Ok(())
}
pub async fn paginate_string_embed(
ctx: Context<'_>,
title: String,
pages: Vec<String>,
) -> Result<(), Error> {
let uuid_command = ctx.id().to_string();
let page_count = pages.len();
match pages.len() {
0 => {
ctx.say("No data found :(").await?;
return Ok(());
}
1 => {
// ctx.say(pages.get(0).unwrap()).await?;
ctx.send(|m| m.embed(|e| e.title(title).description(pages.get(0).unwrap())))
.await?;
return Ok(());
}
_ => {}
};
ctx.send(|m| {
// m.content(format!(
// "{}\n\nPage: {}/{}",
// pages.get(0).unwrap(),
// 1,
// page_count
// ))
m.embed(|e| {
e.title(format!("{} Page 1/{}", title, page_count))
.description(pages.get(0).unwrap())
})
.components(|c| {
c.create_action_row(|ar| {
ar.create_button(|b| {
b.style(serenity::ButtonStyle::Primary)
.label("Previous page")
.custom_id(format!("{}_previous", uuid_command))
});
ar.create_button(|b| {
b.style(serenity::ButtonStyle::Primary)
.label("Next page")
.custom_id(format!("{}_next", uuid_command))
});
ar.create_button(|b| {
b.style(serenity::ButtonStyle::Secondary)
.label("Reset")
.custom_id(format!("{}_close", uuid_command))
})
})
})
})
.await?;
// let interaction1 = if let ReplyHandle::Application { http, interaction } = msg.unwrap(){Some(interaction)} else {None};
// let interaction = interaction1.unwrap();
let mut page = 0;
while let Some(mci) = serenity::CollectComponentInteraction::new(ctx)
// .author_id(ctx.author().id)
.channel_id(ctx.channel_id())
.timeout(std::time::Duration::from_secs(1200))
// .filter(move |mci| mci.data.custom_id == uuid_command.to_string())
.await
{
if !mci.data.custom_id.contains(&uuid_command) {
continue;
}
if mci.data.custom_id.contains("_previous") {
if page == 0 {
page = page_count - 1;
} else {
page = (page - 1) % page_count;
}
} else if mci.data.custom_id.contains("_next") {
page = (page + 1) % page_count;
} else if mci.data.custom_id.contains("_close") {
page = 0;
}
let mut msg = mci.message.clone();
msg.edit(ctx, |m| {
m.embed(|e| {
e.title(format!("{} Page {}/{}", title, page + 1, page_count))
.description(pages.get(page).unwrap())
})
})
.await?;
mci.create_interaction_response(ctx, |ir| {
ir.kind(serenity::InteractionResponseType::DeferredUpdateMessage)
})
.await?;
}
Ok(())
}