send command
This commit is contained in:
parent
08d9a337cc
commit
0488ce4b9b
6 changed files with 259 additions and 2 deletions
91
Cargo.lock
generated
91
Cargo.lock
generated
|
|
@ -1092,6 +1092,8 @@ dependencies = [
|
|||
"sqlx",
|
||||
"thiserror 2.0.12",
|
||||
"tokio",
|
||||
"tracing",
|
||||
"tracing-subscriber",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
|
|
@ -1220,6 +1222,16 @@ dependencies = [
|
|||
"windows-sys 0.52.0",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "nu-ansi-term"
|
||||
version = "0.46.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "77a8165726e8236064dbb45459242600304b42a5ea24ee2948e18e023bf7ba84"
|
||||
dependencies = [
|
||||
"overload",
|
||||
"winapi",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "num-bigint-dig"
|
||||
version = "0.8.4"
|
||||
|
|
@ -1288,6 +1300,12 @@ version = "1.21.3"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "42f5e15c9953c5e4ccceeb2e7382a716482c34515315f7b03532b8b4e8393d2d"
|
||||
|
||||
[[package]]
|
||||
name = "overload"
|
||||
version = "0.1.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "b15813163c1d831bf4a13c3610c05c0d03b39feb07f7e09fa234dac9b15aaf39"
|
||||
|
||||
[[package]]
|
||||
name = "parking"
|
||||
version = "2.2.1"
|
||||
|
|
@ -1841,6 +1859,15 @@ dependencies = [
|
|||
"digest",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "sharded-slab"
|
||||
version = "0.1.7"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f40ca3c46823713e0d4209592e8d6e826aa57e928f09752619fc696c499637f6"
|
||||
dependencies = [
|
||||
"lazy_static",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "shlex"
|
||||
version = "1.3.0"
|
||||
|
|
@ -2265,6 +2292,16 @@ dependencies = [
|
|||
"syn 2.0.101",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "thread_local"
|
||||
version = "1.1.8"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "8b9ef9bad013ada3808854ceac7b46812a6465ba368859a37e2100283d2d719c"
|
||||
dependencies = [
|
||||
"cfg-if",
|
||||
"once_cell",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "time"
|
||||
version = "0.3.41"
|
||||
|
|
@ -2447,6 +2484,32 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
|||
checksum = "e672c95779cf947c5311f83787af4fa8fffd12fb27e4993211a84bdfd9610f9c"
|
||||
dependencies = [
|
||||
"once_cell",
|
||||
"valuable",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "tracing-log"
|
||||
version = "0.2.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "ee855f1f400bd0e5c02d150ae5de3840039a3f54b025156404e34c23c03f47c3"
|
||||
dependencies = [
|
||||
"log",
|
||||
"once_cell",
|
||||
"tracing-core",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "tracing-subscriber"
|
||||
version = "0.3.19"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "e8189decb5ac0fa7bc8b96b7cb9b2701d60d48805aca84a238004d665fcc4008"
|
||||
dependencies = [
|
||||
"nu-ansi-term",
|
||||
"sharded-slab",
|
||||
"smallvec",
|
||||
"thread_local",
|
||||
"tracing-core",
|
||||
"tracing-log",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
|
|
@ -2592,6 +2655,12 @@ version = "1.0.4"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "b6c140620e7ffbb22c2dee59cafe6084a59b5ffc27a8859a5f0d494b5d52b6be"
|
||||
|
||||
[[package]]
|
||||
name = "valuable"
|
||||
version = "0.1.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "ba73ea9cf16a25df0c8caa16c51acb937d5712a8429db78a3ee29d5dcacd3a65"
|
||||
|
||||
[[package]]
|
||||
name = "vcpkg"
|
||||
version = "0.2.15"
|
||||
|
|
@ -2763,6 +2832,22 @@ dependencies = [
|
|||
"wasite",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "winapi"
|
||||
version = "0.3.9"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419"
|
||||
dependencies = [
|
||||
"winapi-i686-pc-windows-gnu",
|
||||
"winapi-x86_64-pc-windows-gnu",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "winapi-i686-pc-windows-gnu"
|
||||
version = "0.4.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"
|
||||
|
||||
[[package]]
|
||||
name = "winapi-util"
|
||||
version = "0.1.9"
|
||||
|
|
@ -2772,6 +2857,12 @@ dependencies = [
|
|||
"windows-sys 0.59.0",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "winapi-x86_64-pc-windows-gnu"
|
||||
version = "0.4.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"
|
||||
|
||||
[[package]]
|
||||
name = "windows-core"
|
||||
version = "0.61.0"
|
||||
|
|
|
|||
|
|
@ -10,3 +10,5 @@ poise = "0.6.1"
|
|||
sqlx = { version = "0.8", features = ["runtime-tokio"] }
|
||||
thiserror = "2.0.12"
|
||||
tokio = { version = "1", features = ["full"] }
|
||||
tracing = "0.1.41"
|
||||
tracing-subscriber = "0.3.19"
|
||||
|
|
|
|||
0
src/checks.rs
Normal file
0
src/checks.rs
Normal file
32
src/commands.rs
Normal file
32
src/commands.rs
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
use crate::prelude::*;
|
||||
|
||||
mod send;
|
||||
|
||||
use poise::Command;
|
||||
pub use send::SendTarget;
|
||||
|
||||
pub fn get() -> Vec<Command<KBotData, Error>> {
|
||||
vec![send()]
|
||||
}
|
||||
|
||||
#[poise::command(prefix_command)]
|
||||
async fn send(
|
||||
ctx: Context<'_>,
|
||||
#[description = "Send target"] target: SendTarget,
|
||||
#[rest]
|
||||
#[description = "Message to send"]
|
||||
content: String,
|
||||
) -> CommandResult {
|
||||
match target {
|
||||
SendTarget::Channel(channel) => {
|
||||
channel.say(ctx, content).await?;
|
||||
}
|
||||
SendTarget::User(user) => {
|
||||
user.create_dm_channel(ctx).await?.say(ctx, content).await?;
|
||||
}
|
||||
SendTarget::Message(message) => {
|
||||
message.reply(ctx, content).await?;
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
60
src/commands/send.rs
Normal file
60
src/commands/send.rs
Normal file
|
|
@ -0,0 +1,60 @@
|
|||
use crate::prelude::*;
|
||||
|
||||
use serenity::{GuildChannelParseError, MessageParseError, async_trait};
|
||||
|
||||
pub enum SendTarget {
|
||||
Channel(Box<serenity::GuildChannel>),
|
||||
User(Box<serenity::User>),
|
||||
Message(Box<serenity::Message>),
|
||||
}
|
||||
|
||||
#[derive(thiserror::Error, Debug)]
|
||||
pub enum SendTargetParseError {
|
||||
#[error("error during parsing as channel")]
|
||||
ChannelError(#[from] GuildChannelParseError),
|
||||
#[error("error during parsing as message")]
|
||||
MessageError(#[from] MessageParseError),
|
||||
#[error("failed to parse target")]
|
||||
Malformed,
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl serenity::ArgumentConvert for SendTarget {
|
||||
type Err = SendTargetParseError;
|
||||
|
||||
async fn convert(
|
||||
ctx: impl serenity::CacheHttp,
|
||||
guild_id: Option<serenity::GuildId>,
|
||||
channel_id: Option<serenity::ChannelId>,
|
||||
s: &str,
|
||||
) -> serenity::Result<Self, Self::Err> {
|
||||
let user_result = serenity::User::convert(&ctx, guild_id, channel_id, s).await;
|
||||
if let Ok(user) = user_result {
|
||||
return Ok(Self::User(Box::new(user)));
|
||||
}
|
||||
|
||||
let message_result = serenity::Message::convert(&ctx, guild_id, channel_id, s).await;
|
||||
match message_result {
|
||||
Ok(message) => return Ok(Self::Message(Box::new(message))),
|
||||
Err(ref e) => match e {
|
||||
MessageParseError::Http(_) | MessageParseError::HttpNotAvailable => {
|
||||
message_result?;
|
||||
}
|
||||
_ => {}
|
||||
},
|
||||
}
|
||||
|
||||
let channel_result = serenity::GuildChannel::convert(&ctx, guild_id, channel_id, s).await;
|
||||
match channel_result {
|
||||
Ok(channel) => return Ok(Self::Channel(Box::new(channel))),
|
||||
Err(ref e) => {
|
||||
tracing::error!("{:?}", e);
|
||||
if let GuildChannelParseError::Http(_) = e {
|
||||
channel_result?;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Err(Self::Err::Malformed)
|
||||
}
|
||||
}
|
||||
76
src/main.rs
76
src/main.rs
|
|
@ -1,3 +1,75 @@
|
|||
fn main() {
|
||||
println!("Hello, world!");
|
||||
use poise::PrefixFrameworkOptions;
|
||||
use tracing::{error, info};
|
||||
|
||||
mod checks;
|
||||
mod commands;
|
||||
|
||||
pub mod prelude {
|
||||
pub use poise::serenity_prelude as serenity;
|
||||
pub struct KBotData {}
|
||||
pub type Error = Box<dyn std::error::Error + Send + Sync>;
|
||||
pub type Context<'a> = poise::Context<'a, KBotData, Error>;
|
||||
pub type CommandResult = Result<(), Error>;
|
||||
}
|
||||
use prelude::*;
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() {
|
||||
tracing_subscriber::fmt::init();
|
||||
|
||||
dotenv::dotenv().ok();
|
||||
|
||||
let token = std::env::var("DISCORD_TOKEN").expect("missing DISCORD_TOKEN");
|
||||
|
||||
let intents =
|
||||
serenity::GatewayIntents::non_privileged() | serenity::GatewayIntents::MESSAGE_CONTENT;
|
||||
|
||||
let framework = poise::Framework::builder()
|
||||
.options(poise::FrameworkOptions {
|
||||
commands: commands::get(),
|
||||
prefix_options: PrefixFrameworkOptions {
|
||||
prefix: Some(",".into()),
|
||||
..Default::default()
|
||||
},
|
||||
on_error: |error| Box::pin(on_error(error)),
|
||||
..Default::default()
|
||||
})
|
||||
.setup(|ctx, ready, framework| {
|
||||
Box::pin(async move {
|
||||
info!("Logged in as {}", ready.user.name);
|
||||
poise::builtins::register_globally(ctx, &framework.options().commands).await?;
|
||||
Ok(KBotData {})
|
||||
})
|
||||
})
|
||||
.build();
|
||||
|
||||
let client = serenity::ClientBuilder::new(token, intents)
|
||||
.framework(framework)
|
||||
.await;
|
||||
client.unwrap().start().await.unwrap();
|
||||
}
|
||||
|
||||
async fn on_error(error: poise::FrameworkError<'_, KBotData, Error>) {
|
||||
match error {
|
||||
poise::FrameworkError::Command { error, ctx, .. } => {
|
||||
let error = error;
|
||||
error!("Error in command `{}`: {}", ctx.command().name, error,);
|
||||
let _ = ctx.say("Failed to run command.".to_string()).await;
|
||||
}
|
||||
// poise::FrameworkError::ArgumentParse {
|
||||
// error, input, ctx, ..
|
||||
// } => {
|
||||
// debug!(
|
||||
// "Failed to parse argument `{:?}` for command `{}`: {}",
|
||||
// input,
|
||||
// ctx.command().name,
|
||||
// error
|
||||
// );
|
||||
// }
|
||||
error => {
|
||||
if let Err(e) = poise::builtins::on_error(error).await {
|
||||
error!("Error while handling error: {}", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue