diff options
| author | Fuwn <[email protected]> | 2021-06-25 03:55:45 -0700 |
|---|---|---|
| committer | Fuwn <[email protected]> | 2021-06-25 03:55:45 -0700 |
| commit | c6df8a46d4f62739318a1b7bfe7b744244cd9138 (patch) | |
| tree | a6b42f316f4f21de43909f07051db9fc8cf0a1d6 /src/database.rs | |
| parent | feat(cmds): say command (diff) | |
| download | dos-bot-c6df8a46d4f62739318a1b7bfe7b744244cd9138.tar.xz dos-bot-c6df8a46d4f62739318a1b7bfe7b744244cd9138.zip | |
feat(cmds): create, remove, and count commands
Diffstat (limited to 'src/database.rs')
| -rw-r--r-- | src/database.rs | 58 |
1 files changed, 58 insertions, 0 deletions
diff --git a/src/database.rs b/src/database.rs new file mode 100644 index 0000000..9dc9f5a --- /dev/null +++ b/src/database.rs @@ -0,0 +1,58 @@ +// Copyright (C) 2021-2021 Fuwn +// SPDX-License-Identifier: GPL-3.0-only + +use simple_error::SimpleResult; +#[allow(clippy::wildcard_imports)] +use simplebase::engine::*; + +pub struct Database(pub RecordData); +impl Database { + #[must_use] + pub fn new() -> Self { + if std::path::Path::new(".dos-bot/db.txt").exists() { + Self(load_hash_database(".dos-bot/db.txt")) + } else { + Self(new_empty_database()) + } + } + + pub fn create_reaction_role(&mut self, message: u64, role: u64) { + self + .0 + .add_record_with_key(message.to_string(), role.to_string()); + + self.0.save_database(".dos-bot/db.txt"); + } + + /// # Panics + /// if the record index is unable to be parsed. + pub fn remove_reaction_role(&mut self, message: u64) { + self.0.delete_record( + self.0.find_key(message.to_string().as_str())[0] + .parse::<usize>() + .unwrap(), + ); + + self.0.save_database(".dos-bot/db.txt"); + } + + /// # Errors + /// if no record is found for the given `message`. + /// + /// # Panics + /// if the record is unable to be parsed. + pub fn get_reaction_role(&self, message: u64) -> SimpleResult<u64> { + let record = self.0.find_key(message.to_string().as_str()); + if record.is_empty() { + simple_error::bail!("no record found"); + } + + Ok(record[1].clone().parse::<u64>().unwrap()) + } + + #[must_use] + pub fn count(&self) -> String { self.0.record_counter.to_string() } +} +impl Default for Database { + fn default() -> Self { Self::new() } +} |