-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Kamran Mackey <kamranm1200@gmail.com>
- Loading branch information
Kamran Mackey
committed
Jan 24, 2020
1 parent
afeee0e
commit 69bf639
Showing
8 changed files
with
199 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
/target | ||
.env | ||
.env | ||
*.sqlite3 |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
pub mod help; | ||
pub mod ping; | ||
pub mod prefix; | ||
pub mod version; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
use crate::utilities::database; | ||
use crate::utilities::database::get_database; | ||
|
||
use serenity::framework::standard::macros::command; | ||
use serenity::framework::standard::Args; | ||
use serenity::framework::standard::CommandError; | ||
use serenity::framework::standard::CommandResult; | ||
use serenity::model::prelude::Message; | ||
use serenity::prelude::Context; | ||
|
||
#[command] | ||
#[description("Get or set the command prefix for the current server." | ||
)] | ||
#[sub_commands(get, set)] | ||
fn prefix(ctx: &mut Context, message: &Message) -> CommandResult { | ||
message | ||
.channel_id | ||
.send_message(&ctx, move |m| { | ||
m.embed(move |e| { | ||
e.title("Error: Invalid / No Subcommand Entered!"); | ||
e.description("Please use subcommand get or set to use this command."); | ||
e | ||
}) | ||
}) | ||
.map_or_else(|e| Err(CommandError(e.to_string())), |_| Ok(())) | ||
} | ||
|
||
#[command] | ||
#[only_in(guilds)] | ||
#[owners_only] | ||
#[description = "Retrieves the command prefix for the current server."] | ||
pub fn get(ctx: &mut Context, message: &Message) -> CommandResult { | ||
let prefix = database::get_prefix(&message.guild_id.unwrap()).unwrap().to_string(); | ||
let guild = message.guild(&ctx.cache).unwrap(); | ||
let guild_name = &guild.read().name; | ||
|
||
return message | ||
.channel_id | ||
.say(&ctx.http, format!("The currently set command prefix for {} is {}.", guild_name, prefix)) | ||
.map_or_else(|e| Err(CommandError(e.to_string())), |_| Ok(())); | ||
} | ||
|
||
#[command] | ||
#[only_in(guilds)] | ||
#[owners_only] | ||
#[num_args(1)] | ||
#[description = "Sets the command prefix for the current server."] | ||
pub fn set(ctx: &mut Context, message: &Message, args: Args) -> CommandResult { | ||
let connection = match get_database() { | ||
Ok(connection) => connection, | ||
Err(_) => return Ok(()), | ||
}; | ||
|
||
let prefix = args.current().unwrap_or(";"); | ||
let guild = message.guild(&ctx.cache).unwrap(); | ||
let guild_id = guild.read().clone().id.as_u64().to_string(); | ||
let guild_name = guild.read().clone().name; | ||
|
||
let _ = connection.execute( | ||
"INSERT OR REPLACE INTO guild_settings (guild_id, guild_name, prefix) values (?1, ?2, ?3)", | ||
&[&guild_id, &guild_name, prefix], | ||
); | ||
|
||
let _ = message | ||
.channel_id | ||
.say(&ctx.http, format!("The command prefix for {} has been set to {}.", guild_name, prefix)); | ||
|
||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
use log::error; | ||
|
||
use rusqlite::Connection; | ||
use rusqlite::NO_PARAMS; | ||
|
||
use serenity::model::prelude::GuildId; | ||
|
||
use std::error::Error; | ||
use std::fs::File; | ||
use std::path::Path; | ||
|
||
pub fn create_database() { | ||
let database = Path::new("database.sqlite3"); | ||
if !database.exists() { | ||
match File::create(&database) { | ||
Ok(_) => (), | ||
Err(e) => error!("Failed to create database file: {}", e), | ||
} | ||
} | ||
if let Ok(connection) = Connection::open(&database) { | ||
match connection.execute( | ||
"CREATE TABLE IF NOT EXISTS guild_settings ( | ||
guild_id TEXT PRIMARY KEY, | ||
guild_name TEXT NOT NULL, | ||
prefix TEXT NOT NULL | ||
);", | ||
NO_PARAMS, | ||
) { | ||
Ok(_) => (), | ||
Err(e) => { | ||
error!("{}", e); | ||
} | ||
} | ||
} else { | ||
error!("Could not open connection to database ({})", &database.to_string_lossy()); | ||
} | ||
} | ||
|
||
pub fn get_database() -> Result<Connection, Box<dyn Error>> { | ||
let db = Path::new("database.sqlite3"); | ||
Ok(Connection::open(db)?) | ||
} | ||
|
||
pub fn get_prefix(guildid: &GuildId) -> Result<String, Box<dyn Error>> { | ||
let conn = get_database()?; | ||
let mut statement = conn.prepare("SELECT prefix FROM guild_settings WHERE guild_id == ?1;")?; | ||
let mut rows = statement.query(&[&guildid.as_u64().to_string()])?; | ||
Ok(rows.next()?.ok_or("Guild not found.")?.get(0)?) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
pub mod built_info; | ||
pub mod database; | ||
pub mod git_utils; | ||
|
||
pub fn format_int(i: isize) -> String { | ||
|