Some basic testing with the Poise crate
parent
7150eb75b4
commit
c610a79d5f
@ -1,20 +1,20 @@
|
|||||||
|
|
||||||
use std::{io::prelude::*, fs::File, path::Path };
|
use std::{io::prelude::*, fs::File, path::Path };
|
||||||
|
|
||||||
pub fn load_token(file_name: &str) -> String
|
pub fn load_token(file_name: &str) -> Result<String, String>
|
||||||
{
|
{
|
||||||
let mut file = match File::open(Path::new(file_name))
|
let mut file = match File::open(Path::new(file_name))
|
||||||
{
|
{
|
||||||
Ok(file) => file,
|
Ok(file) => file,
|
||||||
Err(why) => panic!("Could not open token file {}: {}", Path::new(file_name).display(), why),
|
Err(why) => return Err(format!("Could not open token file {}: {}", Path::new(file_name).display(), why)),
|
||||||
};
|
};
|
||||||
|
|
||||||
let mut s = String::new();
|
let mut s = String::new();
|
||||||
let token = match file.read_to_string(&mut s)
|
let token = match file.read_to_string(&mut s)
|
||||||
{
|
{
|
||||||
Err(why) => panic!("couldn't read {}: {}", Path::new(file_name).display(), why),
|
Err(why) => return Err(format!("couldn't read {}: {}", Path::new(file_name).display(), why)),
|
||||||
Ok(_) => s,
|
Ok(_) => s,
|
||||||
};
|
};
|
||||||
|
|
||||||
token
|
Ok(token)
|
||||||
}
|
}
|
||||||
@ -1,10 +1,76 @@
|
|||||||
|
|
||||||
|
use poise::serenity_prelude as serenity;
|
||||||
|
|
||||||
mod commands;
|
mod commands;
|
||||||
mod data_loader;
|
mod data_loader;
|
||||||
|
|
||||||
fn main()
|
pub struct Data {} // User data, which is stored and accessible in all command invocations
|
||||||
|
type Error = Box<dyn std::error::Error + Send + Sync>;
|
||||||
|
type Context<'a> = poise::Context<'a, Data, Error>;
|
||||||
|
|
||||||
|
/// Displays your or another user's account creation date
|
||||||
|
#[poise::command(slash_command, prefix_command)]
|
||||||
|
async fn age(ctx: Context<'_>, #[description = "Selected user"] user: Option<serenity::User>) -> Result<(), Error>
|
||||||
|
{
|
||||||
|
let u = user.as_ref().unwrap_or_else(|| ctx.author());
|
||||||
|
let response = format!("{}'s account was created at {}", u.name, u.created_at());
|
||||||
|
ctx.say(response).await?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
/// A command with two subcommands: `child1` and `child2`
|
||||||
|
///
|
||||||
|
/// Running this function directly, without any subcommand, is only supported in prefix commands.
|
||||||
|
/// Discord doesn't permit invoking the root command of a slash command if it has subcommands.
|
||||||
|
#[poise::command(prefix_command, slash_command, subcommands("child1", "child2"))]
|
||||||
|
pub async fn parent(ctx: Context<'_>) -> Result<(), Error>
|
||||||
|
{
|
||||||
|
ctx.say("Hello there!").await?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
/// A subcommand of `parent`
|
||||||
|
#[poise::command(prefix_command, slash_command)]
|
||||||
|
pub async fn child1(ctx: Context<'_>) -> Result<(), Error>
|
||||||
|
{
|
||||||
|
ctx.say("You invoked the first child command!").await?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Another subcommand of `parent`
|
||||||
|
#[poise::command(prefix_command, slash_command)]
|
||||||
|
pub async fn child2(ctx: Context<'_>) -> Result<(), Error>
|
||||||
|
{
|
||||||
|
ctx.say("You invoked the second child command!").await?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::main]
|
||||||
|
async fn main()
|
||||||
|
{
|
||||||
|
let token = match data_loader::load_token("secrets/test.txt")
|
||||||
|
{
|
||||||
|
Ok(t) => t,
|
||||||
|
Err(why) => panic!("Could not load app token: {}", why),
|
||||||
|
};
|
||||||
|
|
||||||
|
println!("Connecting...");
|
||||||
|
|
||||||
|
let framework = poise::Framework::builder()
|
||||||
|
.options(poise::FrameworkOptions {
|
||||||
|
commands: vec![age(), parent()],
|
||||||
|
..Default::default()
|
||||||
|
})
|
||||||
|
.token(token)
|
||||||
|
.intents(serenity::GatewayIntents::non_privileged())
|
||||||
|
.setup(|ctx, _ready, framework|
|
||||||
|
{
|
||||||
|
Box::pin(async move
|
||||||
{
|
{
|
||||||
let token = data_loader::load_token("secrets/test.txt");
|
poise::builtins::register_globally(ctx, &framework.options().commands).await?;
|
||||||
println!("test data: {}", token);
|
Ok(Data {})
|
||||||
|
})
|
||||||
|
});
|
||||||
|
|
||||||
|
framework.run().await.unwrap();
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue