Compare commits
No commits in common. "d9211dca9a700b298b08b3cb64cfb0a31615dae4" and "9452c0ac2ed84f31e9dd1314ac1e6e18c98bb07e" have entirely different histories.
d9211dca9a
...
9452c0ac2e
7 changed files with 39 additions and 138 deletions
|
@ -1,5 +0,0 @@
|
||||||
-- temp table to allow folks to verify by committee email.
|
|
||||||
-- delete the col in teh server table
|
|
||||||
ALTER TABLE servers ADD COLUMN bot_channel_id integer DEFAULT 0;
|
|
||||||
ALTER TABLE servers ADD COLUMN server_name text NOT NULL;
|
|
||||||
ALTER TABLE servers ADD COLUMN wolves_link text NOT NULL;
|
|
|
@ -1,2 +1,2 @@
|
||||||
[toolchain]
|
[toolchain]
|
||||||
channel = "1.80"
|
channel = "1.80"
|
|
@ -39,60 +39,18 @@ pub async fn run(command: &ApplicationCommandInteraction, ctx: &Context) -> Stri
|
||||||
.as_ref()
|
.as_ref()
|
||||||
.expect("Expected role object")
|
.expect("Expected role object")
|
||||||
{
|
{
|
||||||
role.id.to_owned()
|
Some(role.id.to_owned())
|
||||||
} else {
|
} else {
|
||||||
return "Please provide a valid role for ``Role Current``".to_string();
|
return "Please provide a valid role for ``Role Current``".to_string();
|
||||||
};
|
};
|
||||||
|
|
||||||
let mut role_past = None;
|
let mut role_past = None;
|
||||||
if let Some(x) = command.data.options.get(5) {
|
if let Some(x) = command.data.options.get(2) {
|
||||||
if let Some(CommandDataOptionValue::Role(role)) = &x.resolved {
|
if let Some(CommandDataOptionValue::Role(role)) = &x.resolved {
|
||||||
role_past = Some(role.id.to_owned());
|
role_past = Some(role.id.to_owned());
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
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")
|
|
||||||
{
|
|
||||||
channel.id.to_owned()
|
|
||||||
} else {
|
|
||||||
return "Please provide a valid channel for ``Bot Channel``".to_string();
|
|
||||||
};
|
|
||||||
|
|
||||||
let server_name = if let CommandDataOptionValue::String(name) = command
|
|
||||||
.data
|
|
||||||
.options
|
|
||||||
.get(3)
|
|
||||||
.expect("Expected Server Name option")
|
|
||||||
.resolved
|
|
||||||
.as_ref()
|
|
||||||
.expect("Expected Server Name object")
|
|
||||||
{
|
|
||||||
name
|
|
||||||
} else {
|
|
||||||
&"UL Computer Society".to_string()
|
|
||||||
};
|
|
||||||
|
|
||||||
let wolves_link = if let CommandDataOptionValue::String(wolves) = command
|
|
||||||
.data
|
|
||||||
.options
|
|
||||||
.get(4)
|
|
||||||
.expect("Expected Wolves Link option")
|
|
||||||
.resolved
|
|
||||||
.as_ref()
|
|
||||||
.expect("Expected Server Name object")
|
|
||||||
{
|
|
||||||
wolves
|
|
||||||
} else {
|
|
||||||
&"https://ulwolves.ie/society/computer".to_string()
|
|
||||||
};
|
|
||||||
|
|
||||||
let db_lock = {
|
let db_lock = {
|
||||||
let data_read = ctx.data.read().await;
|
let data_read = ctx.data.read().await;
|
||||||
data_read.get::<DataBase>().expect("Expected Databse in TypeMap.").clone()
|
data_read.get::<DataBase>().expect("Expected Databse in TypeMap.").clone()
|
||||||
|
@ -106,9 +64,6 @@ pub async fn run(command: &ApplicationCommandInteraction, ctx: &Context) -> Stri
|
||||||
role_current,
|
role_current,
|
||||||
member_past: 0,
|
member_past: 0,
|
||||||
member_current: 0,
|
member_current: 0,
|
||||||
bot_channel_id,
|
|
||||||
server_name: server_name.to_owned(),
|
|
||||||
wolves_link: wolves_link.to_string(),
|
|
||||||
};
|
};
|
||||||
|
|
||||||
match add_server(&db, ctx, &server_data).await {
|
match add_server(&db, ctx, &server_data).await {
|
||||||
|
@ -140,27 +95,6 @@ pub fn register(command: &mut CreateApplicationCommand) -> &mut CreateApplicatio
|
||||||
.kind(CommandOptionType::Role)
|
.kind(CommandOptionType::Role)
|
||||||
.required(true)
|
.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("server_name")
|
|
||||||
.description("Name of the Discord Server.")
|
|
||||||
.kind(CommandOptionType::String)
|
|
||||||
.required(true)
|
|
||||||
})
|
|
||||||
.create_option(|option| {
|
|
||||||
option
|
|
||||||
.name("wolves_link")
|
|
||||||
.description("Link to the Club/Society on UL Wolves.")
|
|
||||||
.kind(CommandOptionType::String)
|
|
||||||
.required(true)
|
|
||||||
})
|
|
||||||
.create_option(|option| {
|
.create_option(|option| {
|
||||||
option
|
option
|
||||||
.name("role_past")
|
.name("role_past")
|
||||||
|
@ -173,20 +107,18 @@ pub fn register(command: &mut CreateApplicationCommand) -> &mut CreateApplicatio
|
||||||
async fn add_server(db: &Pool<Sqlite>, ctx: &Context, server: &Servers) -> Result<Option<Servers>, Error> {
|
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 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.as_u64() as i64);
|
||||||
|
let role_current = server.role_current.map(|x| *x.as_u64() as i64);
|
||||||
|
|
||||||
let insert = sqlx::query_as::<_, Servers>(
|
let insert = sqlx::query_as::<_, Servers>(
|
||||||
"
|
"
|
||||||
INSERT OR REPLACE INTO servers (server, wolves_api, role_past, role_current, bot_channel_id, server_name, wolves_link)
|
INSERT OR REPLACE INTO servers (server, wolves_api, role_past, role_current)
|
||||||
VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7)
|
VALUES (?1, ?2, ?3, ?4)
|
||||||
",
|
",
|
||||||
)
|
)
|
||||||
.bind(*server.server.as_u64() as i64)
|
.bind(*server.server.as_u64() as i64)
|
||||||
.bind(&server.wolves_api)
|
.bind(&server.wolves_api)
|
||||||
.bind(role_past)
|
.bind(role_past)
|
||||||
.bind(*server.role_current.as_u64() as i64)
|
.bind(role_current)
|
||||||
.bind(*server.bot_channel_id.as_u64() as i64)
|
|
||||||
.bind(&server.server_name)
|
|
||||||
.bind(&server.wolves_link)
|
|
||||||
.fetch_optional(db)
|
.fetch_optional(db)
|
||||||
.await;
|
.await;
|
||||||
|
|
||||||
|
@ -201,7 +133,7 @@ async fn add_server(db: &Pool<Sqlite>, ctx: &Context, server: &Servers) -> Resul
|
||||||
if x.role_current != server.role_current {
|
if x.role_current != server.role_current {
|
||||||
result.0 = true;
|
result.0 = true;
|
||||||
result.1 = true;
|
result.1 = true;
|
||||||
result.2 = Some(x.role_current);
|
result.2 = x.role_current;
|
||||||
}
|
}
|
||||||
if x.role_past != server.role_past {
|
if x.role_past != server.role_past {
|
||||||
result.0 = true;
|
result.0 = true;
|
||||||
|
|
|
@ -172,7 +172,7 @@ pub mod link {
|
||||||
let creds = Credentials::new(config.mail_user.clone(), config.mail_pass.clone());
|
let creds = Credentials::new(config.mail_user.clone(), config.mail_pass.clone());
|
||||||
|
|
||||||
// Open a remote connection to gmail using STARTTLS
|
// Open a remote connection to gmail using STARTTLS
|
||||||
let mailer = SmtpTransport::starttls_relay(&config.mail_smtp)?.credentials(creds).build();
|
let mailer = SmtpTransport::starttls_relay(&config.mail_smtp).unwrap().credentials(creds).build();
|
||||||
|
|
||||||
// Send the email
|
// Send the email
|
||||||
mailer.send(&email)
|
mailer.send(&email)
|
||||||
|
|
|
@ -184,7 +184,7 @@ pub mod link {
|
||||||
let creds = Credentials::new(config.mail_user.clone(), config.mail_pass.clone());
|
let creds = Credentials::new(config.mail_user.clone(), config.mail_pass.clone());
|
||||||
|
|
||||||
// Open a remote connection to gmail using STARTTLS
|
// Open a remote connection to gmail using STARTTLS
|
||||||
let mailer = SmtpTransport::starttls_relay(&config.mail_smtp)?.credentials(creds).build();
|
let mailer = SmtpTransport::starttls_relay(&config.mail_smtp).unwrap().credentials(creds).build();
|
||||||
|
|
||||||
// Send the email
|
// Send the email
|
||||||
mailer.send(&email)
|
mailer.send(&email)
|
||||||
|
@ -352,8 +352,10 @@ pub mod verify {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if !member.roles.contains(&role_current) {
|
if let Some(role) = &role_current {
|
||||||
roles.push(role_current.to_owned());
|
if !member.roles.contains(role) {
|
||||||
|
roles.push(role.to_owned());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if let Err(e) = member.add_roles(&ctx, &roles).await {
|
if let Err(e) = member.add_roles(&ctx, &roles).await {
|
||||||
|
|
44
src/lib.rs
44
src/lib.rs
|
@ -3,7 +3,7 @@ use serde::{Deserialize, Serialize};
|
||||||
use serenity::{
|
use serenity::{
|
||||||
model::{
|
model::{
|
||||||
guild,
|
guild,
|
||||||
id::{ChannelId, GuildId, RoleId},
|
id::{GuildId, RoleId},
|
||||||
},
|
},
|
||||||
prelude::TypeMapKey,
|
prelude::TypeMapKey,
|
||||||
};
|
};
|
||||||
|
@ -228,13 +228,9 @@ pub struct Servers {
|
||||||
pub server: GuildId,
|
pub server: GuildId,
|
||||||
pub wolves_api: String,
|
pub wolves_api: String,
|
||||||
pub role_past: Option<RoleId>,
|
pub role_past: Option<RoleId>,
|
||||||
pub role_current: RoleId,
|
pub role_current: Option<RoleId>,
|
||||||
pub member_past: i64,
|
pub member_past: i64,
|
||||||
pub member_current: i64,
|
pub member_current: i64,
|
||||||
pub bot_channel_id: ChannelId,
|
|
||||||
// these can be removed in teh future with an API update
|
|
||||||
pub server_name: String,
|
|
||||||
pub wolves_link: String,
|
|
||||||
}
|
}
|
||||||
impl<'r> FromRow<'r, SqliteRow> for Servers {
|
impl<'r> FromRow<'r, SqliteRow> for Servers {
|
||||||
fn from_row(row: &'r SqliteRow) -> Result<Self, Error> {
|
fn from_row(row: &'r SqliteRow) -> Result<Self, Error> {
|
||||||
|
@ -254,14 +250,15 @@ impl<'r> FromRow<'r, SqliteRow> for Servers {
|
||||||
let role_current = match row.try_get("role_current") {
|
let role_current = match row.try_get("role_current") {
|
||||||
Ok(x) => {
|
Ok(x) => {
|
||||||
let tmp: i64 = x;
|
let tmp: i64 = x;
|
||||||
RoleId::from(tmp as u64)
|
if tmp == 0 {
|
||||||
|
None
|
||||||
|
} else {
|
||||||
|
Some(RoleId::from(tmp as u64))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
_ => RoleId::from(0u64),
|
_ => None,
|
||||||
};
|
};
|
||||||
|
|
||||||
let bot_channel_tmp: i64 = row.try_get("bot_channel_id")?;
|
|
||||||
let bot_channel_id = ChannelId::from(bot_channel_tmp as u64);
|
|
||||||
|
|
||||||
Ok(Self {
|
Ok(Self {
|
||||||
server,
|
server,
|
||||||
wolves_api: row.try_get("wolves_api")?,
|
wolves_api: row.try_get("wolves_api")?,
|
||||||
|
@ -269,9 +266,6 @@ impl<'r> FromRow<'r, SqliteRow> for Servers {
|
||||||
role_current,
|
role_current,
|
||||||
member_past: row.try_get("member_past")?,
|
member_past: row.try_get("member_past")?,
|
||||||
member_current: row.try_get("member_current")?,
|
member_current: row.try_get("member_current")?,
|
||||||
bot_channel_id,
|
|
||||||
server_name: row.try_get("server_name")?,
|
|
||||||
wolves_link: row.try_get("wolves_link")?,
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -306,7 +300,7 @@ pub async fn db_init(config: &Config) -> Result<Pool<Sqlite>, Error> {
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
// migrations are amazing!
|
// migrations are amazing!
|
||||||
sqlx::migrate!("./db/migrations").run(&pool).await?;
|
sqlx::migrate!("./db/migrations").run(&pool).await.unwrap();
|
||||||
|
|
||||||
Ok(pool)
|
Ok(pool)
|
||||||
}
|
}
|
||||||
|
@ -409,9 +403,11 @@ pub mod set_roles {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if !member.roles.contains(role_current) {
|
if let Some(role) = &role_current {
|
||||||
roles_set[1] += 1;
|
if !member.roles.contains(role) {
|
||||||
roles.push(role_current.to_owned());
|
roles_set[1] += 1;
|
||||||
|
roles.push(role.to_owned());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if let Err(e) = member.add_roles(ctx, &roles).await {
|
if let Err(e) = member.add_roles(ctx, &roles).await {
|
||||||
|
@ -426,11 +422,13 @@ pub mod set_roles {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if member.roles.contains(role_current) {
|
if let Some(role) = &role_current {
|
||||||
roles_set[2] += 1;
|
if member.roles.contains(role) {
|
||||||
// if theya re not a current member and have the role then remove it
|
roles_set[2] += 1;
|
||||||
if let Err(e) = member.remove_role(ctx, role_current).await {
|
// if theya re not a current member and have the role then remove it
|
||||||
println!("{:?}", e);
|
if let Err(e) = member.remove_role(ctx, role).await {
|
||||||
|
println!("{:?}", e);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
34
src/main.rs
34
src/main.rs
|
@ -7,8 +7,6 @@ use serenity::{
|
||||||
application::{command::Command, interaction::Interaction},
|
application::{command::Command, interaction::Interaction},
|
||||||
gateway::{GatewayIntents, Ready},
|
gateway::{GatewayIntents, Ready},
|
||||||
guild,
|
guild,
|
||||||
prelude::Activity,
|
|
||||||
user::OnlineStatus,
|
|
||||||
},
|
},
|
||||||
Client,
|
Client,
|
||||||
};
|
};
|
||||||
|
@ -42,44 +40,20 @@ impl EventHandler for Handler {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if !new_member.roles.contains(&config.role_current) {
|
if let Some(role) = &config.role_current {
|
||||||
roles.push(config.role_current.to_owned());
|
if !new_member.roles.contains(role) {
|
||||||
|
roles.push(role.to_owned());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if let Err(e) = new_member.add_roles(&ctx, &roles).await {
|
if let Err(e) = new_member.add_roles(&ctx, &roles).await {
|
||||||
println!("{:?}", e);
|
println!("{:?}", e);
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
let msg = format!(
|
|
||||||
r#"
|
|
||||||
Welcome {} to the {} server!
|
|
||||||
Sign up on [UL Wolves]({}) and go to https://discord.com/channels/{}/{} and use ``/link_wolves`` to get full access.
|
|
||||||
"#,
|
|
||||||
new_member.display_name(),
|
|
||||||
&config.server_name,
|
|
||||||
&config.wolves_link,
|
|
||||||
&config.server,
|
|
||||||
&config.bot_channel_id
|
|
||||||
);
|
|
||||||
// let msg = format!(
|
|
||||||
// "Welcome {} to the {} server! \n\
|
|
||||||
// Sign up on [UL Wolves]({}) and go to https://discord.com/channels/{}/{} and use ``/link_wolves`` to get full access.",
|
|
||||||
// new_member.display_name(),
|
|
||||||
// &config.server_name,
|
|
||||||
// &config.wolves_link,
|
|
||||||
// &config.server,
|
|
||||||
// &config.bot_channel_id
|
|
||||||
// );
|
|
||||||
|
|
||||||
if let Err(err) = new_member.user.direct_message(&ctx, |m| m.content(&msg)).await {
|
|
||||||
dbg!(err);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn ready(&self, ctx: Context, ready: Ready) {
|
async fn ready(&self, ctx: Context, ready: Ready) {
|
||||||
println!("[Main] {} is connected!", ready.user.name);
|
println!("[Main] {} is connected!", ready.user.name);
|
||||||
ctx.set_presence(Some(Activity::playing("with humanity's fate")), OnlineStatus::Online).await;
|
|
||||||
|
|
||||||
match Command::set_global_application_commands(&ctx.http, |commands| {
|
match Command::set_global_application_commands(&ctx.http, |commands| {
|
||||||
commands
|
commands
|
||||||
|
|
Loading…
Reference in a new issue