some changes

This commit is contained in:
Gzod01 2024-05-04 20:06:09 +02:00
parent 62d7684234
commit b0dad06887
3 changed files with 68 additions and 32 deletions

1
.env
View file

@ -1,2 +1,3 @@
DISCORD_TOKEN=MTIzMDE2NjI2NTM3NzU5MTM5Ng.GVqsyl.ZvP2Kigd5VgP4eITdGqAGYfDw58YeXJYSJg4-A DISCORD_TOKEN=MTIzMDE2NjI2NTM3NzU5MTM5Ng.GVqsyl.ZvP2Kigd5VgP4eITdGqAGYfDw58YeXJYSJg4-A
MYSQL_PASS=gitpoi27940 MYSQL_PASS=gitpoi27940
RUST_BACKTRACE=0

View file

@ -0,0 +1,7 @@
{
"folders": [
{
"path": "."
}
]
}

View file

@ -1,17 +1,33 @@
// #![feature(internal_output_capture)]
use std::any::Any;
use std::sync::Arc;
use std::env; use std::env;
use std::fs::File; use std::fs::File;
use std::io::Read; use std::io::Read;
// use lazy_static::lazy_static; // use lazy_static::lazy_static;
use serenity::all::{ComponentInteractionDataKind, Interaction}; use serenity::all::{ComponentInteraction, ComponentInteractionDataKind, CreateInteractionResponse, CreateInteractionResponseMessage, Interaction};
use serenity::{async_trait, json}; use serenity::{async_trait, json};
use serenity::model::channel::Message; use serenity::model::channel::Message;
use serenity::model::event::Event;
use serenity::prelude::*; use serenity::prelude::*;
use serde_json::json; use serde_json::json;
use serde_json::Map; use serde_json::Map;
// use mysql::prelude::Queryable; // use mysql::prelude::Queryable;
struct LogHandler;
#[async_trait]
impl RawEventHandler for LogHandler{
async fn raw_event(&self, _ctx:Context, _ev:Event){
//let guild_id = _ev.guild_id.get();
}
}
struct Handler; struct Handler;
// lazy_static! { // lazy_static! {
@ -49,43 +65,53 @@ impl EventHandler for Handler {
} }
} }
async fn interaction_create(&self, ctx: Context, interaction: Interaction){ async fn interaction_create(&self, ctx: Context, interaction: Interaction){
if let Interaction::Component(comp) = interaction{ if let Interaction::Component(comp) = interaction{
println!("Received component interaction {comp:?}"); println!("Received component interaction {comp:?}");
if matches!(comp.data.kind, ComponentInteractionDataKind::Button){ if matches!(comp.data.kind, ComponentInteractionDataKind::Button){
let compid = comp.data.custom_id; let interaction_output:String = match (button_interaction_role(comp.clone(), ctx.clone()).await) {
// let guild_id=comp.guild_id; Ok(role) => format!("Le rôle <@#{role:?}>",role=role.to_owned()),
// let channel_id=comp.channel_id; Err(err) => format!("```\n{err:?}\n```",err=err)
let mut role:String="nulled".to_string(); };
println!("helloworld {compid:?}"); let interaction_response:CreateInteractionResponse = CreateInteractionResponse::Message(CreateInteractionResponseMessage::new().ephemeral(true).content(interaction_output));
// let mut conn = *CONN.lock(); let _ = comp.create_response(ctx, interaction_response).await;
// conn.lock().query_iter("select role_id from buttons_callback WHERE
// guild_id='{guild_id:?}' AND
// channel_id='{channel_id:?}' AND
// custom_id='{compid:?}'")
// .unwrap()
// .for_each(|row| {
// let r:String = mysql::from_row(row.unwrap());
// role=r;
// });
let mut json_file = File::open("/home/aurel/gzod_site/discordBots/disco_wero_bot/buttons_roles.json").expect("oh no, file not exist!!!!!!!!");
let mut json_file_content = String::new();
let _ = json_file.read_to_string(&mut json_file_content).expect("can't read file");
let json:Map<String,serde_json::Value> =serde_json::from_str(&json_file_content).expect("JSON was not well-formatted");
if json.contains_key("{compid:?}"){
role = json["{compid:?}"].to_string();
}
if role=="nulled"{
println!("{compid:?} does not exist");
return;
}
let _= comp.member.unwrap().add_role(ctx,role.parse::<u64>().unwrap()).await.expect("oh no");
} }
} }
} }
} }
async fn button_interaction_role (comp:ComponentInteraction, ctx:Context)->Result<String,String> {
let compid = comp.data.custom_id;
// let guild_id=comp.guild_id;
// let channel_id=comp.channel_id;
let mut role:String="nulled".to_string();
println!("helloworld {compid:?}");
// let mut conn = *CONN.lock();
// conn.lock().query_iter("select role_id from buttons_callback WHERE
// guild_id='{guild_id:?}' AND
// channel_id='{channel_id:?}' AND
// custom_id='{compid:?}'")
// .unwrap()
// .for_each(|row| {
// let r:String = mysql::from_row(row.unwrap());
// role=r;
// });
let mut json_file = File::open("/home/aurel/gzod_site/discordBots/disco_wero_bot/buttons_role.json").expect("oh no, file not exist!!!!!!!!");
let mut json_file_content = String::new();
let _ = json_file.read_to_string(&mut json_file_content).expect("can't read file");
let jsonobj:serde_json::Value =serde_json::from_str(&json_file_content).expect("JSON was not well-formatted");
if jsonobj[&compid] != serde_json::Value::Null{
role = jsonobj[&compid].as_str().expect("OHOH").to_string();
}
if role=="nulled"{
panic!("{compid:?} does not exist");
}
println!("{compid:?} role is : {role:?}");
println!("{}",role.to_owned());
if let Err(_err)= comp.member.unwrap().add_role(ctx,(&role).to_owned().parse::<u64>().unwrap()).await {
return Err(format!("{_err:?}"));
}
return Ok(role);
}
#[tokio::main] #[tokio::main]
async fn main() { async fn main() {
// Login with a bot token from the environment // Login with a bot token from the environment
@ -93,7 +119,9 @@ async fn main() {
// Set gateway intents, which decides what events the bot will be notified about // Set gateway intents, which decides what events the bot will be notified about
let intents = GatewayIntents::GUILD_MESSAGES let intents = GatewayIntents::GUILD_MESSAGES
| GatewayIntents::DIRECT_MESSAGES | GatewayIntents::DIRECT_MESSAGES
| GatewayIntents::MESSAGE_CONTENT; | GatewayIntents::MESSAGE_CONTENT
| GatewayIntents::GUILDS
| GatewayIntents::GUILD_MEMBERS;
// Create a new instance of the Client, logging in as a bot. // Create a new instance of the Client, logging in as a bot.
let mut client = Client::builder(&token, intents) let mut client = Client::builder(&token, intents)