Finish refactoring
This commit is contained in:
+37
-64
@@ -1,68 +1,38 @@
|
||||
use std::sync::Arc;
|
||||
|
||||
use clap::Parser;
|
||||
use twilight_gateway::{ConfigBuilder, EventTypeFlags, Intents, Shard, ShardId, StreamExt};
|
||||
use twilight_http::Client;
|
||||
use twilight_model::gateway::{
|
||||
payload::outgoing::update_presence::UpdatePresencePayload,
|
||||
presence::{ActivityType, MinimalActivity, Status},
|
||||
};
|
||||
use twilight_gateway::Intents;
|
||||
|
||||
use crate::{
|
||||
cli::Args, commands::register_default_components, discord::events,
|
||||
framework::{handlers::Handlers, interactions::registry::Registry},
|
||||
cli::Args,
|
||||
framework::bot::Bot,
|
||||
groups::{
|
||||
about::{commands::HelpCommand, components::HelpGoBackButton},
|
||||
calls::{
|
||||
commands::{CallCommand, HangupCommand},
|
||||
events::MessageRedirector,
|
||||
},
|
||||
gateway::events::{ReadyHandler, ReconnectHandler},
|
||||
servers::events::{ServerCreateHandler, ServerDeleteHandler, ServerUpdateHandler},
|
||||
},
|
||||
state::State,
|
||||
};
|
||||
|
||||
mod cli;
|
||||
mod commands;
|
||||
mod constants;
|
||||
mod database;
|
||||
mod discord;
|
||||
mod framework;
|
||||
mod groups;
|
||||
mod logging;
|
||||
mod state;
|
||||
mod utils;
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() {
|
||||
pub async fn main() {
|
||||
dotenvy::dotenv().ok();
|
||||
|
||||
let args = Args::parse();
|
||||
|
||||
logging::init();
|
||||
|
||||
let intents = Intents::GUILD_MESSAGES | Intents::MESSAGE_CONTENT;
|
||||
let config = ConfigBuilder::new(args.token.clone(), intents)
|
||||
.presence(
|
||||
UpdatePresencePayload::new(
|
||||
vec![
|
||||
MinimalActivity {
|
||||
kind: ActivityType::Listening,
|
||||
name: "Hello Payphone!".into(),
|
||||
url: None,
|
||||
}
|
||||
.into(),
|
||||
],
|
||||
false,
|
||||
None,
|
||||
Status::Online,
|
||||
)
|
||||
.expect("Could not set presence!"),
|
||||
)
|
||||
.build();
|
||||
let mut shard = Shard::with_config(ShardId::new(0, 1), config);
|
||||
|
||||
let http = Client::new(args.token.clone());
|
||||
let registry = Registry::new();
|
||||
let app =
|
||||
http.current_user_application()
|
||||
.await
|
||||
.expect("Could not get information about the current application")
|
||||
.model()
|
||||
.await
|
||||
.expect("Could not get model of the information of the current application")
|
||||
;
|
||||
|
||||
register_default_components(®istry);
|
||||
logging::init(args.logging.into());
|
||||
|
||||
log::info!("Welcome to Payphone, Nyeki!");
|
||||
log::info!(
|
||||
@@ -70,25 +40,28 @@ async fn main() {
|
||||
env!("CARGO_PKG_VERSION")
|
||||
);
|
||||
|
||||
let db = database::init("127.0.0.1:9042")
|
||||
let db = database::init(args.database_url)
|
||||
.await
|
||||
.expect("Could not connect to the database!");
|
||||
|
||||
let handlers = Arc::new(Handlers {
|
||||
app,
|
||||
db,
|
||||
http,
|
||||
registry
|
||||
});
|
||||
log::info!("Database connection established successfully!");
|
||||
|
||||
while let Some(event) = shard.next_event(EventTypeFlags::all()).await {
|
||||
if let Ok(event) = event {
|
||||
tokio::spawn(events::handle_event(
|
||||
event,
|
||||
handlers.clone(),
|
||||
));
|
||||
} else {
|
||||
log::error!("Could not receive the next event.");
|
||||
}
|
||||
}
|
||||
let state = State { db };
|
||||
|
||||
let bot = Bot::new(state, ["p."])
|
||||
.intent(Intents::GUILDS)
|
||||
.intent(Intents::GUILD_MESSAGES)
|
||||
.intent(Intents::MESSAGE_CONTENT)
|
||||
.command(HelpCommand)
|
||||
.command(CallCommand::new())
|
||||
.command(HangupCommand)
|
||||
.component(HelpGoBackButton)
|
||||
.on_ready(ReadyHandler)
|
||||
.on_reconnect(ReconnectHandler)
|
||||
.on_message(MessageRedirector)
|
||||
.on_server_create(ServerCreateHandler)
|
||||
.on_server_update(ServerUpdateHandler)
|
||||
.on_server_delete(ServerDeleteHandler);
|
||||
|
||||
bot.run(&args.token).await.expect("Could not run bot!");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user