feat: bumped serenity to the latest version

Lots of changes to how it runs
This commit is contained in:
silver 2025-02-19 00:17:02 +00:00
parent a8c1cc9cf1
commit 6b84f33d2e
Signed by: silver
GPG key ID: 36F93D61BAD3FD7D
12 changed files with 352 additions and 485 deletions

View file

@ -1,68 +1,53 @@
use serenity::{
builder::CreateApplicationCommand,
client::Context,
model::{
application::interaction::application_command::ApplicationCommandInteraction,
prelude::{command::CommandOptionType, interaction::application_command::CommandDataOptionValue},
},
};
use serenity::all::{CommandDataOption, CommandDataOptionValue, CommandInteraction, CommandOptionType, CreateCommandOption};
use serenity::{builder::CreateCommand, client::Context};
use skynet_discord_bot::common::database::{get_server_config, DataBase, Servers};
use skynet_discord_bot::common::set_roles::normal::update_server;
use skynet_discord_bot::common::wolves::cns::get_wolves;
use skynet_discord_bot::is_admin;
use sqlx::{Error, Pool, Sqlite};
pub async fn run(command: &ApplicationCommandInteraction, ctx: &Context) -> String {
pub async fn run(command: &CommandInteraction, ctx: &Context) -> String {
// check if user has high enough permisssions
if let Some(msg) = is_admin(command, ctx).await {
return msg;
}
let api_key = if let CommandDataOptionValue::String(key) = command
.data
.options
.first()
.expect("Expected user option")
.resolved
.as_ref()
.expect("Expected user object")
let wolves_api = if let Some(CommandDataOption {
value: CommandDataOptionValue::String(key),
..
}) = command.data.options.first()
{
key
key.to_string()
} else {
return "Please provide a wolves API key".to_string();
};
let role_current = if let CommandDataOptionValue::Role(role) = command
.data
.options
.get(1)
.expect("Expected role option")
.resolved
.as_ref()
.expect("Expected role object")
let role_current = if let Some(CommandDataOption {
value: CommandDataOptionValue::Role(role),
..
}) = command.data.options.get(1)
{
role.id.to_owned()
role.to_owned()
} else {
return "Please provide a valid role for ``Role Current``".to_string();
};
let mut role_past = None;
if let Some(x) = command.data.options.get(5) {
if let Some(CommandDataOptionValue::Role(role)) = &x.resolved {
role_past = Some(role.id.to_owned());
}
let role_past = if let Some(CommandDataOption {
value: CommandDataOptionValue::Role(role),
..
}) = command.data.options.get(5)
{
Some(role.to_owned())
} else {
None
};
let bot_channel_id = if let CommandDataOptionValue::Channel(channel) = command
.data
.options
.get(2)
.expect("Expected channel option")
.resolved
.as_ref()
.expect("Expected channel object")
let bot_channel_id = if let Some(CommandDataOption {
value: CommandDataOptionValue::Channel(channel),
..
}) = command.data.options.get(2)
{
channel.id.to_owned()
channel.to_owned()
} else {
return "Please provide a valid channel for ``Bot Channel``".to_string();
};
@ -75,7 +60,7 @@ pub async fn run(command: &ApplicationCommandInteraction, ctx: &Context) -> Stri
let server_data = Servers {
server: command.guild_id.unwrap_or_default(),
wolves_api: api_key.to_owned(),
wolves_api,
role_past,
role_current,
member_past: 0,
@ -95,43 +80,18 @@ pub async fn run(command: &ApplicationCommandInteraction, ctx: &Context) -> Stri
"Added/Updated server info".to_string()
}
pub fn register(command: &mut CreateApplicationCommand) -> &mut CreateApplicationCommand {
command
.name("add")
pub fn register() -> CreateCommand {
CreateCommand::new("add")
.description("Enable the bot for this discord")
.create_option(|option| {
option
.name("api_key")
.description("UL Wolves API Key")
.kind(CommandOptionType::String)
.required(true)
})
.create_option(|option| {
option
.name("role_current")
.description("Role for Current members")
.kind(CommandOptionType::Role)
.required(true)
})
.create_option(|option| {
option
.name("bot_channel")
.description("Safe space for folks to use the bot commands.")
.kind(CommandOptionType::Channel)
.required(true)
})
.create_option(|option| {
option
.name("role_past")
.description("Role for Past members")
.kind(CommandOptionType::Role)
.required(false)
})
.add_option(CreateCommandOption::new(CommandOptionType::String, "api_key", "UL Wolves API Key").required(true))
.add_option(CreateCommandOption::new(CommandOptionType::Role, "role_current", "Role for Current members").required(true))
.add_option(CreateCommandOption::new(CommandOptionType::Channel, "bot_channel", "Safe space for folks to use the bot commands.").required(true))
.add_option(CreateCommandOption::new(CommandOptionType::Role, "role_past", "Role for Past members").required(false))
}
async fn add_server(db: &Pool<Sqlite>, ctx: &Context, server: &Servers) -> Result<Option<Servers>, Error> {
let existing = get_server_config(db, &server.server).await;
let role_past = server.role_past.map(|x| *x.as_u64() as i64);
let role_past = server.role_past.map(|x| x.get() as i64);
let insert = sqlx::query_as::<_, Servers>(
"
@ -139,11 +99,11 @@ async fn add_server(db: &Pool<Sqlite>, ctx: &Context, server: &Servers) -> Resul
VALUES (?1, ?2, ?3, ?4, ?5)
",
)
.bind(*server.server.as_u64() as i64)
.bind(server.server.get() as i64)
.bind(&server.wolves_api)
.bind(role_past)
.bind(*server.role_current.as_u64() as i64)
.bind(*server.bot_channel_id.as_u64() as i64)
.bind(server.role_current.get() as i64)
.bind(server.bot_channel_id.get() as i64)
.fetch_optional(db)
.await;

View file

@ -4,15 +4,7 @@ use lettre::{
Message, SmtpTransport, Transport,
};
use maud::html;
use serenity::{
builder::CreateApplicationCommand,
client::Context,
model::{
application::interaction::application_command::ApplicationCommandInteraction,
id::UserId,
prelude::{command::CommandOptionType, interaction::application_command::CommandDataOptionValue},
},
};
use serenity::{builder::CreateCommand, client::Context, model::id::UserId};
use skynet_discord_bot::common::database::{DataBase, Wolves, WolvesVerify};
use skynet_discord_bot::{get_now_iso, random_string, Config};
use sqlx::{Pool, Sqlite};
@ -20,8 +12,9 @@ use sqlx::{Pool, Sqlite};
pub mod link {
use super::*;
use serde::{Deserialize, Serialize};
use serenity::all::{CommandDataOption, CommandDataOptionValue, CommandInteraction, CommandOptionType, CreateCommand, CreateCommandOption};
pub async fn run(command: &ApplicationCommandInteraction, ctx: &Context) -> String {
pub async fn run(command: &CommandInteraction, ctx: &Context) -> String {
let db_lock = {
let data_read = ctx.data.read().await;
data_read.get::<DataBase>().expect("Expected Databse in TypeMap.").clone()
@ -44,16 +37,11 @@ pub mod link {
return "Linking already in process, please check email.".to_string();
}
let option = command
.data
.options
.first()
.expect("Expected email option")
.resolved
.as_ref()
.expect("Expected email object");
let email = if let CommandDataOptionValue::String(email) = option {
let email = if let Some(CommandDataOption {
value: CommandDataOptionValue::String(email),
..
}) = command.data.options.first()
{
email.trim()
} else {
return "Please provide a valid user".to_string();
@ -115,11 +103,10 @@ pub mod link {
format!("Verification email sent to {}, it may take up to 15 min for it to arrive. If it takes longer check the Junk folder.", email)
}
pub fn register(command: &mut CreateApplicationCommand) -> &mut CreateApplicationCommand {
command
.name("link_wolves")
pub fn register() -> CreateCommand {
CreateCommand::new("link_wolves")
.description("Set Wolves Email")
.create_option(|option| option.name("email").description("UL Wolves Email").kind(CommandOptionType::String).required(true))
.add_option(CreateCommandOption::new(CommandOptionType::String, "email", "UL Wolves Email").required(true))
}
pub async fn get_server_member_discord(db: &Pool<Sqlite>, user: &UserId) -> Option<Wolves> {
@ -130,7 +117,7 @@ pub mod link {
WHERE discord = ?
"#,
)
.bind(*user.as_u64() as i64)
.bind(user.get() as i64)
.fetch_one(db)
.await
.ok()
@ -242,7 +229,7 @@ pub mod link {
WHERE discord = ?
"#,
)
.bind(*user.as_u64() as i64)
.bind(user.get() as i64)
.fetch_one(db)
.await
.ok()
@ -256,7 +243,7 @@ pub mod link {
",
)
.bind(record.email.to_owned())
.bind(*user.as_u64() as i64)
.bind(user.get() as i64)
.bind(auth.to_owned())
.bind(get_now_iso(false))
.fetch_optional(db)
@ -294,12 +281,13 @@ pub mod link {
pub mod verify {
use super::*;
use crate::commands::link_email::link::{db_pending_clear_expired, get_verify_from_db};
use serenity::all::{CommandDataOption, CommandDataOptionValue, CommandInteraction, CommandOptionType, CreateCommandOption};
use serenity::model::user::User;
use skynet_discord_bot::common::database::get_server_config;
use skynet_discord_bot::common::database::{ServerMembersWolves, Servers};
use sqlx::Error;
pub async fn run(command: &ApplicationCommandInteraction, ctx: &Context) -> String {
pub async fn run(command: &CommandInteraction, ctx: &Context) -> String {
let db_lock = {
let data_read = ctx.data.read().await;
data_read.get::<DataBase>().expect("Expected Databse in TypeMap.").clone()
@ -313,16 +301,11 @@ pub mod verify {
return "Please use /link_wolves first".to_string();
};
let option = command
.data
.options
.first()
.expect("Expected code option")
.resolved
.as_ref()
.expect("Expected code object");
let code = if let CommandDataOptionValue::String(code) = option {
let code = if let Some(CommandDataOption {
value: CommandDataOptionValue::String(code),
..
}) = command.data.options.first()
{
code
} else {
return "Please provide a verification code".to_string();
@ -354,14 +337,10 @@ pub mod verify {
"Failed to verify".to_string()
}
pub fn register(command: &mut CreateApplicationCommand) -> &mut CreateApplicationCommand {
command.name("verify").description("Verify Wolves Email").create_option(|option| {
option
.name("code")
.description("Code from verification email")
.kind(CommandOptionType::String)
.required(true)
})
pub fn register() -> CreateCommand {
CreateCommand::new("verify")
.description("Verify Wolves Email")
.add_option(CreateCommandOption::new(CommandOptionType::String, "code", "Code from verification email").required(true))
}
async fn db_pending_clear_successful(pool: &Pool<Sqlite>, user: &UserId) -> Result<Option<WolvesVerify>, Error> {
@ -372,7 +351,7 @@ pub mod verify {
WHERE discord = ?
"#,
)
.bind(*user.as_u64() as i64)
.bind(user.get() as i64)
.fetch_optional(pool)
.await
}
@ -385,7 +364,7 @@ pub mod verify {
WHERE email = ?
",
)
.bind(*discord.as_u64() as i64)
.bind(discord.get() as i64)
.bind(email)
.fetch_optional(db)
.await
@ -394,7 +373,7 @@ pub mod verify {
async fn set_server_roles(db: &Pool<Sqlite>, discord: &User, ctx: &Context) {
if let Ok(servers) = get_servers(db, &discord.id).await {
for server in servers {
if let Ok(mut member) = server.server.member(&ctx.http, &discord.id).await {
if let Ok(member) = server.server.member(&ctx.http, &discord.id).await {
if let Some(config) = get_server_config(db, &server.server).await {
let Servers {
role_past,
@ -432,7 +411,7 @@ pub mod verify {
WHERE discord = ?
",
)
.bind(*discord.as_u64() as i64)
.bind(discord.get() as i64)
.fetch_all(db)
.await
}

View file

@ -1,11 +1,4 @@
use serenity::{
builder::CreateApplicationCommand,
client::Context,
model::{
application::interaction::application_command::ApplicationCommandInteraction,
prelude::{command::CommandOptionType, interaction::application_command::CommandDataOptionValue},
},
};
use serenity::{builder::CreateCommand, client::Context};
use skynet_discord_bot::common::database::DataBase;
use sqlx::{Pool, Sqlite};
@ -16,33 +9,21 @@ pub(crate) mod user {
use super::*;
use crate::commands::link_email::link::get_server_member_discord;
use serde::{Deserialize, Serialize};
use serenity::all::{CommandDataOption, CommandDataOptionValue, CommandInteraction, CommandOptionType, CreateCommandOption};
use serenity::model::id::UserId;
use skynet_discord_bot::common::database::Wolves;
use skynet_discord_bot::common::minecraft::{whitelist_update, Minecraft};
use skynet_discord_bot::Config;
use sqlx::Error;
pub fn register(command: &mut CreateApplicationCommand) -> &mut CreateApplicationCommand {
command
.name("link_minecraft")
pub fn register() -> CreateCommand {
CreateCommand::new("link_minecraft")
.description("Link your minecraft account")
.create_option(|option| {
option
.name("minecraft_username")
.description("Your Minecraft username")
.kind(CommandOptionType::String)
.required(true)
})
.create_option(|option| {
option
.name("bedrock_account")
.description("Is this a Bedrock account?")
.kind(CommandOptionType::Boolean)
.required(false)
})
.add_option(CreateCommandOption::new(CommandOptionType::String, "minecraft_username", "Your Minecraft username").required(true))
.add_option(CreateCommandOption::new(CommandOptionType::Boolean, "bedrock_account", "Is this a Bedrock account?").required(false))
}
pub async fn run(command: &ApplicationCommandInteraction, ctx: &Context) -> String {
pub async fn run(command: &CommandInteraction, ctx: &Context) -> String {
let db_lock = {
let data_read = ctx.data.read().await;
data_read.get::<DataBase>().expect("Expected Databse in TypeMap.").clone()
@ -60,14 +41,10 @@ pub(crate) mod user {
return "Not linked with wolves, please use ``/link_wolves`` with your wolves email.".to_string();
}
let username = if let CommandDataOptionValue::String(username) = command
.data
.options
.first()
.expect("Expected username option")
.resolved
.as_ref()
.expect("Expected username object")
let username = if let Some(CommandDataOption {
value: CommandDataOptionValue::String(username),
..
}) = command.data.options.first()
{
username.trim()
} else {
@ -75,12 +52,15 @@ pub(crate) mod user {
};
// this is always true unless they state its not
let mut java = true;
if let Some(x) = command.data.options.get(1) {
if let Some(CommandDataOptionValue::Boolean(z)) = x.to_owned().resolved {
java = !z;
}
}
let java = if let Some(CommandDataOption {
value: CommandDataOptionValue::Boolean(z),
..
}) = command.data.options.get(1)
{
!z
} else {
true
};
let username_mc;
if java {
@ -130,7 +110,7 @@ pub(crate) mod user {
WHERE discord = ?1;
",
)
.bind(*user.as_u64() as i64)
.bind(user.get() as i64)
.bind(minecraft)
.fetch_optional(db)
.await
@ -175,7 +155,7 @@ pub(crate) mod user {
WHERE discord = ?1;
",
)
.bind(*user.as_u64() as i64)
.bind(user.get() as i64)
.bind(minecraft)
.fetch_optional(db)
.await
@ -194,7 +174,7 @@ pub(crate) mod user {
) sub on minecraft.server_discord = sub.server
",
)
.bind(*discord.as_u64() as i64)
.bind(discord.get() as i64)
.fetch_all(db)
.await
}
@ -205,6 +185,7 @@ pub(crate) mod server {
use super::*;
pub(crate) mod add {
use serenity::all::{CommandDataOption, CommandDataOptionValue, CommandInteraction, CommandOptionType, CreateCommand, CreateCommandOption};
use serenity::model::id::GuildId;
use sqlx::Error;
// this is to managfe the server side of commands related to minecraft
@ -213,17 +194,13 @@ pub(crate) mod server {
use skynet_discord_bot::common::minecraft::Minecraft;
use skynet_discord_bot::{is_admin, Config};
pub fn register(command: &mut CreateApplicationCommand) -> &mut CreateApplicationCommand {
command.name("minecraft_add").description("Add a minecraft server").create_option(|option| {
option
.name("server_id")
.description("ID of the Minecraft server hosted by the Computer Society")
.kind(CommandOptionType::String)
.required(true)
})
pub fn register() -> CreateCommand {
CreateCommand::new("minecraft_add").description("Add a minecraft server").add_option(
CreateCommandOption::new(CommandOptionType::String, "server_id", "ID of the Minecraft server hosted by the Computer Society").required(true),
)
}
pub async fn run(command: &ApplicationCommandInteraction, ctx: &Context) -> String {
pub async fn run(command: &CommandInteraction, ctx: &Context) -> String {
// check if user has high enough permisssions
if let Some(msg) = is_admin(command, ctx).await {
return msg;
@ -233,16 +210,12 @@ pub(crate) mod server {
Some(x) => x,
};
let server_minecraft = if let CommandDataOptionValue::String(id) = command
.data
.options
.first()
.expect("Expected server_id option")
.resolved
.as_ref()
.expect("Expected server_id object")
let server_minecraft = if let Some(CommandDataOption {
value: CommandDataOptionValue::String(id),
..
}) = command.data.options.first()
{
id.to_owned()
id.to_string()
} else {
return String::from("Expected Server ID");
};
@ -279,7 +252,7 @@ pub(crate) mod server {
VALUES (?1, ?2)
",
)
.bind(*discord.as_u64() as i64)
.bind(discord.get() as i64)
.bind(minecraft)
.fetch_optional(db)
.await
@ -287,18 +260,18 @@ pub(crate) mod server {
}
pub(crate) mod list {
use serenity::builder::CreateApplicationCommand;
use serenity::all::CommandInteraction;
use serenity::builder::CreateCommand;
use serenity::client::Context;
use serenity::model::prelude::application_command::ApplicationCommandInteraction;
use skynet_discord_bot::common::database::DataBase;
use skynet_discord_bot::common::minecraft::{get_minecraft_config_server, server_information};
use skynet_discord_bot::{is_admin, Config};
pub fn register(command: &mut CreateApplicationCommand) -> &mut CreateApplicationCommand {
command.name("minecraft_list").description("List your minecraft servers")
pub fn register() -> CreateCommand {
CreateCommand::new("minecraft_list").description("List your minecraft servers")
}
pub async fn run(command: &ApplicationCommandInteraction, ctx: &Context) -> String {
pub async fn run(command: &CommandInteraction, ctx: &Context) -> String {
if let Some(msg) = is_admin(command, ctx).await {
return msg;
}
@ -348,27 +321,22 @@ pub(crate) mod server {
}
pub(crate) mod delete {
use serenity::builder::CreateApplicationCommand;
use serenity::all::{CommandDataOption, CommandDataOptionValue, CommandInteraction, CommandOptionType, CreateCommandOption};
use serenity::builder::CreateCommand;
use serenity::client::Context;
use serenity::model::application::command::CommandOptionType;
use serenity::model::id::GuildId;
use serenity::model::prelude::application_command::{ApplicationCommandInteraction, CommandDataOptionValue};
use skynet_discord_bot::common::database::DataBase;
use skynet_discord_bot::common::minecraft::Minecraft;
use skynet_discord_bot::is_admin;
use sqlx::{Error, Pool, Sqlite};
pub fn register(command: &mut CreateApplicationCommand) -> &mut CreateApplicationCommand {
command.name("minecraft_delete").description("Delete a minecraft server").create_option(|option| {
option
.name("server_id")
.description("ID of the Minecraft server hosted by the Computer Society")
.kind(CommandOptionType::String)
.required(true)
})
pub fn register() -> CreateCommand {
CreateCommand::new("minecraft_delete").description("Delete a minecraft server").add_option(
CreateCommandOption::new(CommandOptionType::String, "server_id", "ID of the Minecraft server hosted by the Computer Society").required(true),
)
}
pub async fn run(command: &ApplicationCommandInteraction, ctx: &Context) -> String {
pub async fn run(command: &CommandInteraction, ctx: &Context) -> String {
// check if user has high enough permisssions
if let Some(msg) = is_admin(command, ctx).await {
return msg;
@ -378,16 +346,12 @@ pub(crate) mod server {
Some(x) => x,
};
let server_minecraft = if let CommandDataOptionValue::String(id) = command
.data
.options
.first()
.expect("Expected server_id option")
.resolved
.as_ref()
.expect("Expected server_id object")
let server_minecraft = if let Some(CommandDataOption {
value: CommandDataOptionValue::String(id),
..
}) = command.data.options.first()
{
id.to_owned()
id.to_string()
} else {
return String::from("Expected Server ID");
};
@ -418,7 +382,7 @@ pub(crate) mod server {
WHERE server_discord = ?1 AND server_minecraft = ?2
",
)
.bind(*discord.as_u64() as i64)
.bind(discord.get() as i64)
.bind(minecraft)
.fetch_optional(db)
.await

View file

@ -1,11 +1,4 @@
use serenity::{
builder::CreateApplicationCommand,
client::Context,
model::{
application::interaction::application_command::ApplicationCommandInteraction,
prelude::{command::CommandOptionType, interaction::application_command::CommandDataOptionValue},
},
};
use serenity::client::Context;
use skynet_discord_bot::common::database::{DataBase, RoleAdder};
use skynet_discord_bot::is_admin;
@ -13,51 +6,40 @@ use sqlx::{Error, Pool, Sqlite};
pub mod edit {
use super::*;
use serenity::all::{CommandDataOption, CommandDataOptionValue, CommandInteraction, CommandOptionType, CreateCommand, CreateCommandOption};
pub async fn run(command: &ApplicationCommandInteraction, ctx: &Context) -> String {
pub async fn run(command: &CommandInteraction, ctx: &Context) -> String {
// check if user has high enough permisssions
if let Some(msg) = is_admin(command, ctx).await {
return msg;
}
let role_a = if let CommandDataOptionValue::Role(role) = command
.data
.options
.first()
.expect("Expected role option")
.resolved
.as_ref()
.expect("Expected role object")
let role_a = if let Some(CommandDataOption {
value: CommandDataOptionValue::Role(role),
..
}) = command.data.options.first()
{
role.id.to_owned()
role.to_owned()
} else {
return "Please provide a valid role for ``Role Current``".to_string();
};
let role_b = if let CommandDataOptionValue::Role(role) = command
.data
.options
.get(1)
.expect("Expected role option")
.resolved
.as_ref()
.expect("Expected role object")
let role_b = if let Some(CommandDataOption {
value: CommandDataOptionValue::Role(role),
..
}) = command.data.options.get(1)
{
role.id.to_owned()
role.to_owned()
} else {
return "Please provide a valid role for ``Role Current``".to_string();
};
let role_c = if let CommandDataOptionValue::Role(role) = command
.data
.options
.get(2)
.expect("Expected role option")
.resolved
.as_ref()
.expect("Expected role object")
let role_c = if let Some(CommandDataOption {
value: CommandDataOptionValue::Role(role),
..
}) = command.data.options.get(2)
{
role.id.to_owned()
role.to_owned()
} else {
return "Please provide a valid role for ``Role Current``".to_string();
};
@ -70,14 +52,15 @@ pub mod edit {
return "Role C cannot be same as A or B".to_string();
}
let mut delete = false;
if let Some(x) = command.data.options.get(3) {
let tmp = x.to_owned();
if let Some(CommandDataOptionValue::Boolean(z)) = tmp.resolved {
delete = z;
}
}
let delete = if let Some(CommandDataOption {
value: CommandDataOptionValue::Boolean(z),
..
}) = command.data.options.get(3)
{
*z
} else {
false
};
let db_lock = {
let data_read = ctx.data.read().await;
@ -124,32 +107,13 @@ pub mod edit {
}
}
pub fn register(command: &mut CreateApplicationCommand) -> &mut CreateApplicationCommand {
command
.name("roles_adder")
pub fn register() -> CreateCommand {
CreateCommand::new("roles_adder")
.description("Combine roles together to an new one")
.create_option(|option| {
option
.name("role_a")
.description("A role you want to add to Role B")
.kind(CommandOptionType::Role)
.required(true)
})
.create_option(|option| {
option
.name("role_b")
.description("A role you want to add to Role A")
.kind(CommandOptionType::Role)
.required(true)
})
.create_option(|option| option.name("role_c").description("Sum of A and B").kind(CommandOptionType::Role).required(true))
.create_option(|option| {
option
.name("delete")
.description("Delete this entry.")
.kind(CommandOptionType::Boolean)
.required(false)
})
.add_option(CreateCommandOption::new(CommandOptionType::Role, "role_a", "A role you want to add to Role B").required(true))
.add_option(CreateCommandOption::new(CommandOptionType::Role, "role_b", "A role you want to add to Role A").required(true))
.add_option(CreateCommandOption::new(CommandOptionType::Role, "role_c", "Sum of A and B").required(true))
.add_option(CreateCommandOption::new(CommandOptionType::Boolean, "delete", "Delete this entry.").required(false))
}
async fn add_server(db: &Pool<Sqlite>, server: &RoleAdder, delete: bool) -> Result<Option<RoleAdder>, Error> {
@ -160,10 +124,10 @@ pub mod edit {
WHERE server = ?1 AND role_a = ?2 AND role_b = ?3 AND role_c = ?4
",
)
.bind(*server.server.as_u64() as i64)
.bind(*server.role_a.as_u64() as i64)
.bind(*server.role_b.as_u64() as i64)
.bind(*server.role_c.as_u64() as i64)
.bind(server.server.get() as i64)
.bind(server.role_a.get() as i64)
.bind(server.role_b.get() as i64)
.bind(server.role_c.get() as i64)
.fetch_optional(db)
.await
} else {
@ -173,10 +137,10 @@ pub mod edit {
VALUES (?1, ?2, ?3, ?4)
",
)
.bind(*server.server.as_u64() as i64)
.bind(*server.role_a.as_u64() as i64)
.bind(*server.role_b.as_u64() as i64)
.bind(*server.role_c.as_u64() as i64)
.bind(server.server.get() as i64)
.bind(server.role_a.get() as i64)
.bind(server.role_b.get() as i64)
.bind(server.role_c.get() as i64)
.fetch_optional(db)
.await
}
@ -192,7 +156,7 @@ pub mod tools {
use skynet_discord_bot::common::database::RoleAdder;
use sqlx::{Pool, Sqlite};
pub async fn on_role_change(db: &Pool<Sqlite>, ctx: &Context, mut new_data: Member) {
pub async fn on_role_change(db: &Pool<Sqlite>, ctx: &Context, new_data: Member) {
// check if the role changed is part of the oens for this server
if let Ok(role_adders) = sqlx::query_as::<_, RoleAdder>(
r#"
@ -201,7 +165,7 @@ pub mod tools {
WHERE server = ?
"#,
)
.bind(*new_data.guild_id.as_u64() as i64)
.bind(new_data.guild_id.get() as i64)
.fetch_all(db)
.await
{

View file

@ -194,7 +194,7 @@ fn get_role_from_row(row: &SqliteRow, col: &str) -> RoleId {
match row.try_get(col) {
Ok(x) => {
let tmp: i64 = x;
RoleId(tmp as u64)
RoleId::new(tmp as u64)
}
_ => RoleId::from(0u64),
}
@ -226,7 +226,7 @@ pub async fn get_server_config(db: &Pool<Sqlite>, server: &GuildId) -> Option<Se
WHERE server = ?
"#,
)
.bind(*server.as_u64() as i64)
.bind(server.get() as i64)
.fetch_one(db)
.await
.ok()
@ -241,8 +241,8 @@ pub async fn get_server_member(db: &Pool<Sqlite>, server: &GuildId, member: &gui
WHERE server = ? AND discord = ?
"#,
)
.bind(*server.as_u64() as i64)
.bind(*member.user.id.as_u64() as i64)
.bind(server.get() as i64)
.bind(member.user.id.get() as i64)
.fetch_one(db)
.await
}

View file

@ -148,7 +148,7 @@ pub async fn get_minecraft_config_server(db: &Pool<Sqlite>, g_id: GuildId) -> Ve
WHERE server_discord = ?1
"#,
)
.bind(*g_id.as_u64() as i64)
.bind(g_id.get() as i64)
.fetch_all(db)
.await
.unwrap_or_default()

View file

@ -31,7 +31,7 @@ pub mod normal {
let mut members_all = members.len();
if let Ok(x) = server.members(ctx, None, None).await {
for mut member in x {
for member in x {
// members_changed acts as an override to only deal with teh users in it
if !members_changed.is_empty() && !members_changed.contains(&member.user.id) {
continue;
@ -83,7 +83,7 @@ pub mod normal {
set_server_numbers(&db, server, members_all as i64, members.len() as i64).await;
// small bit of logging to note changes over time
println!("{:?} Changes: New: +{}, Current: +{}/-{}", server.as_u64(), roles_set[0], roles_set[1], roles_set[2]);
println!("{:?} Changes: New: +{}, Current: +{}/-{}", server.get(), roles_set[0], roles_set[1], roles_set[2]);
}
pub async fn get_server_member_bulk(db: &Pool<Sqlite>, server: &GuildId) -> Vec<ServerMembersWolves> {
@ -99,7 +99,7 @@ pub mod normal {
)
"#,
)
.bind(*server.as_u64() as i64)
.bind(server.get() as i64)
.bind(get_now_iso(true))
.fetch_all(db)
.await
@ -116,13 +116,13 @@ pub mod normal {
)
.bind(past)
.bind(current)
.bind(*server.as_u64() as i64)
.bind(server.get() as i64)
.fetch_optional(db)
.await
{
Ok(_) => {}
Err(e) => {
println!("Failure to insert into {}", server.as_u64());
println!("Failure to insert into {}", server.get());
println!("{:?}", e);
}
}
@ -134,6 +134,8 @@ pub mod committee {
use crate::common::database::{DataBase, Wolves};
use crate::common::wolves::committees::Committees;
use crate::Config;
use serenity::all::EditRole;
use serenity::builder::CreateChannel;
use serenity::client::Context;
use serenity::model::channel::ChannelType;
use serenity::model::guild::Member;
@ -170,9 +172,16 @@ pub mod committee {
*/
pub async fn update_committees(db: &Pool<Sqlite>, ctx: &Context, config: &Config, members: &mut Vec<Member>) {
let server = config.committee_server;
let committee_member = config.committee_role;
let committee_member = RoleId::new(1226602779968274573);
let committees = get_committees(db).await;
let categories = vec![config.committee_category, ChannelId(1341457244973305927), ChannelId(1341457509717639279)];
let categories = vec![
// C&S Chats 1
ChannelId::new(1226606560973815839),
// C&S Chats 2
ChannelId::new(1341457244973305927),
// C&S Chats 3
ChannelId::new(1341457509717639279),
];
// information about the server
let roles = server.roles(&ctx).await.unwrap_or_default();
@ -216,7 +225,10 @@ pub mod committee {
Some(x) => Some(x.to_owned()),
None => {
// create teh role if it does not exist
match server.create_role(&ctx, |r| r.hoist(false).mentionable(true).name(&committee.name_full)).await {
match server
.create_role(&ctx, EditRole::new().name(&committee.name_full).hoist(false).mentionable(true))
.await
{
Ok(x) => Some(x),
Err(_) => None,
}
@ -226,7 +238,12 @@ pub mod committee {
// create teh channel if it does nto exist
if !channels_name.contains_key(&committee.name_profile) {
match server
.create_channel(&ctx, |c| c.name(&committee.name_profile).kind(ChannelType::Text).category(categories[category_index]))
.create_channel(
&ctx,
CreateChannel::new(&committee.name_profile)
.kind(ChannelType::Text)
.category(categories[category_index]),
)
.await
{
Ok(x) => {
@ -277,7 +294,7 @@ pub mod committee {
Some(x) => {
let mut tmp = x.to_owned();
if !tmp.is_empty() {
tmp.push(RoleId(1226602779968274573));
tmp.push(committee_member);
}
tmp
}
@ -309,7 +326,7 @@ pub mod committee {
// these roles are flavor roles, only there to make folks mentionable
member.add_roles(&ctx, &roles_add).await.unwrap_or_default();
} else {
member.remove_roles(&ctx, &[RoleId(1226602779968274573)]).await.unwrap_or_default();
member.remove_roles(&ctx, &[committee_member]).await.unwrap_or_default();
}
}
@ -347,8 +364,9 @@ pub mod committee {
FROM committees
"#,
)
.fetch_all(db)
.await.unwrap_or_else(|e| {
.fetch_all(db)
.await
.unwrap_or_else(|e| {
dbg!(e);
vec![]
})

View file

@ -144,13 +144,13 @@ pub mod cns {
",
)
.bind(name)
.bind(*server.as_u64() as i64)
.bind(server.get() as i64)
.fetch_optional(db)
.await
{
Ok(_) => {}
Err(e) => {
println!("Failure to set server name {}", server.as_u64());
println!("Failure to set server name {}", server.get());
println!("{:?}", e);
}
}
@ -168,7 +168,7 @@ pub mod cns {
)
"#,
)
.bind(*server.as_u64() as i64)
.bind(server.get() as i64)
.fetch_all(db)
.await
.unwrap_or_default()
@ -181,7 +181,7 @@ pub mod cns {
VALUES (?1, ?2, ?3)
",
)
.bind(*server.as_u64() as i64)
.bind(server.get() as i64)
.bind(&user.member_id)
.bind(&user.expiry)
.fetch_optional(db)
@ -189,7 +189,7 @@ pub mod cns {
{
Ok(_) => {}
Err(e) => {
println!("Failure to insert into ServerMembers {} {:?}", server.as_u64(), user);
println!("Failure to insert into ServerMembers {} {:?}", server.get(), user);
println!("{:?}", e);
}
}

View file

@ -3,9 +3,9 @@ pub mod common;
use chrono::{Datelike, SecondsFormat, Utc};
use dotenvy::dotenv;
use rand::{distributions::Alphanumeric, thread_rng, Rng};
use serenity::all::CommandInteraction;
use serenity::client::Context;
use serenity::model::id::{ChannelId, GuildId, RoleId};
use serenity::model::prelude::application_command::ApplicationCommandInteraction;
use serenity::prelude::TypeMapKey;
use std::{env, sync::Arc};
use tokio::sync::RwLock;
@ -57,9 +57,9 @@ pub fn get_config() -> Config {
mail_pass: "".to_string(),
wolves_url: "".to_string(),
wolves_api: "".to_string(),
committee_server: GuildId(0),
committee_role: RoleId(0),
committee_category: ChannelId(0),
committee_server: GuildId::new(0),
committee_role: RoleId::new(0),
committee_category: ChannelId::new(0),
};
if let Ok(x) = env::var("DATABASE_HOME") {
@ -98,17 +98,17 @@ pub fn get_config() -> Config {
if let Ok(x) = env::var("COMMITTEE_DISCORD") {
if let Ok(x) = x.trim().parse::<u64>() {
config.committee_server = GuildId(x);
config.committee_server = GuildId::new(x);
}
}
if let Ok(x) = env::var("COMMITTEE_DISCORD") {
if let Ok(x) = x.trim().parse::<u64>() {
config.committee_role = RoleId(x);
config.committee_role = RoleId::new(x);
}
}
if let Ok(x) = env::var("COMMITTEE_CATEGORY") {
if let Ok(x) = x.trim().parse::<u64>() {
config.committee_category = ChannelId(x);
config.committee_category = ChannelId::new(x);
}
}
@ -131,7 +131,7 @@ pub fn random_string(len: usize) -> String {
/**
For any time ye need to check if a user who calls a command has admin privlages
*/
pub async fn is_admin(command: &ApplicationCommandInteraction, ctx: &Context) -> Option<String> {
pub async fn is_admin(command: &CommandInteraction, ctx: &Context) -> Option<String> {
let mut admin = false;
let g_id = match command.guild_id {

View file

@ -1,14 +1,13 @@
pub mod commands;
use crate::commands::role_adder::tools::on_role_change;
use serenity::all::{ActivityData, Command, CreateMessage, EditInteractionResponse, GuildMemberUpdateEvent, Interaction};
use serenity::model::guild::Member;
use serenity::{
async_trait,
client::{Context, EventHandler},
model::{
application::{command::Command, interaction::Interaction},
gateway::{GatewayIntents, Ready},
prelude::Activity,
user::OnlineStatus,
},
Client,
@ -26,7 +25,7 @@ struct Handler;
#[async_trait]
impl EventHandler for Handler {
// handles previously linked accounts joining the server
async fn guild_member_addition(&self, ctx: Context, mut new_member: Member) {
async fn guild_member_addition(&self, ctx: Context, new_member: Member) {
let db_lock = {
let data_read = ctx.data.read().await;
data_read.get::<DataBase>().expect("Expected Config in TypeMap.").clone()
@ -84,7 +83,7 @@ Sign up on [UL Wolves]({}) and go to https://discord.com/channels/{}/{} and use
&config_server.bot_channel_id
);
if let Err(err) = new_member.user.direct_message(&ctx, |m| m.content(&msg)).await {
if let Err(err) = new_member.user.direct_message(&ctx, CreateMessage::new().content(&msg)).await {
dbg!(err);
}
}
@ -92,7 +91,7 @@ Sign up on [UL Wolves]({}) and go to https://discord.com/channels/{}/{} and use
}
// handles role updates
async fn guild_member_update(&self, ctx: Context, _old_data: Option<Member>, new_data: Member) {
async fn guild_member_update(&self, ctx: Context, _old_data: Option<Member>, new_data: Option<Member>, _: GuildMemberUpdateEvent) {
// get config/db
let db_lock = {
let data_read = ctx.data.read().await;
@ -102,24 +101,28 @@ Sign up on [UL Wolves]({}) and go to https://discord.com/channels/{}/{} and use
let db = db_lock.read().await;
// check if the role changed is part of the oens for this server
on_role_change(&db, &ctx, new_data).await;
if let Some(x) = new_data {
on_role_change(&db, &ctx, x).await;
}
}
async fn ready(&self, ctx: Context, ready: Ready) {
println!("[Main] {} is connected!", ready.user.name);
ctx.set_presence(Some(Activity::playing("with humanity's fate")), OnlineStatus::Online).await;
ctx.set_presence(Some(ActivityData::playing("with humanity's fate")), OnlineStatus::Online);
match Command::set_global_application_commands(&ctx.http, |commands| {
commands
.create_application_command(|command| commands::add_server::register(command))
.create_application_command(|command| commands::role_adder::edit::register(command))
.create_application_command(|command| commands::link_email::link::register(command))
.create_application_command(|command| commands::link_email::verify::register(command))
.create_application_command(|command| commands::minecraft::server::add::register(command))
.create_application_command(|command| commands::minecraft::server::list::register(command))
.create_application_command(|command| commands::minecraft::server::delete::register(command))
.create_application_command(|command| commands::minecraft::user::add::register(command))
})
match Command::set_global_commands(
&ctx.http,
vec![
commands::add_server::register(),
commands::role_adder::edit::register(),
commands::link_email::link::register(),
commands::link_email::verify::register(),
commands::minecraft::server::add::register(),
commands::minecraft::server::list::register(),
commands::minecraft::server::delete::register(),
commands::minecraft::user::add::register(),
],
)
.await
{
Ok(_) => {}
@ -130,7 +133,7 @@ Sign up on [UL Wolves]({}) and go to https://discord.com/channels/{}/{} and use
}
async fn interaction_create(&self, ctx: Context, interaction: Interaction) {
if let Interaction::ApplicationCommand(command) = interaction {
if let Interaction::Command(command) = interaction {
let _ = command.defer_ephemeral(&ctx.http).await;
//println!("Received command interaction: {:#?}", command);
@ -148,7 +151,7 @@ Sign up on [UL Wolves]({}) and go to https://discord.com/channels/{}/{} and use
_ => "not implemented :(".to_string(),
};
if let Err(why) = command.edit_original_interaction_response(&ctx.http, |response| response.content(content)).await {
if let Err(why) = command.edit_response(&ctx.http, EditInteractionResponse::new().content(content)).await {
println!("Cannot respond to slash command: {}", why);
}
}