Update main to compatibility with latest versions and do some command updating and fixing. Also sort of purging database. might place back later if we use it again.

This commit is contained in:
Tom 2022-08-17 23:06:11 +02:00
parent 314098f68a
commit c53a5f1f2c

View file

@ -1,7 +1,7 @@
use config::{Config, File}; use config::{Config, File};
use glob::glob; use glob::glob;
use lazy_static::lazy_static; use lazy_static::lazy_static;
use log::LevelFilter; use log::{info, LevelFilter};
use poise::serenity_prelude as serenity; use poise::serenity_prelude as serenity;
use poise::serenity_prelude::UserId; use poise::serenity_prelude::UserId;
use regex::Regex; use regex::Regex;
@ -18,7 +18,7 @@ mod commands;
// Custom user data passed to all command functions // Custom user data passed to all command functions
pub struct Data { pub struct Data {
database: sqlx::SqlitePool, // database: sqlx::SqlitePool,
} }
lazy_static! { lazy_static! {
@ -34,17 +34,29 @@ lazy_static! {
// required_permissions = "MANAGE_MESSAGES | ADMINISTRATOR", // required_permissions = "MANAGE_MESSAGES | ADMINISTRATOR",
owners_only=true, owners_only=true,
)] )]
async fn register(ctx: Context<'_>, #[flag] global: bool) -> Result<(), Error> { async fn register(
ctx: Context<'_>,
#[flag] global: bool,
#[flag] purge: bool,
#[rest] rest: String,
) -> Result<(), Error> {
// poise::builtins::register_application_commands(ctx, global).await?; // poise::builtins::register_application_commands(ctx, global).await?;
let mut commands_builder = serenity::CreateApplicationCommands::default(); let mut commands_builder = serenity::CreateApplicationCommands::default();
let commands = &ctx.framework().options().commands; let commands = &ctx.framework().options().commands;
ctx.say(format!("Commands: {}", commands.len())).await?;
let mut command_count: u8 = 0;
for command in commands { for command in commands {
if let Some(slash_command) = command.create_as_slash_command() { command_count += 1;
commands_builder.add_application_command(slash_command); if rest.eq_ignore_ascii_case("all") || rest.contains(&command.name) {
} if let Some(slash_command) = command.create_as_slash_command() {
if let Some(context_menu_command) = command.create_as_context_menu_command() { commands_builder.add_application_command(slash_command);
commands_builder.add_application_command(context_menu_command); }
if let Some(context_menu_command) = command.create_as_context_menu_command() {
commands_builder.add_application_command(context_menu_command);
}
} else {
info!("Not adding command {}", command.name);
} }
} }
let commands_builder = serenity::json::Value::Array(commands_builder.0); let commands_builder = serenity::json::Value::Array(commands_builder.0);
@ -55,13 +67,27 @@ async fn register(ctx: Context<'_>, #[flag] global: bool) -> Result<(), Error> {
ctx.say("Can only be used by bot owner").await?; ctx.say("Can only be used by bot owner").await?;
return Ok(()); return Ok(());
} }
if purge {
ctx.say(format!("Registering {} commands...", commands.len())) ctx.say(format!(
"Purging global commands, must have been a mistake eh?"
))
.await?; .await?;
ctx.discord() ctx.discord()
.http .http
.create_global_application_commands(&commands_builder) .create_global_application_commands(&serenity::json::Value::Null)
.await?;
} else {
ctx.say(format!(
"Registering {} commands out of {}...",
command_count,
commands.len()
))
.await?; .await?;
ctx.discord()
.http
.create_global_application_commands(&commands_builder)
.await?;
}
} else { } else {
let guild = match ctx.guild() { let guild = match ctx.guild() {
Some(x) => x, Some(x) => x,
@ -77,8 +103,12 @@ async fn register(ctx: Context<'_>, #[flag] global: bool) -> Result<(), Error> {
return Ok(()); return Ok(());
} }
ctx.say(format!("Registering {} commands...", commands.len())) ctx.say(format!(
.await?; "Registering {} commands out of {}...",
command_count,
commands.len()
))
.await?;
ctx.discord() ctx.discord()
.http .http
.create_guild_application_commands(guild.id.0, &commands_builder) .create_guild_application_commands(guild.id.0, &commands_builder)
@ -130,22 +160,32 @@ This bot is very much a work in progress. Commands will change and may break.",
} }
fn setup_config() -> Result<(), Error> { fn setup_config() -> Result<(), Error> {
SETTINGS let mut setting = SETTINGS.write().unwrap();
.write() *setting = Config::builder()
.unwrap() .add_source(
.merge(
glob("data/config/*") glob("data/config/*")
.unwrap() .unwrap()
.map(|path| File::from(path.unwrap())) .map(|path| File::from(path.unwrap()))
.collect::<Vec<_>>(), .collect::<Vec<_>>(),
) )
.build()
.unwrap(); .unwrap();
println!("{:#?}", SETTINGS.read());
Ok(()) Ok(())
} }
async fn app() -> Result<(), Error> { async fn app() -> Result<(), Error> {
let commands = vec![
help(),
register(),
commands::invites::invites(),
commands::schedule::schedule(),
commands::boop(),
commands::fix(),
// commands::planning::get_command(),
commands::get_links(),
];
let discord = SETTINGS.read().unwrap().get_table("discord").unwrap(); let discord = SETTINGS.read().unwrap().get_table("discord").unwrap();
let mut owners: HashSet<UserId> = HashSet::new(); let mut owners: HashSet<UserId> = HashSet::new();
@ -155,19 +195,10 @@ async fn app() -> Result<(), Error> {
.get("prefix") .get("prefix")
.expect("Config error, please set the discord[token] value") .expect("Config error, please set the discord[token] value")
.to_owned() .to_owned()
.try_into() .into_string()
.expect("Config error, please make sure discord[token] is a string"); .expect("Config error, please make sure discord[token] is a string");
let options = poise::FrameworkOptions { let options = poise::FrameworkOptions {
commands: vec![ commands: commands,
help(),
register(),
commands::invites::invites(),
commands::schedule::schedule(),
commands::boop(),
commands::fix(),
commands::planning::get_command(),
commands::get_links(),
],
prefix_options: poise::PrefixFrameworkOptions { prefix_options: poise::PrefixFrameworkOptions {
prefix: Some(prefix.into()), prefix: Some(prefix.into()),
edit_tracker: Some(poise::EditTracker::for_timespan( edit_tracker: Some(poise::EditTracker::for_timespan(
@ -199,28 +230,33 @@ async fn app() -> Result<(), Error> {
.get("token") .get("token")
.expect("Config error, please set the discord[token] value") .expect("Config error, please set the discord[token] value")
.clone() .clone()
.into_str() .into_string()
.expect("Config error, please make sure discord[token] is a string"); .expect("Config error, please make sure discord[token] is a string");
let database = sqlx::sqlite::SqlitePoolOptions::new() // let database = sqlx::sqlite::SqlitePoolOptions::new()
.max_connections(5) // .max_connections(5)
.connect_with( // .connect_with(
"sqlite:data/database.db" // "sqlite:data/database.db"
.parse::<sqlx::sqlite::SqliteConnectOptions>()? // .parse::<sqlx::sqlite::SqliteConnectOptions>()?
.create_if_missing(true), // .create_if_missing(true),
) // )
.await?; // .await?;
sqlx::migrate!("./migrations").run(&database).await?; // sqlx::migrate!("./migrations").run(&database).await?;
poise::Framework::build() // let framework = poise::Framework::builder().options(options).token(token)
let framework = poise::Framework::builder()
.token(token) .token(token)
.user_data_setup(move |_ctx, _ready, _framework| { .user_data_setup(move |_ctx, _ready, _framework| {
Box::pin(async move { Ok(Data { database: database }) }) // Box::pin(async move { Ok(Data { database: database }) })
Box::pin(async move { Ok(Data {}) })
}) })
.options(options) .options(options)
.run() .intents(
.await serenity::GatewayIntents::non_privileged() | serenity::GatewayIntents::MESSAGE_CONTENT,
.unwrap(); );
framework.run().await.unwrap();
Ok(()) Ok(())
} }
@ -239,7 +275,7 @@ async fn main() {
.unwrap_or_default() .unwrap_or_default()
.get("level") .get("level")
{ {
Some(val) => match val.clone().into_str() { Some(val) => match val.clone().into_string() {
Ok(level) => match level.to_lowercase().as_str() { Ok(level) => match level.to_lowercase().as_str() {
"trace" => LevelFilter::Trace, "trace" => LevelFilter::Trace,
"debug" => LevelFilter::Debug, "debug" => LevelFilter::Debug,