1
Fork 0
mirror of https://github.com/RYGhub/royalnet.git synced 2024-11-25 20:44:21 +00:00

Refactor services to use traits

This commit is contained in:
Steffo 2024-07-11 08:05:58 +02:00
parent cd4214cb4b
commit 7a8d30e1d0
Signed by: steffo
GPG key ID: 5ADA3868646C3FC0
13 changed files with 42 additions and 38 deletions

View file

@ -1,9 +1,11 @@
use anyhow::Result; use anyhow::Result;
use crate::telegram::DispatchWithResult; use services::telegram;
use crate::services::RoyalnetService;
pub(crate) mod database; pub(crate) mod database;
pub(crate) mod utils; pub(crate) mod utils;
mod telegram; mod services;
#[tokio::main] #[tokio::main]
async fn main() -> Result<()> { async fn main() -> Result<()> {
@ -12,14 +14,13 @@ async fn main() -> Result<()> {
log::debug!("Logging initialized successfully!"); log::debug!("Logging initialized successfully!");
// Telegram setup // Telegram setup
log::trace!("Setting up Telegram bot dispatcher..."); log::trace!("Setting up Telegram bot service...");
let mut telegram_dispatcher = telegram::dispatcher(); let telegram = telegram::init();
let telegram_awaitable = telegram_dispatcher.dispatch_with_result();
// Run all services concurrently // Run all services concurrently
log::info!("Starting services..."); log::info!("Starting services...");
let result = tokio::try_join![ let result = tokio::try_join![
telegram_awaitable, telegram.run_royalnet(),
]; ];
// This should never happen, but just in case... // This should never happen, but just in case...

18
src/services/mod.rs Normal file
View file

@ -0,0 +1,18 @@
use std::convert::Infallible;
use anyhow::Result;
pub mod telegram;
pub trait RoyalnetService {
async fn run_royalnet(self) -> Result<Infallible>;
}
impl RoyalnetService for teloxide::dispatching::Dispatcher<teloxide::Bot, anyhow::Error, teloxide::dispatching::DefaultKey> {
async fn run_royalnet(mut self) -> Result<Infallible> {
log::info!("Starting Telegram service...");
self.dispatch().await;
log::error!("Telegram dispatcher has exited, bailing out...");
anyhow::bail!("Telegram dispatcher has exited.")
}
}

View file

@ -4,7 +4,7 @@ use rand::seq::SliceRandom;
use teloxide::Bot; use teloxide::Bot;
use teloxide::payloads::SendMessageSetters; use teloxide::payloads::SendMessageSetters;
use teloxide::prelude::{Message, Requester}; use teloxide::prelude::{Message, Requester};
use crate::telegram::commands::{CommandResult}; use crate::services::telegram::commands::{CommandResult};
// Cerchiamo di tenere bilanciate le tre colonne, o almeno le prime due. // Cerchiamo di tenere bilanciate le tre colonne, o almeno le prime due.
// Se avete un'idea ma metterebbe troppe opzioni in un'unica categoria, mettetela sotto commento. // Se avete un'idea ma metterebbe troppe opzioni in un'unica categoria, mettetela sotto commento.

View file

@ -6,7 +6,7 @@ use rand::seq::SliceRandom;
use teloxide::Bot; use teloxide::Bot;
use teloxide::payloads::SendMessageSetters; use teloxide::payloads::SendMessageSetters;
use teloxide::prelude::{Message, Requester}; use teloxide::prelude::{Message, Requester};
use crate::telegram::commands::{CommandResult}; use crate::services::telegram::commands::{CommandResult};
// Tutte le fortune devono essere positive, o almeno neutrali, per poter essere aggiunte. // Tutte le fortune devono essere positive, o almeno neutrali, per poter essere aggiunte.
const FORTUNES: [&str; 164] = [ const FORTUNES: [&str; 164] = [

View file

@ -20,7 +20,7 @@ mod reminder;
#[derive(Debug, Clone, PartialEq, Eq, BotCommands)] #[derive(Debug, Clone, PartialEq, Eq, BotCommands)]
#[command(rename_rule = "lowercase")] #[command(rename_rule = "lowercase")]
enum Command { pub enum Command {
#[command(description = "Invia messaggio di introduzione.")] #[command(description = "Invia messaggio di introduzione.")]
Start, Start,
#[command(description = "Visualizza l'elenco dei comandi disponibili, o mostra informazioni su uno specifico comando.")] #[command(description = "Visualizza l'elenco dei comandi disponibili, o mostra informazioni su uno specifico comando.")]

View file

@ -0,0 +1,14 @@
use anyhow::Error;
use teloxide::Bot;
use teloxide::dispatching::{DefaultKey, Dispatcher};
mod config;
mod commands;
pub fn init() -> Dispatcher<Bot, Error, DefaultKey> {
commands::dispatcher(
Bot::new(
config::TELEGRAM_BOT_TOKEN()
)
)
}

View file

@ -1,29 +0,0 @@
use std::convert::Infallible;
use anyhow::{Error, Result};
use teloxide::Bot;
use teloxide::dispatching::{DefaultKey, Dispatcher};
mod config;
mod commands;
pub fn dispatcher() -> Dispatcher<Bot, Error, DefaultKey> {
commands::dispatcher(
Bot::new(
config::TELEGRAM_BOT_TOKEN()
)
)
}
pub trait DispatchWithResult {
async fn dispatch_with_result(&mut self) -> Result<Infallible>;
}
impl DispatchWithResult for Dispatcher<Bot, Error, DefaultKey> {
async fn dispatch_with_result(&mut self) -> Result<Infallible> {
log::info!("Starting Telegram dispatcher...");
self.dispatch().await;
log::error!("Telegram dispatcher has exited, bailing out...");
anyhow::bail!("Telegram dispatcher has exited.")
}
}