forked from Skynet/discord-bot
feat: get data does not need discord running
This commit is contained in:
parent
670a85d2ef
commit
55c042861f
1 changed files with 12 additions and 73 deletions
|
@ -1,20 +1,9 @@
|
||||||
use skynet_discord_bot::{db_init, get_config, get_server_config_bulk, Accounts, Config, DataBase, Servers};
|
use skynet_discord_bot::{db_init, get_config, get_server_config_bulk, Accounts, Config, Servers};
|
||||||
use std::{process, sync::Arc};
|
|
||||||
|
|
||||||
use serde::Deserialize;
|
use serde::Deserialize;
|
||||||
use serenity::{
|
use serenity::model::id::GuildId;
|
||||||
async_trait,
|
|
||||||
client::{Context, EventHandler},
|
|
||||||
model::{
|
|
||||||
gateway::{GatewayIntents, Ready},
|
|
||||||
id::GuildId,
|
|
||||||
},
|
|
||||||
Client,
|
|
||||||
};
|
|
||||||
use sqlx::{Pool, Sqlite};
|
use sqlx::{Pool, Sqlite};
|
||||||
|
|
||||||
use tokio::sync::RwLock;
|
|
||||||
|
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
async fn main() {
|
async fn main() {
|
||||||
let config = get_config();
|
let config = get_config();
|
||||||
|
@ -23,56 +12,6 @@ async fn main() {
|
||||||
Err(_) => return,
|
Err(_) => return,
|
||||||
};
|
};
|
||||||
|
|
||||||
// Intents are a bitflag, bitwise operations can be used to dictate which intents to use
|
|
||||||
let intents = GatewayIntents::GUILDS | GatewayIntents::GUILD_MESSAGES | GatewayIntents::MESSAGE_CONTENT | GatewayIntents::GUILD_MEMBERS;
|
|
||||||
// Build our client.
|
|
||||||
let mut client = Client::builder(&config.discord_token, intents)
|
|
||||||
.event_handler(Handler {})
|
|
||||||
.await
|
|
||||||
.expect("Error creating client");
|
|
||||||
|
|
||||||
{
|
|
||||||
let mut data = client.data.write().await;
|
|
||||||
|
|
||||||
data.insert::<Config>(Arc::new(RwLock::new(config)));
|
|
||||||
data.insert::<DataBase>(Arc::new(RwLock::new(db)));
|
|
||||||
}
|
|
||||||
|
|
||||||
// Finally, start a single shard, and start listening to events.
|
|
||||||
//
|
|
||||||
// Shards will automatically attempt to reconnect, and will perform
|
|
||||||
// exponential backoff until it reconnects.
|
|
||||||
if let Err(why) = client.start().await {
|
|
||||||
println!("Client error: {:?}", why);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
struct Handler;
|
|
||||||
#[async_trait]
|
|
||||||
impl EventHandler for Handler {
|
|
||||||
async fn ready(&self, ctx: Context, ready: Ready) {
|
|
||||||
println!("{} is connected!", ready.user.name);
|
|
||||||
|
|
||||||
fetch_accounts(&ctx).await;
|
|
||||||
|
|
||||||
process::exit(0);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
async fn fetch_accounts(ctx: &Context) {
|
|
||||||
let config_lock = {
|
|
||||||
let data_read = ctx.data.read().await;
|
|
||||||
data_read.get::<Config>().expect("Expected Config in TypeMap.").clone()
|
|
||||||
};
|
|
||||||
let config = config_lock.read().await;
|
|
||||||
|
|
||||||
let db_lock = {
|
|
||||||
let data_read = ctx.data.read().await;
|
|
||||||
data_read.get::<DataBase>().expect("Expected Config in TypeMap.").clone()
|
|
||||||
};
|
|
||||||
|
|
||||||
let db = db_lock.read().await;
|
|
||||||
|
|
||||||
// handle wolves api here
|
// handle wolves api here
|
||||||
get_wolves(&db).await;
|
get_wolves(&db).await;
|
||||||
|
|
||||||
|
@ -103,11 +42,11 @@ async fn add_users_skynet(db: &Pool<Sqlite>, server: &GuildId, user: &SkynetResu
|
||||||
WHERE server = ? AND wolves_id = ?
|
WHERE server = ? AND wolves_id = ?
|
||||||
",
|
",
|
||||||
)
|
)
|
||||||
.bind(&user.discord)
|
.bind(&user.discord)
|
||||||
.bind(*server.as_u64() as i64)
|
.bind(*server.as_u64() as i64)
|
||||||
.bind(&user.id_wolves)
|
.bind(&user.id_wolves)
|
||||||
.fetch_optional(db)
|
.fetch_optional(db)
|
||||||
.await
|
.await
|
||||||
{
|
{
|
||||||
Ok(_) => {}
|
Ok(_) => {}
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
|
@ -124,11 +63,11 @@ async fn add_users_skynet(db: &Pool<Sqlite>, server: &GuildId, user: &SkynetResu
|
||||||
WHERE server = ? AND id_member = ?
|
WHERE server = ? AND id_member = ?
|
||||||
",
|
",
|
||||||
)
|
)
|
||||||
.bind(&user.discord)
|
.bind(&user.discord)
|
||||||
.bind(*server.as_u64() as i64)
|
.bind(*server.as_u64() as i64)
|
||||||
.bind(&user.id_member)
|
.bind(&user.id_member)
|
||||||
.fetch_optional(db)
|
.fetch_optional(db)
|
||||||
.await
|
.await
|
||||||
{
|
{
|
||||||
Ok(_) => {}
|
Ok(_) => {}
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
|
|
Loading…
Reference in a new issue