Add initial structure

Signed-off-by: trivernis <trivernis@protonmail.com>
pull/2/head
trivernis 3 years ago
commit f630161356
Signed by: Trivernis
GPG Key ID: DFFFCC2C7A02DB45

4
.gitignore vendored

@ -0,0 +1,4 @@
/target
bot.db
.env
*.env

8
.idea/.gitignore vendored

@ -0,0 +1,8 @@
# Default ignored files
/shelf/
/workspace.xml
# Datasource local storage ignored files
/dataSources/
/dataSources.local.xml
# Editor-based HTTP Client requests
/httpRequests/

@ -0,0 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="CsvFileAttributes">
<option name="attributeMap">
<map>
<entry key="/src/database/scripts/update_tables.sql">
<value>
<Attribute>
<option name="separator" value="," />
</Attribute>
</value>
</entry>
</map>
</option>
</component>
</project>

@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="DataSourceManagerImpl" format="xml" multifile-model="true">
<data-source source="LOCAL" name="bot.db" uuid="4d1d7a8d-098d-4916-b51c-e7bde2613eda">
<driver-ref>sqlite.xerial</driver-ref>
<synchronize>true</synchronize>
<jdbc-driver>org.sqlite.JDBC</jdbc-driver>
<jdbc-url>jdbc:sqlite:bot.db</jdbc-url>
<working-dir>$ProjectFileDir$</working-dir>
</data-source>
</component>
</project>

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/tobi-rs.iml" filepath="$PROJECT_DIR$/.idea/tobi-rs.iml" />
</modules>
</component>
</project>

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="SqlDialectMappings">
<file url="file://$PROJECT_DIR$/src/database/scripts/create_tables.sql" dialect="GenericSQL" />
<file url="file://$PROJECT_DIR$/src/database/scripts/update_tables.sql" dialect="SQLite" />
<file url="PROJECT" dialect="SQLite" />
</component>
</project>

@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="CPP_MODULE" version="4">
<component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
<excludeFolder url="file://$MODULE_DIR$/target" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>

1461
Cargo.lock generated

File diff suppressed because it is too large Load Diff

@ -0,0 +1,18 @@
[package]
name = "tobi-rs"
version = "0.1.0"
authors = ["trivernis <trivernis@protonmail.com>"]
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
serenity = "0.10.5"
dotenv = "0.15.0"
tokio = { version = "1.4.0", features = ["macros", "rt-multi-thread"] }
serde_rusqlite = "0.26.0"
rusqlite = "0.24"
serde_derive = "1.0.125"
serde = "1.0.125"
thiserror = "1.0.24"
parking_lot = "0.11.1"

@ -0,0 +1,37 @@
use crate::database::{get_database, Database};
use crate::utils::error::{BotError, BotResult};
use crate::utils::store::{Store, StoreData};
use serenity::async_trait;
use serenity::client::EventHandler;
use serenity::framework::StandardFramework;
use serenity::Client;
struct Handler;
#[async_trait]
impl EventHandler for Handler {}
pub async fn get_client() -> BotResult<Client> {
let token = dotenv::var("BOT_TOKEN").map_err(|_| BotError::MissingToken)?;
let database = get_database()?;
let client = Client::builder(token).framework(get_framework()).await?;
{
let mut data = client.data.write().await;
data.insert::<Store>(StoreData::new(database))
}
Ok(client)
}
pub fn get_framework() -> StandardFramework {
StandardFramework::default().configure(|c| {
c.prefix(
dotenv::var("BOT_PREFIX")
.unwrap_or("~!".to_string())
.as_str(),
)
.allow_dm(true)
.ignore_bots(true)
})
}

@ -0,0 +1,13 @@
use serde_derive::{Deserialize, Serialize};
#[derive(Serialize, Deserialize, Debug, PartialEq)]
pub struct Guild {
guild_id: i32,
}
#[derive(Serialize, Deserialize, Debug, PartialEq)]
pub struct GuildSettings {
guild_id: i32,
key: String,
value: String,
}

@ -0,0 +1,34 @@
use crate::database::scripts::{CREATE_SCRIPT, UPDATE_SCRIPT};
use crate::utils::error::BotResult;
use rusqlite::{Connection, NO_PARAMS};
pub mod guild;
pub mod scripts;
#[derive(Debug)]
pub struct Database {
connection: Connection,
}
impl Database {
pub fn new(connection: Connection) -> Self {
Self { connection }
}
/// Initializes the database
pub fn init(&self) -> BotResult<()> {
self.connection.execute(CREATE_SCRIPT, NO_PARAMS)?;
self.connection.execute(UPDATE_SCRIPT, NO_PARAMS)?;
Ok(())
}
}
pub fn get_database() -> BotResult<Database> {
let filename = dotenv::var("DB_NAME").unwrap_or("bot.db".to_string());
let connection = rusqlite::Connection::open(filename)?;
let database = Database::new(connection);
database.init()?;
Ok(database)
}

@ -0,0 +1,10 @@
CREATE TABLE IF NOT EXISTS guilds (
guild_id INTEGER PRIMARY KEY
);
CREATE TABLE IF NOT EXISTS guild_settings (
guild_id INTEGER NOT NULL,
setting_key TEXT NOT NULL,
setting_value TEXT,
FOREIGN KEY (guild_id) REFERENCES guilds (guild_id)
);

@ -0,0 +1,2 @@
pub static CREATE_SCRIPT: &str = include_str!("create_tables.sql");
pub static UPDATE_SCRIPT: &str = include_str!("update_tables.sql");

@ -0,0 +1 @@
SELECT NULL FROM guilds;

@ -0,0 +1,34 @@
use crate::client::get_client;
use serenity::client::{Context, EventHandler};
use serenity::framework::standard::{
macros::{command, group},
CommandResult,
};
use serenity::model::channel::Message;
pub(crate) mod client;
pub(crate) mod database;
pub(crate) mod utils;
#[group]
#[commands(ping)]
struct General;
struct Handler;
#[tokio::main]
async fn main() {
let mut client = get_client().await.unwrap();
// start listening for events by starting a single shard
if let Err(why) = client.start().await {
println!("An error occurred while running the client: {:?}", why);
}
}
#[command]
async fn ping(ctx: &Context, msg: &Message) -> CommandResult {
msg.reply(ctx, "Pong!").await?;
Ok(())
}

@ -0,0 +1,15 @@
use thiserror::Error;
pub type BotResult<T> = Result<T, BotError>;
#[derive(Error, Debug)]
pub enum BotError {
#[error("Serenity Error: {0}")]
SerenityError(#[from] serenity::Error),
#[error("Sqlite Error: {0}")]
Sqlite(#[from] rusqlite::Error),
#[error("Missing Bot Token")]
MissingToken,
}

@ -0,0 +1,2 @@
pub mod error;
pub mod store;

@ -0,0 +1,22 @@
use crate::database::Database;
use parking_lot::Mutex;
use serenity::prelude::TypeMapKey;
use std::sync::Arc;
pub struct Store;
pub struct StoreData {
pub database: Arc<Mutex<Database>>,
}
impl StoreData {
pub fn new(database: Database) -> StoreData {
Self {
database: Arc::new(Mutex::new(database)),
}
}
}
impl TypeMapKey for Store {
type Value = StoreData;
}
Loading…
Cancel
Save