forked from Skynet/discord-bot
feat: fmt and clippy
This commit is contained in:
parent
9b42187b22
commit
802f3fce59
2 changed files with 245 additions and 224 deletions
9
.rustfmt.toml
Normal file
9
.rustfmt.toml
Normal file
|
@ -0,0 +1,9 @@
|
||||||
|
max_width = 150
|
||||||
|
single_line_if_else_max_width = 100
|
||||||
|
chain_width = 100
|
||||||
|
fn_params_layout = "Compressed"
|
||||||
|
#control_brace_style = "ClosingNextLine"
|
||||||
|
#brace_style = "PreferSameLine"
|
||||||
|
struct_lit_width = 0
|
||||||
|
tab_spaces = 2
|
||||||
|
use_small_heuristics = "Max"
|
56
src/main.rs
56
src/main.rs
|
@ -1,14 +1,25 @@
|
||||||
use std::env;
|
|
||||||
use std::sync::Arc;
|
|
||||||
use std::sync::atomic::{AtomicBool, Ordering};
|
|
||||||
use std::time::Duration;
|
|
||||||
use dotenvy::dotenv;
|
use dotenvy::dotenv;
|
||||||
use serenity::async_trait;
|
use serenity::{
|
||||||
use serenity::model::gateway::{GatewayIntents, Ready};
|
async_trait,
|
||||||
use serenity::model::guild::Member;
|
client::{Context, EventHandler},
|
||||||
use serenity::model::id::GuildId;
|
model::{
|
||||||
use serenity::prelude::*;
|
gateway::{GatewayIntents, Ready},
|
||||||
use serenity::model::prelude::RoleId;
|
guild::Member,
|
||||||
|
id::GuildId,
|
||||||
|
prelude::RoleId,
|
||||||
|
},
|
||||||
|
prelude::TypeMapKey,
|
||||||
|
Client,
|
||||||
|
};
|
||||||
|
use std::{
|
||||||
|
env,
|
||||||
|
sync::{
|
||||||
|
atomic::{AtomicBool, Ordering},
|
||||||
|
Arc,
|
||||||
|
},
|
||||||
|
time::Duration,
|
||||||
|
};
|
||||||
|
use tokio::sync::RwLock;
|
||||||
|
|
||||||
struct Handler {
|
struct Handler {
|
||||||
is_loop_running: AtomicBool,
|
is_loop_running: AtomicBool,
|
||||||
|
@ -32,10 +43,10 @@ impl EventHandler for Handler {
|
||||||
if members.contains(&new_member.user.name) {
|
if members.contains(&new_member.user.name) {
|
||||||
let mut roles = vec![];
|
let mut roles = vec![];
|
||||||
|
|
||||||
if !new_member.roles.contains(&config.member_role_past){
|
if !new_member.roles.contains(&config.member_role_past) {
|
||||||
roles.push(config.member_role_past);
|
roles.push(config.member_role_past);
|
||||||
}
|
}
|
||||||
if !new_member.roles.contains(&config.member_role_current){
|
if !new_member.roles.contains(&config.member_role_current) {
|
||||||
roles.push(config.member_role_current);
|
roles.push(config.member_role_current);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -99,7 +110,7 @@ struct Members;
|
||||||
impl TypeMapKey for Members {
|
impl TypeMapKey for Members {
|
||||||
type Value = Arc<RwLock<Vec<String>>>;
|
type Value = Arc<RwLock<Vec<String>>>;
|
||||||
}
|
}
|
||||||
async fn bulk_check(ctx: Arc<Context>){
|
async fn bulk_check(ctx: Arc<Context>) {
|
||||||
let config_lock = {
|
let config_lock = {
|
||||||
let data_read = ctx.data.read().await;
|
let data_read = ctx.data.read().await;
|
||||||
data_read.get::<Config>().expect("Expected Config in TypeMap.").clone()
|
data_read.get::<Config>().expect("Expected Config in TypeMap.").clone()
|
||||||
|
@ -112,7 +123,7 @@ async fn bulk_check(ctx: Arc<Context>){
|
||||||
};
|
};
|
||||||
let members = members_lock.read().await;
|
let members = members_lock.read().await;
|
||||||
|
|
||||||
let mut roles_set = [0,0,0];
|
let mut roles_set = [0, 0, 0];
|
||||||
let mut res = MembersCount {
|
let mut res = MembersCount {
|
||||||
members: 0,
|
members: 0,
|
||||||
members_current: 0,
|
members_current: 0,
|
||||||
|
@ -122,11 +133,11 @@ async fn bulk_check(ctx: Arc<Context>){
|
||||||
if members.contains(&member.user.name) {
|
if members.contains(&member.user.name) {
|
||||||
let mut roles = vec![];
|
let mut roles = vec![];
|
||||||
|
|
||||||
if !member.roles.contains(&config.member_role_past){
|
if !member.roles.contains(&config.member_role_past) {
|
||||||
roles_set[0] += 1;
|
roles_set[0] += 1;
|
||||||
roles.push(config.member_role_past);
|
roles.push(config.member_role_past);
|
||||||
}
|
}
|
||||||
if !member.roles.contains(&config.member_role_current){
|
if !member.roles.contains(&config.member_role_current) {
|
||||||
roles_set[1] += 1;
|
roles_set[1] += 1;
|
||||||
roles.push(config.member_role_current);
|
roles.push(config.member_role_current);
|
||||||
}
|
}
|
||||||
|
@ -142,10 +153,10 @@ async fn bulk_check(ctx: Arc<Context>){
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if member.roles.contains(&config.member_role_past){
|
if member.roles.contains(&config.member_role_past) {
|
||||||
res.members += 1;
|
res.members += 1;
|
||||||
}
|
}
|
||||||
if member.roles.contains(&config.member_role_current){
|
if member.roles.contains(&config.member_role_current) {
|
||||||
res.members_current += 1;
|
res.members_current += 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -166,7 +177,7 @@ async fn bulk_check(ctx: Arc<Context>){
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn fetch_accounts(ctx: Arc<Context>){
|
async fn fetch_accounts(ctx: Arc<Context>) {
|
||||||
let config_lock = {
|
let config_lock = {
|
||||||
let data_read = ctx.data.read().await;
|
let data_read = ctx.data.read().await;
|
||||||
data_read.get::<Config>().expect("Expected Config in TypeMap.").clone()
|
data_read.get::<Config>().expect("Expected Config in TypeMap.").clone()
|
||||||
|
@ -193,7 +204,9 @@ async fn main() {
|
||||||
let intents = GatewayIntents::GUILDS | GatewayIntents::GUILD_MESSAGES | GatewayIntents::MESSAGE_CONTENT | GatewayIntents::GUILD_MEMBERS;
|
let intents = GatewayIntents::GUILDS | GatewayIntents::GUILD_MESSAGES | GatewayIntents::MESSAGE_CONTENT | GatewayIntents::GUILD_MEMBERS;
|
||||||
// Build our client.
|
// Build our client.
|
||||||
let mut client = Client::builder(&config.discord_token, intents)
|
let mut client = Client::builder(&config.discord_token, intents)
|
||||||
.event_handler(Handler { is_loop_running: AtomicBool::new(false)})
|
.event_handler(Handler {
|
||||||
|
is_loop_running: AtomicBool::new(false),
|
||||||
|
})
|
||||||
.await
|
.await
|
||||||
.expect("Error creating client");
|
.expect("Error creating client");
|
||||||
|
|
||||||
|
@ -206,10 +219,10 @@ async fn main() {
|
||||||
// a list of all current members
|
// a list of all current members
|
||||||
data.insert::<Members>(Arc::new(RwLock::new(vec![])));
|
data.insert::<Members>(Arc::new(RwLock::new(vec![])));
|
||||||
|
|
||||||
|
// make config available top all, strangely its easier to keep it in a shared lock state.
|
||||||
data.insert::<Config>(Arc::new(RwLock::new(config)));
|
data.insert::<Config>(Arc::new(RwLock::new(config)));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// Finally, start a single shard, and start listening to events.
|
// Finally, start a single shard, and start listening to events.
|
||||||
//
|
//
|
||||||
// Shards will automatically attempt to reconnect, and will perform
|
// Shards will automatically attempt to reconnect, and will perform
|
||||||
|
@ -219,7 +232,6 @@ async fn main() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
struct Config {
|
struct Config {
|
||||||
server: GuildId,
|
server: GuildId,
|
||||||
member_role_current: RoleId,
|
member_role_current: RoleId,
|
||||||
|
|
Loading…
Reference in a new issue