reduce redundancy

This commit is contained in:
kageru 2021-05-03 22:06:19 +02:00
parent cdd370bd0d
commit 23d8ed09df
Signed by: kageru
GPG Key ID: 8282A2BEA4ADA3D2

View File

@ -8,7 +8,7 @@ use serenity::framework::standard::{
}; };
use serenity::model::{ use serenity::model::{
channel::Message, channel::Message,
id::{ChannelId, GuildId}, id::{ChannelId, GuildId, UserId},
prelude::User, prelude::User,
}; };
use serenity::prelude::*; use serenity::prelude::*;
@ -18,11 +18,20 @@ use time::Duration;
mod inbox; mod inbox;
const APPLICATION_NAME: &str = "didgeridoo"; const APPLICATION_NAME: &str = "didgeridoo";
const INBOX_OWNER: UserId = UserId(354694403798990848);
lazy_static! { lazy_static! {
static ref INBOX: Inbox = Inbox(redis::Client::open("redis://127.0.0.1/").unwrap()); static ref INBOX: Inbox = Inbox(redis::Client::open("redis://127.0.0.1/").unwrap());
} }
macro_rules! send_or_log {
($e: expr) => {
if let Err(e) = $e {
eprintln!("Couldn’t send message because {:?}", e);
}
};
}
#[group] #[group]
#[commands(name, message, question, inbox)] #[commands(name, message, question, inbox)]
struct Fluff; struct Fluff;
@ -30,11 +39,9 @@ struct Handler;
impl EventHandler for Handler { impl EventHandler for Handler {
fn guild_ban_addition(&self, ctx: Context, guild_id: GuildId, _: User) { fn guild_ban_addition(&self, ctx: Context, guild_id: GuildId, _: User) {
if guild_id == 427456811973738498 { if guild_id == 427456811973738498 {
if let Err(e) = send_or_log!(
ChannelId(562731470423064587).say(&ctx, "Dies ist eine flauschige Diktatur!") ChannelId(562731470423064587).say(&ctx, "Dies ist eine flauschige Diktatur!")
{ );
eprintln!("Couldn’t send message because {:?}", e);
}
} }
} }
} }
@ -89,9 +96,42 @@ fn name(ctx: &mut Context, msg: &Message) -> CommandResult {
Ok(()) Ok(())
} }
#[command]
fn inbox(ctx: &mut Context, msg: &Message) -> CommandResult {
if msg.author.id != INBOX_OWNER {
send_or_log!(msg.reply(&ctx, "You don’t have the permission to do that"));
return Ok(());
}
if let Some((_, name)) = msg.content.split_once(' ') {
let messages = INBOX.fetch_messages(name);
if messages.is_empty() {
send_or_log!(msg.reply(&ctx, format!("Keine neuen Nachrichten für {}.", name)));
return Ok(());
}
let output = messages.into_iter().join("\n");
send_or_log!(msg.reply(&ctx, output));
} else {
send_or_log!(msg.reply(&ctx, "Kein Name für die Abfrage."));
}
Ok(())
}
#[command] #[command]
fn message(ctx: &mut Context, msg: &Message) -> CommandResult { fn message(ctx: &mut Context, msg: &Message) -> CommandResult {
InboxMessage::parse(&msg.content, msg.author.id) message_internal(ctx, msg, &msg.content)
}
#[command]
fn question(ctx: &mut Context, msg: &Message) -> CommandResult {
message_internal(
ctx,
msg,
&msg.content.replacen("!question", "!message Stream:", 1),
)
}
fn message_internal(ctx: &mut Context, msg: &Message, content: &str) -> CommandResult {
InboxMessage::parse(content, msg.author.id)
.ok_or_else(|| { .ok_or_else(|| {
String::from( String::from(
"Nachricht konnte nicht gesendet werden. "Nachricht konnte nicht gesendet werden.
@ -101,70 +141,13 @@ z.B. “!message Lana: Hawwu!”",
}) })
.and_then(|m| INBOX.queue_message(&m)) .and_then(|m| INBOX.queue_message(&m))
.map(|n| { .map(|n| {
if let Err(e) = msg.reply( send_or_log!(msg.reply(
&ctx, &ctx,
&format!("Deine Nachricht wurde erfolgreich an {} gesendet", n), &format!("Deine Nachricht wurde erfolgreich an {} gesendet", n),
) { ));
eprintln!("Could not reply because of {:?}", e);
}
}) })
.map_err(|e| { .map_err(|e| {
if let Err(e) = msg.reply(ctx, &e) { send_or_log!(msg.reply(&ctx, &e));
eprintln!("Could not reply because of {:?}", e);
}
CommandError(e) CommandError(e)
}) })
} }
#[command]
fn inbox(ctx: &mut Context, msg: &Message) -> CommandResult {
if let Some((_, name)) = msg.content.split_once(' ') {
let messages = INBOX.fetch_messages(name);
if messages.is_empty() {
if let Err(e) = msg.reply(ctx, "Keine neuen Nachrichten für .") {
eprintln!("Could not reply because of {:?}", e);
}
return Ok(());
}
let output = messages.into_iter().join("\n");
if let Err(e) = msg.reply(ctx, output) {
eprintln!("Could not reply because of {:?}", e);
}
} else {
if let Err(e) = msg.reply(ctx, "Kein Name für die Abfrage.") {
eprintln!("Could not reply because of {:?}", e);
}
}
Ok(())
}
// TODO: remove copy paste
#[command]
fn question(ctx: &mut Context, msg: &Message) -> CommandResult {
InboxMessage::parse(
&msg.content.replacen("!question", "!message Stream:", 1),
msg.author.id,
)
.ok_or_else(|| {
String::from(
"Nachricht konnte nicht gesendet werden.
Bitte achte auf die richtige Formulierung: !message <name>: <text>,
z.B. !message Lana: Hawwu!",
)
})
.and_then(|m| INBOX.queue_message(&m))
.map(|n| {
if let Err(e) = msg.reply(
&ctx,
&format!("Deine Nachricht wurde erfolgreich an {} gesendet", n),
) {
eprintln!("Could not reply because of {:?}", e);
}
})
.map_err(|e| {
if let Err(e) = msg.reply(ctx, &e) {
eprintln!("Could not reply because of {:?}", e);
}
CommandError(e)
})
}