I'm learning!

This commit is contained in:
shibedrill 2024-04-22 21:49:12 -04:00
parent 49f9b732dd
commit fcb227e316
6 changed files with 55 additions and 20 deletions

11
src/command/devel.rs Normal file
View File

@ -0,0 +1,11 @@
use crate::Context;
use crate::Error;
#[poise::command(slash_command, owners_only, hide_in_help)]
pub async fn shutdown(ctx: Context<'_>) -> Result<(), Error> {
ctx.defer_ephemeral().await?;
ctx.say("Shutting down...").await?;
info!("Received `shutdown` command, shutting down all shards");
ctx.framework().shard_manager().shutdown_all().await;
Ok(())
}

View File

@ -1,6 +0,0 @@
use rand::seq::SliceRandom;
use rand::Rng;
use crate::Context;
use crate::Error;

View File

@ -1,2 +1,2 @@
pub mod fun; pub mod devel;
pub mod util; pub mod util;

View File

@ -20,7 +20,7 @@ pub async fn age(
#[poise::command(slash_command)] #[poise::command(slash_command)]
pub async fn info(ctx: Context<'_>) -> Result<(), Error> { pub async fn info(ctx: Context<'_>) -> Result<(), Error> {
ctx.say(format!( ctx.say(format!(
"DeerBot ReBleated v{} was created by Shibe Drill (@shibedrill) using Rust and Poise.\nVisit her website: https://riverdev.carrd.co\nCheck out her Github: https://github.com/shibedrill\nPoise: https://docs.rs/poise/latest/poise/\nRust: https://www.rust-lang.org/", "Shibe Bot v{} was created by Shibe Drill (@shibedrill) using Rust and Poise.\nVisit her website: https://riverdev.carrd.co\nCheck out her Github: https://github.com/shibedrill\nPoise: https://docs.rs/poise/latest/poise/\nRust: https://www.rust-lang.org/",
env!("CARGO_PKG_VERSION") env!("CARGO_PKG_VERSION")
)) ))
.await?; .await?;
@ -34,6 +34,7 @@ pub async fn add_channel(
ctx: Context<'_>, ctx: Context<'_>,
#[description = "Selected channel"] channel: Option<serenity::Channel>, #[description = "Selected channel"] channel: Option<serenity::Channel>,
) -> Result<(), Error> { ) -> Result<(), Error> {
ctx.defer_ephemeral().await?;
if let Some(channel_ok) = channel { if let Some(channel_ok) = channel {
let config = &mut ctx.data().config_manager.lock().await; let config = &mut ctx.data().config_manager.lock().await;
let channel_id = { u64::from(channel_ok.id()) }; let channel_id = { u64::from(channel_ok.id()) };
@ -54,6 +55,7 @@ pub async fn add_channel(
#[poise::command(slash_command)] #[poise::command(slash_command)]
pub async fn list_channels(ctx: Context<'_>) -> Result<(), Error> { pub async fn list_channels(ctx: Context<'_>) -> Result<(), Error> {
ctx.defer_ephemeral().await?;
let config = &mut ctx.data().config_manager.lock().await; let config = &mut ctx.data().config_manager.lock().await;
let mut channel_ids: Vec<u64> = vec![]; let mut channel_ids: Vec<u64> = vec![];
config config
@ -61,7 +63,7 @@ pub async fn list_channels(ctx: Context<'_>) -> Result<(), Error> {
.iter() .iter()
.for_each(|c| channel_ids.push(u64::from(c.id()))); .for_each(|c| channel_ids.push(u64::from(c.id())));
ctx.say(format!( ctx.say(format!(
"Current channel IDs in registry: {:#?}", "Current channel IDs in registry: \n{:#?}",
channel_ids channel_ids
)) ))
.await?; .await?;

View File

@ -1,38 +1,54 @@
// Tokio async crap
use std::sync::Arc; use std::sync::Arc;
use tokio::sync::Mutex; use tokio::sync::Mutex;
// For secure credential handling
use dotenv::dotenv; use dotenv::dotenv;
// Poise and Serenity - Framework and API prelude
use poise::serenity_prelude as serenity; use poise::serenity_prelude as serenity;
use serenity::Channel; use serenity::Channel;
// Logging stuff
extern crate pretty_env_logger; extern crate pretty_env_logger;
#[macro_use] #[macro_use]
extern crate log; extern crate log;
// For managing config storage
use serde::*; use serde::*;
mod settings;
use crate::settings::*;
// Bot commands
mod command; mod command;
use crate::{ use crate::command::{
command::{fun::*, util::*}, // Commands for development and testing
settings::SettingsManager, devel::*,
// Useful commands for mods
util::*,
}; };
mod settings; // Data passed to every command (shared state)
struct Data { struct Data {
config_manager: Arc<Mutex<SettingsManager<Settings>>>, config_manager: Arc<Mutex<SettingsManager<Settings>>>,
} // User data, which is stored and accessible in all command invocations }
// Errors returnable by a command
type Error = Box<dyn std::error::Error + Send + Sync>; type Error = Box<dyn std::error::Error + Send + Sync>;
// The full context passed to a command
type Context<'a> = poise::Context<'a, Data, Error>; type Context<'a> = poise::Context<'a, Data, Error>;
// The structure making up the configuration
#[derive(Debug, Serialize, Deserialize, Default)] #[derive(Debug, Serialize, Deserialize, Default)]
struct Settings { struct Settings {
channels: Vec<Channel>, channels: Vec<Channel>,
} }
// Path at which our settings are stored (currently PWD)
const SETTINGS_PATH: &str = "settings.json"; const SETTINGS_PATH: &str = "settings.json";
// Main function for setup
#[tokio::main] #[tokio::main]
async fn main() { async fn main() {
// Get secure env vars from .env file // Get secure env vars from .env file
@ -55,12 +71,17 @@ async fn main() {
// Set up framework // Set up framework
let framework = poise::Framework::builder() let framework = poise::Framework::builder()
.options(poise::FrameworkOptions { .options(poise::FrameworkOptions {
commands: vec![age(), info(), add_channel(), list_channels()], // +---------------------------------------------------------+
// | ADD COMMANDS HERE |
// +---------------------------------------------------------+
commands: vec![age(), info(), add_channel(), list_channels(), shutdown()],
initialize_owners: true,
..Default::default() ..Default::default()
}) })
.setup(|ctx, _ready, framework| { .setup(|ctx, _ready, framework| {
Box::pin(async move { Box::pin(async move {
poise::builtins::register_globally(ctx, &framework.options().commands).await?; poise::builtins::register_globally(ctx, &framework.options().commands).await?;
// Shared data has to go here!!!
Ok(Data { config_manager }) Ok(Data { config_manager })
}) })
}) })
@ -69,6 +90,7 @@ async fn main() {
// Log pertinent info // Log pertinent info
info!("Built framework successfully"); info!("Built framework successfully");
{ {
// List registered commands
let mut commands: Vec<&str> = vec![]; let mut commands: Vec<&str> = vec![];
framework framework
.options() .options()
@ -78,10 +100,16 @@ async fn main() {
info!("Registered commands: {:?}", commands); info!("Registered commands: {:?}", commands);
} }
let client = serenity::ClientBuilder::new(token, intents) // Build client
let mut client = serenity::ClientBuilder::new(token, intents)
.framework(framework) .framework(framework)
.await; .await
.unwrap();
info!("Built client successfully"); info!("Built client successfully");
info!("Registered owner: {:?}", client.http.get_current_application_info().await.unwrap().owner.unwrap().name);
// Finally start everything. Nothing after this should be reachable normally.
info!("Starting client"); info!("Starting client");
client.unwrap().start().await.unwrap(); client.start().await.unwrap();
} }