[ci skip] use functions instead of macros
This commit is contained in:
@@ -2,7 +2,7 @@
|
||||
name = "singh3"
|
||||
version = "0.1.0"
|
||||
authors = [ "Amneesh Singh <natto@weirdnatto.in>" ]
|
||||
edition = "2018"
|
||||
edition = "2021"
|
||||
|
||||
[dependencies]
|
||||
tracing = "*"
|
||||
|
@@ -55,6 +55,12 @@ jobs:
|
||||
passed: [configure-self]
|
||||
- get: rust-latest-image
|
||||
trigger: false
|
||||
- task: format
|
||||
config:
|
||||
run:
|
||||
path: cargo
|
||||
args: ["fmt"]
|
||||
dir: singh3-bin
|
||||
- task: build
|
||||
image: rust-latest-image
|
||||
config:
|
||||
|
@@ -30,7 +30,7 @@
|
||||
nativeBuildInputs = with pkgs; [
|
||||
rust-bin.nightly.latest.default
|
||||
];
|
||||
cargoSha256 = "sha256-0Apd8a9IdQJ8Mj5Xvm1/wM0PSc7PgciIptmz/KGx8XM=";
|
||||
cargoSha256 = "sha256-04yTexSkFpa3KQKVvfi7NM1j4V7m08kHDqw98bxXT5M=";
|
||||
};
|
||||
}
|
||||
);
|
||||
|
@@ -1,14 +1,11 @@
|
||||
use crate::commands::macros::*;
|
||||
use crate::lib::components::make_terminal_components;
|
||||
use core::time::Duration;
|
||||
use serenity::{
|
||||
builder::CreateEmbed,
|
||||
collector::component_interaction_collector::ComponentInteractionCollectorBuilder,
|
||||
framework::standard::{macros::command, Args, CommandResult},
|
||||
futures::StreamExt,
|
||||
model::{
|
||||
channel::ReactionType,
|
||||
interactions::{message_component::ButtonStyle, InteractionResponseType},
|
||||
prelude::*,
|
||||
},
|
||||
model::{interactions::InteractionResponseType, prelude::*},
|
||||
prelude::*,
|
||||
utils::Colour,
|
||||
};
|
||||
@@ -182,23 +179,21 @@ pub async fn cedit(ctx: &Context, msg: &Message, args: Args) -> CommandResult {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
macro_rules! make_embed {
|
||||
($e: expr, $cur: expr, $group: expr) => {{
|
||||
$e = $e
|
||||
.title(format!("List of words: Page {}", $cur))
|
||||
fn make_list_embed(cur: usize, group: &[Row]) -> CreateEmbed {
|
||||
let mut e = CreateEmbed::default();
|
||||
e.title(format!("List of words: Page {}", cur))
|
||||
.color(Colour::TEAL);
|
||||
for row in $group {
|
||||
for row in group {
|
||||
let idx: i64 = row.get(0);
|
||||
let name: String = row.get(1);
|
||||
let owner_id: String = row.get(2);
|
||||
$e = $e.field(
|
||||
e.field(
|
||||
format!("{}. {}", idx, name),
|
||||
format!(" by <@{}>", owner_id),
|
||||
false,
|
||||
);
|
||||
}
|
||||
$e
|
||||
}};
|
||||
e
|
||||
}
|
||||
|
||||
#[command]
|
||||
@@ -236,15 +231,17 @@ pub async fn clist(ctx: &Context, msg: &Message, mut args: Args) -> CommandResul
|
||||
let message = msg
|
||||
.channel_id
|
||||
.send_message(ctx, |m| {
|
||||
m.embed(|mut e| make_embed!(e, cur, groups[cur - 1]))
|
||||
.components(|c| make_terminal_components!(c, "first", groups.len()))
|
||||
m.set_embed(make_list_embed(cur, groups[cur - 1]))
|
||||
.set_components(make_terminal_components("first", groups.len()))
|
||||
})
|
||||
.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 {
|
||||
match interaction.data.custom_id.as_ref() {
|
||||
"next" => {
|
||||
@@ -254,14 +251,11 @@ pub async fn clist(ctx: &Context, msg: &Message, mut args: Args) -> CommandResul
|
||||
.create_interaction_response(&ctx, |r| {
|
||||
r.kind(InteractionResponseType::UpdateMessage)
|
||||
.interaction_response_data(|m| {
|
||||
m.create_embed(|mut e| make_embed!(e, cur, groups[cur - 1]))
|
||||
.components(|c| {
|
||||
make_terminal_components!(
|
||||
c,
|
||||
m.add_embed(make_list_embed(cur, groups[cur - 1]))
|
||||
.set_components(make_terminal_components(
|
||||
if cur == groups.len() { "last" } else { "mid" },
|
||||
groups.len()
|
||||
)
|
||||
})
|
||||
groups.len(),
|
||||
))
|
||||
})
|
||||
})
|
||||
.await;
|
||||
@@ -274,14 +268,11 @@ pub async fn clist(ctx: &Context, msg: &Message, mut args: Args) -> CommandResul
|
||||
.create_interaction_response(&ctx, |r| {
|
||||
r.kind(InteractionResponseType::UpdateMessage)
|
||||
.interaction_response_data(|m| {
|
||||
m.create_embed(|mut e| make_embed!(e, cur, groups[cur - 1]))
|
||||
.components(|c| {
|
||||
make_terminal_components!(
|
||||
c,
|
||||
m.add_embed(make_list_embed(cur, groups[cur - 1]))
|
||||
.set_components(make_terminal_components(
|
||||
if cur == 1 { "first" } else { "mid" },
|
||||
groups.len()
|
||||
)
|
||||
})
|
||||
groups.len(),
|
||||
))
|
||||
})
|
||||
})
|
||||
.await;
|
||||
@@ -293,10 +284,8 @@ pub async fn clist(ctx: &Context, msg: &Message, mut args: Args) -> CommandResul
|
||||
.create_interaction_response(&ctx, |r| {
|
||||
r.kind(InteractionResponseType::UpdateMessage)
|
||||
.interaction_response_data(|m| {
|
||||
m.create_embed(|mut e| make_embed!(e, cur, groups[cur - 1]))
|
||||
.components(|c| {
|
||||
make_terminal_components!(c, "first", groups.len())
|
||||
})
|
||||
m.add_embed(make_list_embed(cur, groups[cur - 1]))
|
||||
.set_components(make_terminal_components("first", groups.len()))
|
||||
})
|
||||
})
|
||||
.await;
|
||||
@@ -307,10 +296,8 @@ pub async fn clist(ctx: &Context, msg: &Message, mut args: Args) -> CommandResul
|
||||
.create_interaction_response(&ctx, |r| {
|
||||
r.kind(InteractionResponseType::UpdateMessage)
|
||||
.interaction_response_data(|m| {
|
||||
m.create_embed(|mut e| make_embed!(e, cur, groups[cur - 1]))
|
||||
.components(|c| {
|
||||
make_terminal_components!(c, "last", groups.len())
|
||||
})
|
||||
m.add_embed(make_list_embed(cur, groups[cur - 1]))
|
||||
.set_components(make_terminal_components("last", groups.len()))
|
||||
})
|
||||
})
|
||||
.await;
|
||||
@@ -325,10 +312,8 @@ pub async fn clist(ctx: &Context, msg: &Message, mut args: Args) -> CommandResul
|
||||
.create_interaction_response(&ctx, |r| {
|
||||
r.kind(InteractionResponseType::UpdateMessage)
|
||||
.interaction_response_data(|m| {
|
||||
m.create_embed(|mut e| make_embed!(e, cur, groups[cur - 1]))
|
||||
.components(|c| {
|
||||
make_terminal_components!(
|
||||
c,
|
||||
m.add_embed(make_list_embed(cur, groups[cur - 1]))
|
||||
.set_components(make_terminal_components(
|
||||
if cur == 1 {
|
||||
"first"
|
||||
} else if cur == groups.len() {
|
||||
@@ -336,9 +321,8 @@ pub async fn clist(ctx: &Context, msg: &Message, mut args: Args) -> CommandResul
|
||||
} else {
|
||||
"mid"
|
||||
},
|
||||
groups.len()
|
||||
)
|
||||
})
|
||||
groups.len(),
|
||||
))
|
||||
})
|
||||
})
|
||||
.await;
|
||||
|
@@ -1,9 +1,6 @@
|
||||
use serenity::prelude::*;
|
||||
use serenity::framework::standard::{macros::command, CommandResult};
|
||||
use serenity::model::prelude::*;
|
||||
use serenity::framework::standard::{
|
||||
CommandResult,
|
||||
macros::command
|
||||
};
|
||||
use serenity::prelude::*;
|
||||
|
||||
#[command]
|
||||
pub async fn ping(ctx: &Context, msg: &Message) -> CommandResult {
|
||||
|
@@ -1,57 +0,0 @@
|
||||
macro_rules! make_range_select_menu {
|
||||
($sm:expr, $f: expr, $l: expr) => {{
|
||||
$sm.custom_id("range")
|
||||
.placeholder("Page No")
|
||||
.options(|mut os| {
|
||||
for x in $f..=$l {
|
||||
os = os.create_option(|o| o.label(x).value(x));
|
||||
}
|
||||
|
||||
os
|
||||
})
|
||||
}};
|
||||
}
|
||||
|
||||
macro_rules! make_terminal_components {
|
||||
($c: expr, $terminal: expr, $pages: expr ) => {{
|
||||
$c.create_action_row(|ar| {
|
||||
ar.create_button(|b| {
|
||||
b.style(ButtonStyle::Primary)
|
||||
.label("First")
|
||||
.emoji(ReactionType::Unicode("\u{23EA}".to_string()))
|
||||
.custom_id("first")
|
||||
.disabled($terminal == "first")
|
||||
})
|
||||
.create_button(|b| {
|
||||
b.style(ButtonStyle::Primary)
|
||||
.label("Prev")
|
||||
.emoji(ReactionType::Unicode("\u{25C0}".to_string()))
|
||||
.custom_id("prev")
|
||||
.disabled($terminal == "first")
|
||||
})
|
||||
.create_button(|b| {
|
||||
b.style(ButtonStyle::Primary)
|
||||
.label("Next")
|
||||
.emoji(ReactionType::Unicode("\u{25B6}".to_string()))
|
||||
.custom_id("next")
|
||||
.disabled($terminal == "last")
|
||||
})
|
||||
.create_button(|b| {
|
||||
b.style(ButtonStyle::Primary)
|
||||
.label("Last")
|
||||
.emoji(ReactionType::Unicode("\u{23E9}".to_string()))
|
||||
.custom_id("last")
|
||||
.disabled($terminal == "last")
|
||||
})
|
||||
.create_button(|b| {
|
||||
b.style(ButtonStyle::Danger)
|
||||
.label("Delete")
|
||||
.emoji(ReactionType::Unicode("\u{1F5D1}".to_string()))
|
||||
.custom_id("delete")
|
||||
})
|
||||
})
|
||||
.create_action_row(|ar| ar.create_select_menu(|sm| make_range_select_menu!(sm, 1, $pages)))
|
||||
}};
|
||||
}
|
||||
pub(crate) use make_range_select_menu;
|
||||
pub(crate) use make_terminal_components;
|
@@ -2,4 +2,3 @@ pub mod count;
|
||||
pub mod general;
|
||||
pub mod minigames;
|
||||
pub mod tags;
|
||||
pub mod macros;
|
||||
|
@@ -1,14 +1,11 @@
|
||||
use crate::commands::macros::*;
|
||||
use crate::lib::components::make_terminal_components;
|
||||
use core::time::Duration;
|
||||
use serenity::{
|
||||
builder::CreateEmbed,
|
||||
collector::component_interaction_collector::ComponentInteractionCollectorBuilder,
|
||||
framework::standard::{macros::command, Args, CommandResult},
|
||||
futures::StreamExt,
|
||||
model::{
|
||||
channel::ReactionType,
|
||||
interactions::{message_component::ButtonStyle, InteractionResponseType},
|
||||
prelude::*,
|
||||
},
|
||||
model::{interactions::InteractionResponseType, prelude::*},
|
||||
prelude::*,
|
||||
utils::Colour,
|
||||
};
|
||||
@@ -222,23 +219,21 @@ pub async fn tedit(ctx: &Context, msg: &Message, args: Args) -> CommandResult {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
macro_rules! make_embed {
|
||||
($e: expr, $cur: expr, $group: expr) => {{
|
||||
$e = $e
|
||||
.title(format!("List of tags: Page {}", $cur))
|
||||
fn make_list_embed(cur: usize, group: &[Row]) -> CreateEmbed {
|
||||
let mut e = CreateEmbed::default();
|
||||
e.title(format!("List of tags: Page {}", cur))
|
||||
.color(Colour::FABLED_PINK);
|
||||
for row in $group {
|
||||
for row in group {
|
||||
let idx: i64 = row.get(0);
|
||||
let name: String = row.get(1);
|
||||
let owner_id: String = row.get(2);
|
||||
$e = $e.field(
|
||||
e.field(
|
||||
format!("{}. {}", idx, name),
|
||||
format!(" by <@{}>", owner_id),
|
||||
false,
|
||||
);
|
||||
}
|
||||
$e
|
||||
}};
|
||||
e
|
||||
}
|
||||
|
||||
#[command]
|
||||
@@ -276,10 +271,11 @@ pub async fn tlist(ctx: &Context, msg: &Message, mut args: Args) -> CommandResul
|
||||
let message = msg
|
||||
.channel_id
|
||||
.send_message(ctx, |m| {
|
||||
m.embed(|mut e| make_embed!(e, cur, groups[cur - 1]))
|
||||
.components(|c| make_terminal_components!(c, "first", groups.len()))
|
||||
m.set_embed(make_list_embed(cur, groups[cur - 1]))
|
||||
.set_components(make_terminal_components("first", groups.len()))
|
||||
})
|
||||
.await?;
|
||||
|
||||
let mut collector = ComponentInteractionCollectorBuilder::new(&ctx)
|
||||
.timeout(Duration::from_secs(90))
|
||||
.author_id(msg.author.id)
|
||||
@@ -287,8 +283,7 @@ pub async fn tlist(ctx: &Context, msg: &Message, mut args: Args) -> CommandResul
|
||||
.await;
|
||||
|
||||
while let Some(interaction) = collector.next().await {
|
||||
let custom_id = interaction.data.custom_id.as_ref();
|
||||
match custom_id {
|
||||
match interaction.data.custom_id.as_ref() {
|
||||
"next" => {
|
||||
if cur != groups.len() {
|
||||
cur += 1;
|
||||
@@ -296,14 +291,11 @@ pub async fn tlist(ctx: &Context, msg: &Message, mut args: Args) -> CommandResul
|
||||
.create_interaction_response(&ctx, |r| {
|
||||
r.kind(InteractionResponseType::UpdateMessage)
|
||||
.interaction_response_data(|m| {
|
||||
m.create_embed(|mut e| make_embed!(e, cur, groups[cur - 1]))
|
||||
.components(|c| {
|
||||
make_terminal_components!(
|
||||
c,
|
||||
m.add_embed(make_list_embed(cur, groups[cur - 1]))
|
||||
.set_components(make_terminal_components(
|
||||
if cur == groups.len() { "last" } else { "mid" },
|
||||
groups.len()
|
||||
)
|
||||
})
|
||||
groups.len(),
|
||||
))
|
||||
})
|
||||
})
|
||||
.await;
|
||||
@@ -316,14 +308,11 @@ pub async fn tlist(ctx: &Context, msg: &Message, mut args: Args) -> CommandResul
|
||||
.create_interaction_response(&ctx, |r| {
|
||||
r.kind(InteractionResponseType::UpdateMessage)
|
||||
.interaction_response_data(|m| {
|
||||
m.create_embed(|mut e| make_embed!(e, cur, groups[cur - 1]))
|
||||
.components(|c| {
|
||||
make_terminal_components!(
|
||||
c,
|
||||
m.add_embed(make_list_embed(cur, groups[cur - 1]))
|
||||
.set_components(make_terminal_components(
|
||||
if cur == 1 { "first" } else { "mid" },
|
||||
groups.len()
|
||||
)
|
||||
})
|
||||
groups.len(),
|
||||
))
|
||||
})
|
||||
})
|
||||
.await;
|
||||
@@ -335,10 +324,8 @@ pub async fn tlist(ctx: &Context, msg: &Message, mut args: Args) -> CommandResul
|
||||
.create_interaction_response(&ctx, |r| {
|
||||
r.kind(InteractionResponseType::UpdateMessage)
|
||||
.interaction_response_data(|m| {
|
||||
m.create_embed(|mut e| make_embed!(e, cur, groups[cur - 1]))
|
||||
.components(|c| {
|
||||
make_terminal_components!(c, "first", groups.len())
|
||||
})
|
||||
m.add_embed(make_list_embed(cur, groups[cur - 1]))
|
||||
.set_components(make_terminal_components("first", groups.len()))
|
||||
})
|
||||
})
|
||||
.await;
|
||||
@@ -349,10 +336,8 @@ pub async fn tlist(ctx: &Context, msg: &Message, mut args: Args) -> CommandResul
|
||||
.create_interaction_response(&ctx, |r| {
|
||||
r.kind(InteractionResponseType::UpdateMessage)
|
||||
.interaction_response_data(|m| {
|
||||
m.create_embed(|mut e| make_embed!(e, cur, groups[cur - 1]))
|
||||
.components(|c| {
|
||||
make_terminal_components!(c, "last", groups.len())
|
||||
})
|
||||
m.add_embed(make_list_embed(cur, groups[cur - 1]))
|
||||
.set_components(make_terminal_components("last", groups.len()))
|
||||
})
|
||||
})
|
||||
.await;
|
||||
@@ -367,10 +352,8 @@ pub async fn tlist(ctx: &Context, msg: &Message, mut args: Args) -> CommandResul
|
||||
.create_interaction_response(&ctx, |r| {
|
||||
r.kind(InteractionResponseType::UpdateMessage)
|
||||
.interaction_response_data(|m| {
|
||||
m.create_embed(|mut e| make_embed!(e, cur, groups[cur - 1]))
|
||||
.components(|c| {
|
||||
make_terminal_components!(
|
||||
c,
|
||||
m.add_embed(make_list_embed(cur, groups[cur - 1]))
|
||||
.set_components(make_terminal_components(
|
||||
if cur == 1 {
|
||||
"first"
|
||||
} else if cur == groups.len() {
|
||||
@@ -378,9 +361,8 @@ pub async fn tlist(ctx: &Context, msg: &Message, mut args: Args) -> CommandResul
|
||||
} else {
|
||||
"mid"
|
||||
},
|
||||
groups.len()
|
||||
)
|
||||
})
|
||||
groups.len(),
|
||||
))
|
||||
})
|
||||
})
|
||||
.await;
|
||||
|
60
src/lib/components.rs
Normal file
60
src/lib/components.rs
Normal file
@@ -0,0 +1,60 @@
|
||||
use serenity::{
|
||||
builder::{CreateComponents, CreateSelectMenu},
|
||||
model::{channel::ReactionType, interactions::message_component::ButtonStyle},
|
||||
};
|
||||
|
||||
pub fn make_range_select_menu(first: usize, last: usize) -> CreateSelectMenu {
|
||||
let mut sm = CreateSelectMenu::default();
|
||||
sm.custom_id("range")
|
||||
.placeholder("Page No")
|
||||
.options(|mut os| {
|
||||
for x in first..=last {
|
||||
os = os.create_option(|o| o.label(x).value(x));
|
||||
}
|
||||
|
||||
os
|
||||
});
|
||||
sm
|
||||
}
|
||||
|
||||
pub fn make_terminal_components(terminal: &str, pages: usize) -> CreateComponents {
|
||||
let mut c = CreateComponents::default();
|
||||
c.create_action_row(|ar| {
|
||||
ar.create_button(|b| {
|
||||
b.style(ButtonStyle::Primary)
|
||||
.label("First")
|
||||
.emoji(ReactionType::Unicode("\u{23EA}".to_string()))
|
||||
.custom_id("first")
|
||||
.disabled(terminal == "first")
|
||||
})
|
||||
.create_button(|b| {
|
||||
b.style(ButtonStyle::Primary)
|
||||
.label("Prev")
|
||||
.emoji(ReactionType::Unicode("\u{25C0}".to_string()))
|
||||
.custom_id("prev")
|
||||
.disabled(terminal == "first")
|
||||
})
|
||||
.create_button(|b| {
|
||||
b.style(ButtonStyle::Primary)
|
||||
.label("Next")
|
||||
.emoji(ReactionType::Unicode("\u{25B6}".to_string()))
|
||||
.custom_id("next")
|
||||
.disabled(terminal == "last")
|
||||
})
|
||||
.create_button(|b| {
|
||||
b.style(ButtonStyle::Primary)
|
||||
.label("Last")
|
||||
.emoji(ReactionType::Unicode("\u{23E9}".to_string()))
|
||||
.custom_id("last")
|
||||
.disabled(terminal == "last")
|
||||
})
|
||||
.create_button(|b| {
|
||||
b.style(ButtonStyle::Danger)
|
||||
.label("Delete")
|
||||
.emoji(ReactionType::Unicode("\u{1F5D1}".to_string()))
|
||||
.custom_id("delete")
|
||||
})
|
||||
})
|
||||
.create_action_row(|ar| ar.add_select_menu(make_range_select_menu(1, pages)));
|
||||
c
|
||||
}
|
1
src/lib/mod.rs
Normal file
1
src/lib/mod.rs
Normal file
@@ -0,0 +1 @@
|
||||
pub mod components;
|
@@ -1,5 +1,6 @@
|
||||
mod commands;
|
||||
mod handler;
|
||||
mod lib;
|
||||
use commands::count::*;
|
||||
use commands::general::*;
|
||||
use commands::minigames::*;
|
||||
|
Reference in New Issue
Block a user