Skip to content

Commit

Permalink
add serenity_test.rs
Browse files Browse the repository at this point in the history
  • Loading branch information
www committed Nov 16, 2024
1 parent 6cfae2b commit 3d75256
Show file tree
Hide file tree
Showing 3 changed files with 151 additions and 2 deletions.
6 changes: 5 additions & 1 deletion services/rust.assetgen/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
mod docgen;
mod serenity_test;
mod tester;

use std::fs::File;
Expand Down Expand Up @@ -83,8 +84,11 @@ async fn main() {

println!("{}", docs)
}
Some("serenity.test") => {
crate::serenity_test::test_serenity().await;
}
_ => {
println!("No/unknown command specified.\n\ngenassets: [generate build assets]\ntest [test bot with some sanity checks]\ntemplatedocs: [generate template docs]");
println!("No/unknown command specified.\n\ngenassets: [generate build assets]\ntest [test bot with some sanity checks]\ntemplatedocs: [generate template docs]\nserenity.test: [test serenity library]");
}
}
}
145 changes: 145 additions & 0 deletions services/rust.assetgen/src/serenity_test.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
use log::{error, info};

#[allow(unused_imports)] // They aren't unused
use serenity::all::{FullEvent, HttpBuilder};
use std::io::Write;
use std::sync::Arc;

pub async fn test_serenity() {
let debug_mode = std::env::var("DEBUG").unwrap_or_default() == "true";
let debug_opts = std::env::var("DEBUG_OPTS").unwrap_or_default();

let mut env_builder = env_logger::builder();

let default_filter =
"serenity=debug,rust_assetgen=info,bot_binutils=info,botox=info,templating=debug,sqlx=error"
.to_string();

env_builder
.format(move |buf, record| {
writeln!(
buf,
"({}) {} - {}",
record.target(),
record.level(),
record.args()
)
})
.parse_filters(&default_filter)
.filter(None, log::LevelFilter::Info);

// Set custom log levels
for opt in debug_opts.split(',') {
let opt = opt.trim();

if opt.is_empty() {
continue;
}

let (target, level) = if opt.contains('=') {
let mut split = opt.split('=');
let target = split.next().unwrap();
let level = split.next().unwrap();
(target, level)
} else {
(opt, "debug")
};

let level = match level {
"trace" => log::LevelFilter::Trace,
"debug" => log::LevelFilter::Debug,
"info" => log::LevelFilter::Info,
"warn" => log::LevelFilter::Warn,
"error" => log::LevelFilter::Error,
_ => {
error!("Invalid log level: {}", level);
continue;
}
};

env_builder.filter(Some(target), level);
}

if debug_mode {
env_builder.filter(None, log::LevelFilter::Debug);
} else {
env_builder.filter(None, log::LevelFilter::Error);
}

env_builder.init();

info!("Starting serenity_test");

let proxy_url = config::CONFIG.meta.proxy.clone();

let http = Arc::new(
HttpBuilder::new(&config::CONFIG.discord_auth.token)
.proxy(proxy_url)
.ratelimiter_disabled(true)
.build(),
);

info!("HttpBuilder done");

let mut intents = serenity::all::GatewayIntents::all();

// Remove the really spammy intents
intents.remove(serenity::all::GatewayIntents::GUILD_PRESENCES); // Don't even have the privileged gateway intent for this
intents.remove(serenity::all::GatewayIntents::GUILD_MESSAGE_TYPING); // Don't care about typing
intents.remove(serenity::all::GatewayIntents::DIRECT_MESSAGE_TYPING); // Don't care about typing
intents.remove(serenity::all::GatewayIntents::DIRECT_MESSAGES); // Don't care about DMs

let client_builder = serenity::all::ClientBuilder::new_with_http(http, intents);

let framework_opts = poise::FrameworkOptions {
initialize_owners: true,
prefix_options: poise::PrefixFrameworkOptions {
prefix: Some("%".into()),
..poise::PrefixFrameworkOptions::default()
},
event_handler: |_ctx, event| {
Box::pin(async move {
log::trace!("Event: {:?}", event);
Ok(())
})
},
commands: vec![],
command_check: Some(|ctx| Box::pin(bot_binutils::command_check(ctx))),
pre_command: |ctx| {
Box::pin(async move {
info!(
"Executing command {} for user {} ({})...",
ctx.command().qualified_name,
ctx.author().name,
ctx.author().id
);
})
},
post_command: |ctx| {
Box::pin(async move {
info!(
"Done executing command {} for user {} ({})...",
ctx.command().qualified_name,
ctx.author().name,
ctx.author().id
);
})
},
on_error: |error| Box::pin(bot_binutils::on_error(error)),
..Default::default()
};

let framework = poise::Framework::builder().options(framework_opts).build();

info!("DB Connect [start]");

let mut client = client_builder
.framework(framework)
.data(Arc::new(()))
.await
.expect("Error creating client");

if let Err(why) = client.start_autosharded().await {
panic!("Client error occurred during testing: {:?}", why);
}
}
2 changes: 1 addition & 1 deletion services/website

0 comments on commit 3d75256

Please sign in to comment.