Compare commits

...

No commits in common. "master" and "master" have entirely different histories.

5 changed files with 151 additions and 122 deletions

View File

@ -1,5 +1,5 @@
use serenity::{ use serenity::{
framework::standard::{macros::command, Args, CommandError, CommandResult}, framework::standard::{macros::command, Args, CommandResult},
model::channel::Message, model::channel::Message,
prelude::*, prelude::*,
}; };
@ -8,18 +8,16 @@ use serenity::{
#[command] #[command]
#[aliases("what's this")] #[aliases("what's this")]
async fn define(ctx: &Context, message: &Message, args: Args) -> CommandResult { async fn define(ctx: &Context, message: &Message, args: Args) -> CommandResult {
if args.is_empty() {
return Err("No arguments!".into());
}
let text: String = args.rest().trim().to_string(); let text: String = args.rest().trim().to_string();
let defs = let defs = &urbandict::get_definitions(&text);
urbandict::get_definitions(&text).map_err(|_| CommandError::from("Invalid query >w<"))?; if !args.is_empty() {
match defs {
let def = defs Err(_e) => {
.first() return Err("Invalid query >w<".into());
.ok_or_else(|| CommandError::from("No results!"))?; }
Ok(v) => {
if !v.is_empty() {
let def = &v[0];
let _ = message let _ = message
.channel_id .channel_id
.send_message(&ctx.http, |m| { .send_message(&ctx.http, |m| {
@ -34,6 +32,12 @@ async fn define(ctx: &Context, message: &Message, args: Args) -> CommandResult {
}) })
}) })
.await; .await;
} else {
return Err("No results!".into());
}
}
}
}
Ok(()) Ok(())
} }

View File

@ -1,7 +1,7 @@
use percent_encoding::{percent_encode, NON_ALPHANUMERIC}; use percent_encoding::{percent_encode, NON_ALPHANUMERIC};
use serde::Deserialize; use serde::Deserialize;
use serenity::{ use serenity::{
framework::standard::{macros::command, Args, CommandError, CommandResult}, framework::standard::{macros::command, Args, CommandResult},
model::channel::Message, model::channel::Message,
prelude::*, prelude::*,
}; };
@ -24,19 +24,24 @@ async fn lyrics(ctx: &Context, message: &Message, args: Args) -> CommandResult {
// TODO: use https://orion.apiseeds.com/api/music/lyric/:artist/:track instead // TODO: use https://orion.apiseeds.com/api/music/lyric/:artist/:track instead
let mut url = String::from("https://mourits.xyz:2096/?q="); let mut url = String::from("https://mourits.xyz:2096/?q=");
// check if input is not empty // check if input is not empty
let input = args.rest().trim(); let input = match args.rest().trim() {
if input.is_empty() { "" => {
return Err("Called without input!".into()); return Err("Called without input!".into());
} }
v => v,
};
// encode into url // encode into url
url += &s!(percent_encode(input.as_bytes(), NON_ALPHANUMERIC)); url += &s!(percent_encode(input.as_bytes(), NON_ALPHANUMERIC));
let request = reqwest::get(&url).await?; let request = match reqwest::get(&url).await {
Ok(v) => v,
Err(e) => return Err(e.into()),
};
let resp: Response = request let resp: Response = match request.json().await {
.json() Ok(v) => v,
.await Err(_) => return Err("Could not find lyrics".into()),
.map_err(|_| CommandError::from("Could not find lyrics"))?; };
let _ = message let _ = message
.channel_id .channel_id
.send_message(&ctx.http, |m| { .send_message(&ctx.http, |m| {

View File

@ -1,5 +1,5 @@
use serenity::{ use serenity::{
framework::standard::{macros::command, Args, CommandError, CommandResult}, framework::standard::{macros::command, Args, CommandResult},
model::{channel::Message, id::ChannelId}, model::{channel::Message, id::ChannelId},
prelude::*, prelude::*,
}; };
@ -19,10 +19,12 @@ async fn pinned(ctx: &Context, message: &Message, mut args: Args) -> CommandResu
Ok(v) => v, Ok(v) => v,
Err(_) => message.channel_id, Err(_) => message.channel_id,
}; };
let pinned = target_channel let pinned = match target_channel.pins(&ctx.http).await {
.pins(&ctx.http) Ok(v) => v,
.await Err(e) => {
.map_err(|e| CommandError::from(format!("Could not get pinned messages! Error: {}", e)))?; return Err(format!("Could not get pinned messages! Error: {}", e).into());
}
};
if pinned.is_empty() { if pinned.is_empty() {
return Err("No pinned messages found!".into()); return Err("No pinned messages found!".into());
} }

View File

@ -1,6 +1,6 @@
use serenity::{ use serenity::{
builder::CreateMessage, builder::CreateMessage,
framework::standard::{macros::command, Args, CommandError, CommandResult}, framework::standard::{macros::command, Args, CommandResult},
http::AttachmentType, http::AttachmentType,
model::channel::Message, model::channel::Message,
prelude::*, prelude::*,
@ -10,11 +10,11 @@ use serenity::{
#[command] #[command]
async fn spoiler(ctx: &Context, message: &Message, args: Args) -> CommandResult { async fn spoiler(ctx: &Context, message: &Message, args: Args) -> CommandResult {
// check if the message has any attachments // check if the message has any attachments
let attachments = match message.attachments.is_empty() {
let attachments = if message.attachments.is_empty() { true => {
return Err("No images were attached!".into()); return Err("No images were attached!".into());
} else { }
&message.attachments false => &message.attachments,
}; };
// get the author's nick in the server, otherwise default to global username later // get the author's nick in the server, otherwise default to global username later
@ -42,10 +42,10 @@ async fn spoiler(ctx: &Context, message: &Message, args: Args) -> CommandResult
for a in attachments { for a in attachments {
// download each attachment // download each attachment
let content = a let content = match a.download().await {
.download() Ok(content) => content,
.await Err(_) => return Err("Error downloading attachment".into()),
.map_err(|_| CommandError::from("Error downloading attachment"))?; };
let content: &[u8] = content.as_slice(); let content: &[u8] = content.as_slice();

View File

@ -14,8 +14,7 @@ use serenity::{
}, },
framework::standard::{ framework::standard::{
macros::{check, command, group, hook}, macros::{check, command, group, hook},
Args, CommandError, CommandOptions, CommandResult, DispatchError, Reason, Args, CommandOptions, CommandResult, DispatchError, Reason, StandardFramework,
StandardFramework,
}, },
model::{ model::{
channel::{Message, ReactionType}, channel::{Message, ReactionType},
@ -92,7 +91,9 @@ async fn dispatch_error(ctx: &Context, msg: &Message, error: DispatchError) {
#[hook] #[hook]
async fn after(ctx: &Context, msg: &Message, command_name: &str, command_result: CommandResult) { async fn after(ctx: &Context, msg: &Message, command_name: &str, command_result: CommandResult) {
// prints error in chat // prints error in chat
if let Err(why) = command_result { match command_result {
Ok(()) => (),
Err(why) => {
let _ = msg let _ = msg
.channel_id .channel_id
.send_message(&ctx.http, |m| { .send_message(&ctx.http, |m| {
@ -115,6 +116,7 @@ async fn after(ctx: &Context, msg: &Message, command_name: &str, command_result:
); );
} }
} }
}
#[group] #[group]
#[commands( #[commands(
@ -207,21 +209,23 @@ async fn ping(ctx: &Context, message: &Message) -> CommandResult {
// I have no idea if this works but its 5æm and I need to sleep help // I have no idea if this works but its 5æm and I need to sleep help
let data = ctx.data.read().await; let data = ctx.data.read().await;
let shard_manager = data let shard_manager = match data.get::<ShardManagerContainer>() {
.get::<ShardManagerContainer>() Some(v) => v,
.ok_or_else(|| CommandError::from("There was a problem getting the shard manager!"))?; None => return Err("There was a problem getting the shard manager!".into()),
};
let manager = shard_manager.lock().await; let manager = shard_manager.lock().await;
let runners = manager.runners.lock().await; let runners = manager.runners.lock().await;
let runner = runners let runner = match runners.get(&ShardId(ctx.shard_id)) {
.get(&ShardId(ctx.shard_id)) Some(v) => v,
.ok_or_else(|| CommandError::from("No shard found!"))?; None => return Err("No shard found!".into()),
};
let ping = runner let ping = match runner.latency {
.latency Some(v) => v.as_millis(),
.ok_or_else(|| CommandError::from("Could not get latency!"))? None => return Err("Could not get latency!".into()),
.as_millis(); };
let _ = message let _ = message
.channel_id .channel_id
@ -343,12 +347,14 @@ async fn bottom_rng(ctx: &Context, message: &Message, mut args: Args) -> Command
// get N last messages, otherwise 10 // get N last messages, otherwise 10
let num = args.single::<u64>().unwrap_or(10); let num = args.single::<u64>().unwrap_or(10);
let mut messages = message let messages = message
.channel_id .channel_id
.messages(&ctx.http, |get| get.before(message.id).limit(num)) .messages(&ctx.http, |get| get.before(message.id).limit(num))
.await .await;
.map_err(|e| CommandError::from(format!("Error: {}", e)))?; if let Err(e) = messages {
return Err(format!("Error: {}", e).into());
} else {
let mut messages = messages?;
// remove all messages by other users // remove all messages by other users
messages.retain(|v| v.author != message.mentions[0]); messages.retain(|v| v.author != message.mentions[0]);
let mut input = String::new(); let mut input = String::new();
@ -366,6 +372,7 @@ async fn bottom_rng(ctx: &Context, message: &Message, mut args: Args) -> Command
}) })
}) })
.await; .await;
}
Ok(()) Ok(())
} }
@ -385,7 +392,7 @@ async fn headpat(ctx: &Context, message: &Message, args: Args) -> CommandResult
_ => message.mentions[0].name.as_str(), _ => message.mentions[0].name.as_str(),
}; };
message if let Err(e) = message
.channel_id .channel_id
.send_message(&ctx.http, |m| { .send_message(&ctx.http, |m| {
m.embed(|e| { m.embed(|e| {
@ -396,7 +403,10 @@ async fn headpat(ctx: &Context, message: &Message, args: Args) -> CommandResult
.description("[Source](https://www.pinterest.com/pin/377809856242075277/)") .description("[Source](https://www.pinterest.com/pin/377809856242075277/)")
}) })
}) })
.await?; .await
{
let _ = message.channel_id.say(&ctx.http, format!("{:?}", e)).await;
};
Ok(()) Ok(())
} }
@ -523,18 +533,20 @@ async fn pfp(ctx: &Context, message: &Message) -> CommandResult {
async fn owo(ctx: &Context, message: &Message, args: Args) -> CommandResult { async fn owo(ctx: &Context, message: &Message, args: Args) -> CommandResult {
use owoify::OwOifiable; use owoify::OwOifiable;
let messages = message let lastmsg = match message
.channel_id .channel_id
.messages(&ctx.http, |get| get.before(message.id).limit(1)) .messages(&ctx.http, |get| get.before(message.id).limit(1))
.await .await
.map_err(|_| CommandError::from("Could not get last message!"))?; {
Ok(v) => v,
Err(_) => return Err("Could not get last message!".into()),
};
let lastmsg = &messages[0].content; let lastmsg = &lastmsg[0].content;
let input: String = if args.is_empty() { let input: String = match args.is_empty() {
s!(lastmsg) true => s!(lastmsg),
} else { false => args.rest().trim().to_string(),
args.rest().trim().to_string()
}; };
let _ = message.channel_id.say(&ctx.http, input.owoify()).await; let _ = message.channel_id.say(&ctx.http, input.owoify()).await;
@ -546,17 +558,23 @@ async fn owo(ctx: &Context, message: &Message, args: Args) -> CommandResult {
#[only_in(guilds)] #[only_in(guilds)]
#[aliases("description", "topic")] #[aliases("description", "topic")]
async fn desc(ctx: &Context, message: &Message) -> CommandResult { async fn desc(ctx: &Context, message: &Message) -> CommandResult {
let channel = message let channel = match message.channel(&ctx).await {
.channel(&ctx) Some(ch) => ch,
.await None => {
.ok_or_else(|| CommandError::from("Could not get channel!"))?; return Err("Could not get channel!".into());
let channel = channel }
.guild() };
.ok_or_else(|| CommandError::from("Could not get guild channel!"))?; let channel = match channel.guild() {
Some(g) => g,
None => {
return Err("Could not get guild channel!".into());
}
};
let topic = match channel.topic.as_deref() { let topic = if channel.topic.clone().unwrap() != "" {
Some("") | None => String::from("No channel topic found"), channel.topic.clone().unwrap()
Some(topic) => topic.to_string(), } else {
String::from("No channel topic found")
}; };
let _ = message let _ = message