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,9 +65,21 @@ 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 interaction_output:String = match (button_interaction_role(comp.clone(), ctx.clone()).await) {
Ok(role) => format!("Le rôle <@#{role:?}>",role=role.to_owned()),
Err(err) => format!("```\n{err:?}\n```",err=err)
};
let interaction_response:CreateInteractionResponse = CreateInteractionResponse::Message(CreateInteractionResponseMessage::new().ephemeral(true).content(interaction_output));
let _ = comp.create_response(ctx, interaction_response).await;
}
}
}
}
async fn button_interaction_role (comp:ComponentInteraction, ctx:Context)->Result<String,String> {
let compid = comp.data.custom_id; let compid = comp.data.custom_id;
// let guild_id=comp.guild_id; // let guild_id=comp.guild_id;
// let channel_id=comp.channel_id; // let channel_id=comp.channel_id;
@ -67,25 +95,23 @@ impl EventHandler for Handler {
// let r:String = mysql::from_row(row.unwrap()); // let r:String = mysql::from_row(row.unwrap());
// role=r; // 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 = 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 mut json_file_content = String::new();
let _ = json_file.read_to_string(&mut json_file_content).expect("can't read file"); 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"); let jsonobj:serde_json::Value =serde_json::from_str(&json_file_content).expect("JSON was not well-formatted");
if json.contains_key("{compid:?}"){ if jsonobj[&compid] != serde_json::Value::Null{
role = json["{compid:?}"].to_string(); role = jsonobj[&compid].as_str().expect("OHOH").to_string();
} }
if role=="nulled"{ if role=="nulled"{
println!("{compid:?} does not exist"); panic!("{compid:?} does not exist");
return;
} }
let _= comp.member.unwrap().add_role(ctx,role.parse::<u64>().unwrap()).await.expect("oh no"); 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)