initial code

This commit is contained in:
jude 2020-08-06 15:22:13 +01:00
parent 210b0afa67
commit 784a72dd08
11 changed files with 2154 additions and 0 deletions

2
.cargo/config Normal file
View File

@ -0,0 +1,2 @@
[build]
target-dir = "/home/jude/.rust_build/reminder-rs"

8
.idea/.gitignore vendored Normal file
View File

@ -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/

View File

@ -0,0 +1,7 @@
<component name="ProjectDictionaryState">
<dictionary name="jude">
<words>
<w>reqwest</w>
</words>
</dictionary>
</component>

6
.idea/misc.xml Normal file
View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="JavaScriptSettings">
<option name="languageLevel" value="ES6" />
</component>
</project>

8
.idea/modules.xml Normal file
View File

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

11
.idea/reminder-rs.iml Normal file
View File

@ -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>

6
.idea/vcs.xml Normal file
View File

@ -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>

1890
Cargo.lock generated Normal file

File diff suppressed because it is too large Load Diff

16
Cargo.toml Normal file
View File

@ -0,0 +1,16 @@
[package]
name = "reminder-rs"
version = "0.1.0"
authors = ["jude <judewrs@gmail.com>"]
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
serenity = {git = "https://github.com/acdenisSK/serenity", branch = "await_next"}
dotenv = "0.15"
tokio = {version = "0.2.19", features = ["fs", "sync", "process", "io-util"]}
reqwest = "0.10.6"
sqlx = {version = "0.3.5", default-features = false, features = ["runtime-tokio", "macros", "mysql", "bigdecimal"]}
regex = "1.3.9"
async-trait = "0.1.36"

123
src/framework.rs Normal file
View File

@ -0,0 +1,123 @@
use async_trait::async_trait;
use serenity::{
client::Context,
framework::Framework,
model::channel::Message,
};
use std::{
collections::HashSet,
hash::{
Hash,
Hasher
},
};
use serenity::framework::standard::CommandFn;
pub enum PermissionLevel {
Unrestricted,
Managed,
Restricted,
}
pub struct Command {
name: String,
required_perms: PermissionLevel,
can_blacklist: bool,
supports_dm: bool,
func: CommandFn,
}
impl Hash for Command {
fn hash<H: Hasher>(&self, state: &mut H) {
self.name.hash(state);
}
}
impl PartialEq for Command {
fn eq(&self, other: &Self) -> bool {
self.name == other.name
}
}
impl Eq for Command {}
// create event handler for bot
pub struct RegexFramework {
commands: HashSet<Command>,
command_names: String,
default_prefix: String,
ignore_bots: bool,
}
impl Command {
pub fn from(name: &str, required_perms: PermissionLevel, func: CommandFn) -> Self {
Command {
name: name.to_string(),
required_perms,
can_blacklist: true,
supports_dm: false,
func,
}
}
pub fn can_blacklist(&mut self, can_blacklist: bool) -> &mut Self {
self.can_blacklist = can_blacklist;
self
}
pub fn supports_dm(&mut self, supports_dm: bool) -> &mut Self {
self.supports_dm = supports_dm;
self
}
}
impl RegexFramework {
pub fn new() -> Self {
Self {
commands: HashSet::new(),
command_names: String::new(),
default_prefix: String::from("$"),
ignore_bots: true,
}
}
pub fn default_prefix(mut self, new_prefix: &str) -> Self {
self.default_prefix = new_prefix.to_string();
self
}
pub fn ignore_bots(mut self, ignore_bots: bool) -> Self {
self.ignore_bots = ignore_bots;
self
}
pub fn add_command(mut self, command: Command) -> Self {
self.commands.insert(command);
self
}
pub fn build(mut self) -> Self {
self.command_names = self.commands
.iter()
.map(|c| c.name.clone())
.collect::<Vec<String>>()
.join("|");
self
}
}
#[async_trait]
impl Framework for RegexFramework {
async fn dispatch(&self, ctx: Context, msg: Message) {
println!("Message received! command_names=={}", self.command_names);
}
}

77
src/main.rs Normal file
View File

@ -0,0 +1,77 @@
mod framework;
use serenity::{
client::{
bridge::gateway::GatewayIntents,
Client, Context,
},
model::{
channel::{
Message,
},
},
framework::standard::{
Args, CommandResult,
macros::{
command,
}
},
prelude::TypeMapKey,
};
use sqlx::{
Pool,
mysql::{
MySqlConnection,
}
};
use dotenv::dotenv;
use std::{
sync::Arc,
env,
};
use crate::framework::{RegexFramework, Command, PermissionLevel};
struct SQLPool;
impl TypeMapKey for SQLPool {
type Value = Pool<MySqlConnection>;
}
struct ReqwestClient;
impl TypeMapKey for ReqwestClient {
type Value = Arc<reqwest::Client>;
}
static THEME_COLOR: u32 = 0x00e0f3;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
dotenv()?;
let framework = RegexFramework::new()
.ignore_bots(true)
.default_prefix("$")
.add_command(Command::from("help", PermissionLevel::Unrestricted, help_command))
.build();
let mut client = Client::new(&env::var("DISCORD_TOKEN").expect("Missing DISCORD_TOKEN from environment"))
.intents(GatewayIntents::GUILD_MESSAGES | GatewayIntents::GUILDS | GatewayIntents::DIRECT_MESSAGES)
.framework(framework)
.await.expect("Error occured creating client");
client.start_autosharded().await?;
Ok(())
}
#[command]
async fn help_command(_ctx: &Context, _msg: &Message, _args: Args) -> CommandResult {
println!("Help command called");
Ok(())
}