Prefix configuration
This commit is contained in:
parent
4d9dbf9899
commit
005b629a21
@ -0,0 +1 @@
|
|||||||
|
DROP TABLE prefixes
|
@ -0,0 +1,4 @@
|
|||||||
|
CREATE TABLE prefixes (
|
||||||
|
id BIGINT PRIMARY KEY,
|
||||||
|
prefix VARCHAR NOT NULL
|
||||||
|
)
|
@ -4,7 +4,6 @@ use serenity::model::*;
|
|||||||
|
|
||||||
use ::diesel;
|
use ::diesel;
|
||||||
use ::diesel::prelude::*;
|
use ::diesel::prelude::*;
|
||||||
use ::diesel::pg::PgConnection;
|
|
||||||
use ::diesel::result::DatabaseErrorKind;
|
use ::diesel::result::DatabaseErrorKind;
|
||||||
|
|
||||||
use ::serde_json;
|
use ::serde_json;
|
||||||
@ -12,11 +11,13 @@ use ::serde_json::Value;
|
|||||||
|
|
||||||
use ::reqwest;
|
use ::reqwest;
|
||||||
|
|
||||||
use ::std::{env, thread};
|
use ::std::thread;
|
||||||
|
|
||||||
use failure;
|
use failure;
|
||||||
use failure::Error;
|
use failure::Error;
|
||||||
|
|
||||||
|
use super::establish_connection;
|
||||||
|
|
||||||
pub fn territory_livefeed() {
|
pub fn territory_livefeed() {
|
||||||
use models::LivefeedListener;
|
use models::LivefeedListener;
|
||||||
use schema::livefeedlisteners::dsl::*;
|
use schema::livefeedlisteners::dsl::*;
|
||||||
@ -179,10 +180,3 @@ pub fn wc_unwarfeed(msg: &Message) -> Result<(), Error> {
|
|||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn establish_connection() -> PgConnection {
|
|
||||||
let database_url = env::var("DATABASE_URL")
|
|
||||||
.expect("DATABASE_URL must be set");
|
|
||||||
PgConnection::establish(&database_url)
|
|
||||||
.expect(&format!("Error connecting to {}", database_url))
|
|
||||||
}
|
|
@ -8,9 +8,15 @@ use ::reqwest;
|
|||||||
use ::serde_json;
|
use ::serde_json;
|
||||||
use ::serde_json::Value;
|
use ::serde_json::Value;
|
||||||
|
|
||||||
|
use diesel;
|
||||||
|
use diesel::pg::PgConnection;
|
||||||
|
use diesel::prelude::*;
|
||||||
|
|
||||||
use failure;
|
use failure;
|
||||||
use failure::Error;
|
use failure::Error;
|
||||||
|
|
||||||
|
use std::env;
|
||||||
|
|
||||||
pub mod player;
|
pub mod player;
|
||||||
pub mod guild;
|
pub mod guild;
|
||||||
pub mod feed;
|
pub mod feed;
|
||||||
@ -19,6 +25,29 @@ pub use player::*;
|
|||||||
pub use guild::*;
|
pub use guild::*;
|
||||||
pub use feed::{wc_livefeed, wc_unlivefeed, wc_warfeed, wc_unwarfeed};
|
pub use feed::{wc_livefeed, wc_unlivefeed, wc_warfeed, wc_unwarfeed};
|
||||||
|
|
||||||
|
pub fn wc_prefix(command: &str, msg: &Message) -> Result<(), Error> {
|
||||||
|
use models::PrefixConfig;
|
||||||
|
use schema::prefixes::dsl::*;
|
||||||
|
|
||||||
|
let connection = establish_connection();
|
||||||
|
|
||||||
|
let new_prefix = &command[7..].to_owned();
|
||||||
|
|
||||||
|
diesel::insert_into(prefixes)
|
||||||
|
.values(&PrefixConfig {
|
||||||
|
id: msg.guild_id().unwrap().0 as i64,
|
||||||
|
prefix: new_prefix.clone()
|
||||||
|
})
|
||||||
|
.on_conflict(id)
|
||||||
|
.do_update()
|
||||||
|
.set(prefix.eq(new_prefix))
|
||||||
|
.execute(&connection)?;
|
||||||
|
|
||||||
|
msg.channel_id.say(format!("Prefix is now {:?}", new_prefix))?;
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
pub fn wc_status(msg: &Message) -> Result<(), Error> {
|
pub fn wc_status(msg: &Message) -> Result<(), Error> {
|
||||||
let resp = reqwest::get("https://api.wynncraft.com/public_api.php?action=onlinePlayersSum")?.text().unwrap();
|
let resp = reqwest::get("https://api.wynncraft.com/public_api.php?action=onlinePlayersSum")?.text().unwrap();
|
||||||
|
|
||||||
@ -205,4 +234,11 @@ pub fn wc_shout(command: &str, msg: &Message) -> Result<(), Error> {
|
|||||||
}
|
}
|
||||||
msg.channel_id.say("Success!")?;
|
msg.channel_id.say("Success!")?;
|
||||||
Ok(())
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn establish_connection() -> PgConnection {
|
||||||
|
let database_url = env::var("DATABASE_URL")
|
||||||
|
.expect("DATABASE_URL must be set");
|
||||||
|
PgConnection::establish(&database_url)
|
||||||
|
.expect(&format!("Error connecting to {}", database_url))
|
||||||
}
|
}
|
26
src/main.rs
26
src/main.rs
@ -10,6 +10,7 @@ extern crate serde_json;
|
|||||||
|
|
||||||
#[macro_use]
|
#[macro_use]
|
||||||
extern crate diesel;
|
extern crate diesel;
|
||||||
|
use diesel::prelude::*;
|
||||||
|
|
||||||
#[macro_use]
|
#[macro_use]
|
||||||
extern crate failure;
|
extern crate failure;
|
||||||
@ -36,6 +37,7 @@ use std::thread;
|
|||||||
|
|
||||||
mod models;
|
mod models;
|
||||||
mod schema;
|
mod schema;
|
||||||
|
use schema::prefixes;
|
||||||
mod commands;
|
mod commands;
|
||||||
use commands::*;
|
use commands::*;
|
||||||
|
|
||||||
@ -84,7 +86,19 @@ impl EventHandler for Handler {
|
|||||||
// Event handlers are dispatched through multi-threading, and so multiple
|
// Event handlers are dispatched through multi-threading, and so multiple
|
||||||
// of a single event can be dispatched simultaneously.
|
// of a single event can be dispatched simultaneously.
|
||||||
fn on_message(&self, _: Context, msg: Message) {
|
fn on_message(&self, _: Context, msg: Message) {
|
||||||
if let Err(error) = self.process_message(&msg) {
|
let cfg = Config::generate().unwrap();
|
||||||
|
let prefix = cfg.prefix;
|
||||||
|
let connection = commands::establish_connection();
|
||||||
|
|
||||||
|
let prefix = prefixes::table
|
||||||
|
.select(prefixes::prefix)
|
||||||
|
.filter(prefixes::id.eq(msg.guild_id().unwrap_or(GuildId(0)).0 as i64))
|
||||||
|
.load::<String>(&connection)
|
||||||
|
.expect("Error loading prefix").get(0)
|
||||||
|
.map(|x| x.to_owned())
|
||||||
|
.unwrap_or(prefix);
|
||||||
|
|
||||||
|
if let Err(error) = self.process_message(&msg, &prefix) {
|
||||||
//eprintln!("Error: {}", error);
|
//eprintln!("Error: {}", error);
|
||||||
let _ = msg.channel_id.say(format!("Error: {}", error));
|
let _ = msg.channel_id.say(format!("Error: {}", error));
|
||||||
for cause in error.causes().skip(1) {
|
for cause in error.causes().skip(1) {
|
||||||
@ -92,8 +106,7 @@ impl EventHandler for Handler {
|
|||||||
let _ = msg.channel_id.say(format!("caused by: {}", cause));
|
let _ = msg.channel_id.say(format!("caused by: {}", cause));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
let cfg = Config::generate().unwrap();
|
|
||||||
let prefix = cfg.prefix;
|
|
||||||
if msg.content.starts_with(&prefix) {
|
if msg.content.starts_with(&prefix) {
|
||||||
let command = &msg.content[prefix.len()..];
|
let command = &msg.content[prefix.len()..];
|
||||||
// log
|
// log
|
||||||
@ -117,10 +130,7 @@ impl EventHandler for Handler {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl Handler {
|
impl Handler {
|
||||||
fn process_message(&self, msg: &Message) -> Result<(), Error> {
|
fn process_message(&self, msg: &Message, prefix: &str) -> Result<(), Error> {
|
||||||
let cfg = Config::generate()?;
|
|
||||||
let prefix = cfg.prefix;
|
|
||||||
|
|
||||||
if msg.content == "<@392763365409292298> has big gay" {
|
if msg.content == "<@392763365409292298> has big gay" {
|
||||||
let _ = msg.channel_id.broadcast_typing();
|
let _ = msg.channel_id.broadcast_typing();
|
||||||
thread::sleep_ms(5000);
|
thread::sleep_ms(5000);
|
||||||
@ -165,6 +175,8 @@ impl Handler {
|
|||||||
} else {
|
} else {
|
||||||
"<@210743594061922306>".parse::<User>().unwrap().dm(|f| f.content(format!("{} tried to shout {:?}", msg.author, &command[6..]))).unwrap();
|
"<@210743594061922306>".parse::<User>().unwrap().dm(|f| f.content(format!("{} tried to shout {:?}", msg.author, &command[6..]))).unwrap();
|
||||||
}
|
}
|
||||||
|
} else if command.starts_with("prefix ") {
|
||||||
|
wc_prefix(command, msg)?;
|
||||||
} else {
|
} else {
|
||||||
bail!(ResponseError::UnknownCommand { name: command.to_owned() });
|
bail!(ResponseError::UnknownCommand { name: command.to_owned() });
|
||||||
}
|
}
|
||||||
|
@ -10,4 +10,11 @@ pub struct LivefeedListener {
|
|||||||
#[table_name="warfeedlisteners"]
|
#[table_name="warfeedlisteners"]
|
||||||
pub struct WarfeedListener {
|
pub struct WarfeedListener {
|
||||||
pub id: i64
|
pub id: i64
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Queryable, Insertable)]
|
||||||
|
#[table_name="prefixes"]
|
||||||
|
pub struct PrefixConfig {
|
||||||
|
pub id: i64,
|
||||||
|
pub prefix: String
|
||||||
}
|
}
|
@ -4,6 +4,13 @@ table! {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
table! {
|
||||||
|
prefixes (id) {
|
||||||
|
id -> Int8,
|
||||||
|
prefix -> Varchar,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
table! {
|
table! {
|
||||||
warfeedlisteners (id) {
|
warfeedlisteners (id) {
|
||||||
id -> Int8,
|
id -> Int8,
|
||||||
@ -12,5 +19,6 @@ table! {
|
|||||||
|
|
||||||
allow_tables_to_appear_in_same_query!(
|
allow_tables_to_appear_in_same_query!(
|
||||||
livefeedlisteners,
|
livefeedlisteners,
|
||||||
|
prefixes,
|
||||||
warfeedlisteners,
|
warfeedlisteners,
|
||||||
);
|
);
|
||||||
|
Loading…
Reference in New Issue
Block a user