|
| 1 | +/// Rustic Economy: a small example plugin backed by SQLite. |
| 2 | +/// |
| 3 | +/// This example demonstrates how to: |
| 4 | +/// - Use `#[derive(Plugin)]` to declare plugin metadata and register commands. |
| 5 | +/// - Use `#[derive(Command)]` to define a typed command enum. |
| 6 | +/// - Hold state (a `SqlitePool`) inside your plugin struct. |
| 7 | +/// - Use `Ctx` to reply to the invoking player. |
| 8 | +/// - Use `#[event_handler]` for event subscriptions (even when you don't |
| 9 | +/// implement any event methods yet). |
| 10 | +use dragonfly_plugin::{ |
| 11 | + Command, Plugin, PluginRunner, command::Ctx, event::EventHandler, event_handler, types, |
| 12 | +}; |
| 13 | +use sqlx::{SqlitePool, sqlite::SqlitePoolOptions}; |
| 14 | + |
| 15 | +#[derive(Plugin)] |
| 16 | +#[plugin( |
| 17 | + id = "rustic-economy", |
| 18 | + name = "Rustic Economy", |
| 19 | + version = "0.3.0", |
| 20 | + api = "1.0.0", |
| 21 | + commands(Eco) |
| 22 | +)] |
| 23 | +struct RusticEconomy { |
| 24 | + db: SqlitePool, |
| 25 | +} |
| 26 | + |
| 27 | +/// Database helpers for the Rustic Economy example. |
| 28 | +impl RusticEconomy { |
| 29 | + async fn new() -> Result<Self, Box<dyn std::error::Error>> { |
| 30 | + // Create database connection |
| 31 | + let db = SqlitePoolOptions::new() |
| 32 | + .max_connections(5) |
| 33 | + .connect("sqlite:economy.db") |
| 34 | + .await?; |
| 35 | + |
| 36 | + // Create table if it doesn't exist. |
| 37 | + // |
| 38 | + // NOTE: This example stores balances as REAL/f64 for simplicity. |
| 39 | + // For real-world money you should use an integer representation |
| 40 | + // (e.g. cents as i64) to avoid floating point rounding issues. |
| 41 | + sqlx::query( |
| 42 | + "CREATE TABLE IF NOT EXISTS users ( |
| 43 | + uuid TEXT PRIMARY KEY, |
| 44 | + balance REAL NOT NULL DEFAULT 0.0 |
| 45 | + )", |
| 46 | + ) |
| 47 | + .execute(&db) |
| 48 | + .await?; |
| 49 | + |
| 50 | + Ok(Self { db }) |
| 51 | + } |
| 52 | + |
| 53 | + async fn get_balance(&self, uuid: &str) -> Result<f64, sqlx::Error> { |
| 54 | + let result: Option<(f64,)> = sqlx::query_as("SELECT balance FROM users WHERE uuid = ?") |
| 55 | + .bind(uuid) |
| 56 | + .fetch_optional(&self.db) |
| 57 | + .await?; |
| 58 | + |
| 59 | + Ok(result.map(|(bal,)| bal).unwrap_or(0.0)) |
| 60 | + } |
| 61 | + |
| 62 | + async fn add_money(&self, uuid: &str, amount: f64) -> Result<f64, sqlx::Error> { |
| 63 | + // Insert or update user balance |
| 64 | + sqlx::query( |
| 65 | + "INSERT INTO users (uuid, balance) VALUES (?, ?) |
| 66 | + ON CONFLICT(uuid) DO UPDATE SET balance = balance + ?", |
| 67 | + ) |
| 68 | + .bind(uuid) |
| 69 | + .bind(amount) |
| 70 | + .bind(amount) |
| 71 | + .execute(&self.db) |
| 72 | + .await?; |
| 73 | + |
| 74 | + self.get_balance(uuid).await |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +#[derive(Command)] |
| 79 | +#[command( |
| 80 | + name = "eco", |
| 81 | + description = "Rustic Economy commands.", |
| 82 | + aliases("economy", "rustic_eco") |
| 83 | +)] |
| 84 | +pub enum Eco { |
| 85 | + #[subcommand(aliases("donate"))] |
| 86 | + Pay { amount: f64 }, |
| 87 | + #[subcommand(aliases("balance", "money"))] |
| 88 | + Bal, |
| 89 | +} |
| 90 | + |
| 91 | +impl EcoHandler for RusticEconomy { |
| 92 | + async fn pay(&self, ctx: Ctx<'_>, amount: f64) { |
| 93 | + match self.add_money(&ctx.sender, amount).await { |
| 94 | + Ok(new_balance) => { |
| 95 | + if let Err(e) = ctx |
| 96 | + .reply(format!( |
| 97 | + "Added ${:.2}! New balance: ${:.2}", |
| 98 | + amount, new_balance |
| 99 | + )) |
| 100 | + .await |
| 101 | + { |
| 102 | + eprintln!("Failed to send payment reply: {}", e); |
| 103 | + } |
| 104 | + } |
| 105 | + Err(e) => { |
| 106 | + eprintln!("Database error: {}", e); |
| 107 | + if let Err(send_err) = ctx |
| 108 | + .reply("Error processing payment!".to_string()) |
| 109 | + .await |
| 110 | + { |
| 111 | + eprintln!("Failed to send error reply: {}", send_err); |
| 112 | + } |
| 113 | + } |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + async fn bal(&self, ctx: Ctx<'_>) { |
| 118 | + match self.get_balance(&ctx.sender).await { |
| 119 | + Ok(balance) => { |
| 120 | + if let Err(e) = ctx |
| 121 | + .reply(format!("Your balance: ${:.2}", balance)) |
| 122 | + .await |
| 123 | + { |
| 124 | + eprintln!("Failed to send balance reply: {}", e); |
| 125 | + } |
| 126 | + } |
| 127 | + Err(e) => { |
| 128 | + eprintln!("Database error: {}", e); |
| 129 | + if let Err(send_err) = ctx |
| 130 | + .reply("Error checking balance!".to_string()) |
| 131 | + .await |
| 132 | + { |
| 133 | + eprintln!("Failed to send error reply: {}", send_err); |
| 134 | + } |
| 135 | + } |
| 136 | + } |
| 137 | + } |
| 138 | +} |
| 139 | + |
| 140 | +#[event_handler] |
| 141 | +impl EventHandler for RusticEconomy {} |
| 142 | + |
| 143 | +#[tokio::main] |
| 144 | +async fn main() -> Result<(), Box<dyn std::error::Error>> { |
| 145 | + println!("Starting the plugin..."); |
| 146 | + println!("Initializing database..."); |
| 147 | + |
| 148 | + let plugin = RusticEconomy::new().await?; |
| 149 | + |
| 150 | + PluginRunner::run(plugin, "tcp://127.0.0.1:50050").await |
| 151 | +} |
0 commit comments