Move to poise, add slash commands
This commit is contained in:
parent
d397bb7117
commit
9e9eb48025
8 changed files with 385 additions and 404 deletions
156
src/main.rs
156
src/main.rs
|
@ -1,69 +1,26 @@
|
|||
mod commands;
|
||||
mod utils;
|
||||
|
||||
use std::{collections::HashSet, env, sync::Arc};
|
||||
use std::env;
|
||||
|
||||
type Error = Box<dyn std::error::Error + Send + Sync>;
|
||||
type Context<'a> = poise::Context<'a, Data, Error>;
|
||||
type PrefixContext<'a> = poise::PrefixContext<'a, Data, Error>;
|
||||
// User data, which is stored and accessible in all command invocations
|
||||
pub struct Data {}
|
||||
|
||||
use commands::{challenge::*, kanji::*, meta::*, owner::*};
|
||||
use serenity::model::gateway::Activity;
|
||||
use serenity::{
|
||||
async_trait,
|
||||
client::bridge::gateway::ShardManager,
|
||||
framework::{standard::macros::group, StandardFramework},
|
||||
http::Http,
|
||||
model::{
|
||||
event::ResumedEvent,
|
||||
gateway::{GatewayIntents, Ready},
|
||||
},
|
||||
prelude::*,
|
||||
use poise::serenity_prelude::{
|
||||
model::gateway::GatewayIntents
|
||||
};
|
||||
use tracing::{error, info};
|
||||
use poise::serenity_prelude as serenity;
|
||||
|
||||
pub struct ShardManagerContainer;
|
||||
|
||||
impl TypeMapKey for ShardManagerContainer {
|
||||
type Value = Arc<Mutex<ShardManager>>;
|
||||
#[poise::command(prefix_command)]
|
||||
async fn register(ctx: Context<'_>) -> Result<(), Error> {
|
||||
poise::builtins::register_application_commands_buttons(ctx).await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
struct Handler;
|
||||
|
||||
#[async_trait]
|
||||
impl EventHandler for Handler {
|
||||
async fn ready(&self, ctx: Context, ready: Ready) {
|
||||
info!("Connected as {}", ready.user.name);
|
||||
let activity = Activity::watching("for new submissions");
|
||||
ctx.set_activity(activity).await;
|
||||
}
|
||||
|
||||
async fn resume(&self, _: Context, _: ResumedEvent) {
|
||||
info!("Resumed");
|
||||
}
|
||||
}
|
||||
|
||||
#[group]
|
||||
#[commands(
|
||||
i,
|
||||
joyo,
|
||||
jinmeiyo,
|
||||
kyoiku,
|
||||
jlpt,
|
||||
hyogai,
|
||||
so,
|
||||
rebuildSite,
|
||||
challenge,
|
||||
submit,
|
||||
images,
|
||||
imageDelete,
|
||||
help,
|
||||
sleep,
|
||||
setSubmissionChannel,
|
||||
setSuggestionChannel,
|
||||
setAnnouncementRole,
|
||||
suggest,
|
||||
announce,
|
||||
announceChallenge
|
||||
)]
|
||||
struct General;
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() {
|
||||
// This will load the environment variables located at `./.env`, relative to
|
||||
|
@ -74,55 +31,52 @@ async fn main() {
|
|||
//
|
||||
// In this case, a good default is setting the environment variable
|
||||
// `RUST_LOG` to `debug`.
|
||||
tracing_subscriber::fmt::init();
|
||||
|
||||
let token = env::var("DISCORD_TOKEN").expect("Expected a token in the environment");
|
||||
let prefix = env::var("PREFIX").expect("Expected a prefix in the environment");
|
||||
let framework = poise::Framework::builder()
|
||||
.options(poise::FrameworkOptions {
|
||||
commands: vec![
|
||||
register(),
|
||||
|
||||
// owner
|
||||
sleep(),
|
||||
setsubmissionchannel(),
|
||||
setsuggestionchannel(),
|
||||
setannouncementrole(),
|
||||
announce(),
|
||||
announcechallenge(),
|
||||
rebuildsite(),
|
||||
|
||||
let http = Http::new(&token);
|
||||
// challenge
|
||||
challenge(),
|
||||
submit(),
|
||||
images(),
|
||||
imagedelete(),
|
||||
suggest(),
|
||||
|
||||
// We will fetch your bot's owners and id
|
||||
let (owners, _bot_id) = match http.get_current_application_info().await {
|
||||
Ok(info) => {
|
||||
let mut owners = HashSet::new();
|
||||
owners.insert(info.owner.id);
|
||||
// meta
|
||||
help(),
|
||||
|
||||
(owners, info.id)
|
||||
}
|
||||
Err(why) => panic!("Could not access application info: {:?}", why),
|
||||
};
|
||||
|
||||
// Create the framework
|
||||
let framework = StandardFramework::new()
|
||||
.configure(|c| c.owners(owners).prefix(prefix))
|
||||
.group(&GENERAL_GROUP)
|
||||
.unrecognised_command(commands::meta::unrecognised_command_hook);
|
||||
|
||||
let intents = GatewayIntents::GUILD_MESSAGES
|
||||
// kanji
|
||||
i(),
|
||||
joyo(),
|
||||
jinmeiyo(),
|
||||
kyoiku(),
|
||||
jlpt(),
|
||||
hyogai(),
|
||||
so()
|
||||
],
|
||||
prefix_options: poise::PrefixFrameworkOptions {
|
||||
prefix: Some(env::var("PREFIX").expect("Expected a prefix in the environment")),
|
||||
..Default::default()
|
||||
},
|
||||
..Default::default()
|
||||
})
|
||||
.token(std::env::var("DISCORD_TOKEN").expect("Expected a token in the environment"))
|
||||
.intents(GatewayIntents::GUILD_MESSAGES
|
||||
| GatewayIntents::DIRECT_MESSAGES
|
||||
| GatewayIntents::MESSAGE_CONTENT
|
||||
| GatewayIntents::GUILDS; // required for getting guild name like in setSubmissionChannel
|
||||
let mut client = Client::builder(&token, intents)
|
||||
.framework(framework)
|
||||
.event_handler(Handler)
|
||||
.await
|
||||
.expect("Err creating client");
|
||||
| GatewayIntents::GUILDS)
|
||||
.user_data_setup(move |_ctx, _ready, _framework| Box::pin(async move { Ok(Data {}) }));
|
||||
|
||||
{
|
||||
let mut data = client.data.write().await;
|
||||
data.insert::<ShardManagerContainer>(client.shard_manager.clone());
|
||||
}
|
||||
|
||||
let shard_manager = client.shard_manager.clone();
|
||||
|
||||
tokio::spawn(async move {
|
||||
tokio::signal::ctrl_c()
|
||||
.await
|
||||
.expect("Could not register ctrl+c handler");
|
||||
shard_manager.lock().await.shutdown_all().await;
|
||||
});
|
||||
|
||||
if let Err(why) = client.start().await {
|
||||
error!("Client error: {:?}", why);
|
||||
}
|
||||
framework.run().await.unwrap();
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue