refactor: ok_or_else()
-> ok_or_eyre()
This commit is contained in:
parent
fafa0bf689
commit
f4fa737124
10 changed files with 23 additions and 29 deletions
|
@ -1,6 +1,6 @@
|
||||||
use crate::api::REQWEST_CLIENT;
|
use crate::api::REQWEST_CLIENT;
|
||||||
|
|
||||||
use eyre::{eyre, Context, Result};
|
use eyre::{eyre, Context, OptionExt, Result};
|
||||||
use log::debug;
|
use log::debug;
|
||||||
use reqwest::StatusCode;
|
use reqwest::StatusCode;
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
@ -30,12 +30,12 @@ pub async fn get_latest_minecraft_version() -> Result<String> {
|
||||||
let data = resp
|
let data = resp
|
||||||
.json::<MinecraftPackageJson>()
|
.json::<MinecraftPackageJson>()
|
||||||
.await
|
.await
|
||||||
.wrap_err_with(|| "Couldn't parse Minecraft versions!")?;
|
.wrap_err("Couldn't parse Minecraft versions!")?;
|
||||||
|
|
||||||
let version = data
|
let version = data
|
||||||
.recommended
|
.recommended
|
||||||
.first()
|
.first()
|
||||||
.ok_or_else(|| eyre!("Couldn't find latest version of Minecraft!"))?;
|
.ok_or_eyre("Couldn't find latest version of Minecraft!")?;
|
||||||
|
|
||||||
Ok(version.clone())
|
Ok(version.clone())
|
||||||
} else {
|
} else {
|
||||||
|
|
|
@ -21,13 +21,13 @@ pub async fn get(id: Option<u64>) -> Result<Response> {
|
||||||
let req = REQWEST_CLIENT
|
let req = REQWEST_CLIENT
|
||||||
.get(format!("{RORY}{ENDPOINT}/{target}"))
|
.get(format!("{RORY}{ENDPOINT}/{target}"))
|
||||||
.build()
|
.build()
|
||||||
.wrap_err_with(|| "Couldn't build reqwest client!")?;
|
.wrap_err("Couldn't build reqwest client!")?;
|
||||||
|
|
||||||
debug!("Making request to {}", req.url());
|
debug!("Making request to {}", req.url());
|
||||||
let resp = REQWEST_CLIENT
|
let resp = REQWEST_CLIENT
|
||||||
.execute(req)
|
.execute(req)
|
||||||
.await
|
.await
|
||||||
.wrap_err_with(|| "Couldn't make request for rory!")?;
|
.wrap_err("Couldn't make request for rory!")?;
|
||||||
|
|
||||||
let status = resp.status();
|
let status = resp.status();
|
||||||
|
|
||||||
|
@ -35,7 +35,7 @@ pub async fn get(id: Option<u64>) -> Result<Response> {
|
||||||
let data = resp
|
let data = resp
|
||||||
.json::<Response>()
|
.json::<Response>()
|
||||||
.await
|
.await
|
||||||
.wrap_err_with(|| "Couldn't parse the rory response!")?;
|
.wrap_err("Couldn't parse the rory response!")?;
|
||||||
|
|
||||||
Ok(data)
|
Ok(data)
|
||||||
} else {
|
} else {
|
||||||
|
|
|
@ -1,16 +1,13 @@
|
||||||
use crate::{consts, Context};
|
use crate::{consts, Context};
|
||||||
|
|
||||||
use eyre::{eyre, Result};
|
use eyre::{OptionExt, Result};
|
||||||
use poise::serenity_prelude::CreateEmbed;
|
use poise::serenity_prelude::CreateEmbed;
|
||||||
use poise::CreateReply;
|
use poise::CreateReply;
|
||||||
|
|
||||||
/// Returns the number of members in the server
|
/// Returns the number of members in the server
|
||||||
#[poise::command(slash_command, prefix_command)]
|
#[poise::command(slash_command, prefix_command)]
|
||||||
pub async fn members(ctx: Context<'_>) -> Result<()> {
|
pub async fn members(ctx: Context<'_>) -> Result<()> {
|
||||||
let guild = ctx
|
let guild = ctx.guild().ok_or_eyre("Couldn't fetch guild!")?.to_owned();
|
||||||
.guild()
|
|
||||||
.ok_or_else(|| eyre!("Couldn't fetch guild!"))?
|
|
||||||
.to_owned();
|
|
||||||
|
|
||||||
let count = guild.member_count;
|
let count = guild.member_count;
|
||||||
let online = if let Some(count) = guild.approximate_presence_count {
|
let online = if let Some(count) = guild.approximate_presence_count {
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
use crate::Context;
|
use crate::Context;
|
||||||
|
|
||||||
use eyre::{eyre, Result};
|
use eyre::{OptionExt, Result};
|
||||||
use poise::serenity_prelude::{CreateEmbed, CreateEmbedAuthor, CreateMessage};
|
use poise::serenity_prelude::{CreateEmbed, CreateEmbedAuthor, CreateMessage};
|
||||||
|
|
||||||
/// Say something through the bot
|
/// Say something through the bot
|
||||||
|
@ -12,14 +12,11 @@ use poise::serenity_prelude::{CreateEmbed, CreateEmbedAuthor, CreateMessage};
|
||||||
required_permissions = "MODERATE_MEMBERS"
|
required_permissions = "MODERATE_MEMBERS"
|
||||||
)]
|
)]
|
||||||
pub async fn say(ctx: Context<'_>, #[description = "Just content?"] content: String) -> Result<()> {
|
pub async fn say(ctx: Context<'_>, #[description = "Just content?"] content: String) -> Result<()> {
|
||||||
let guild = ctx
|
let guild = ctx.guild().ok_or_eyre("Couldn't get guild!")?.to_owned();
|
||||||
.guild()
|
|
||||||
.ok_or_else(|| eyre!("Couldn't get guild!"))?
|
|
||||||
.to_owned();
|
|
||||||
let channel = ctx
|
let channel = ctx
|
||||||
.guild_channel()
|
.guild_channel()
|
||||||
.await
|
.await
|
||||||
.ok_or_else(|| eyre!("Couldn't get channel!"))?;
|
.ok_or_eyre("Couldn't get channel!")?;
|
||||||
|
|
||||||
ctx.defer_ephemeral().await?;
|
ctx.defer_ephemeral().await?;
|
||||||
channel.say(ctx, &content).await?;
|
channel.say(ctx, &content).await?;
|
||||||
|
@ -30,7 +27,7 @@ pub async fn say(ctx: Context<'_>, #[description = "Just content?"] content: Str
|
||||||
.channels
|
.channels
|
||||||
.iter()
|
.iter()
|
||||||
.find(|c| c.0 == &channel_id)
|
.find(|c| c.0 == &channel_id)
|
||||||
.ok_or_else(|| eyre!("Couldn't get log channel from guild!"))?;
|
.ok_or_eyre("Couldn't get log channel from guild!")?;
|
||||||
|
|
||||||
let author = CreateEmbedAuthor::new(ctx.author().tag())
|
let author = CreateEmbedAuthor::new(ctx.author().tag())
|
||||||
.icon_url(ctx.author().avatar_url().unwrap_or("Undefined".to_string()));
|
.icon_url(ctx.author().avatar_url().unwrap_or("Undefined".to_string()));
|
||||||
|
|
|
@ -13,7 +13,7 @@ pub async fn stars(ctx: Context<'_>) -> Result<()> {
|
||||||
.repos("PrismLauncher", "PrismLauncher")
|
.repos("PrismLauncher", "PrismLauncher")
|
||||||
.get()
|
.get()
|
||||||
.await
|
.await
|
||||||
.wrap_err_with(|| "Couldn't get PrismLauncher/PrismLauncher from GitHub!")?;
|
.wrap_err("Couldn't get PrismLauncher/PrismLauncher from GitHub!")?;
|
||||||
|
|
||||||
let count = if let Some(count) = prismlauncher.stargazers_count {
|
let count = if let Some(count) = prismlauncher.stargazers_count {
|
||||||
count.to_string()
|
count.to_string()
|
||||||
|
|
|
@ -3,7 +3,7 @@ use crate::tags::Tag;
|
||||||
use crate::{consts, Context};
|
use crate::{consts, Context};
|
||||||
use std::env;
|
use std::env;
|
||||||
|
|
||||||
use eyre::{eyre, Result};
|
use eyre::{OptionExt, Result};
|
||||||
use once_cell::sync::Lazy;
|
use once_cell::sync::Lazy;
|
||||||
use poise::serenity_prelude::{Color, CreateEmbed, User};
|
use poise::serenity_prelude::{Color, CreateEmbed, User};
|
||||||
use poise::CreateReply;
|
use poise::CreateReply;
|
||||||
|
@ -22,7 +22,7 @@ pub async fn tag(
|
||||||
let tag = TAGS
|
let tag = TAGS
|
||||||
.iter()
|
.iter()
|
||||||
.find(|t| t.file_name == tag_file)
|
.find(|t| t.file_name == tag_file)
|
||||||
.ok_or_else(|| eyre!("Tried to get non-existent tag: {tag_file}"))?;
|
.ok_or_eyre("Tried to get non-existent tag: {tag_file}")?;
|
||||||
|
|
||||||
let frontmatter = &tag.frontmatter;
|
let frontmatter = &tag.frontmatter;
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
use crate::api::REQWEST_CLIENT;
|
use crate::api::REQWEST_CLIENT;
|
||||||
|
|
||||||
use eyre::{eyre, Result};
|
use eyre::{eyre, OptionExt, Result};
|
||||||
use once_cell::sync::Lazy;
|
use once_cell::sync::Lazy;
|
||||||
use regex::Regex;
|
use regex::Regex;
|
||||||
use reqwest::StatusCode;
|
use reqwest::StatusCode;
|
||||||
|
@ -48,7 +48,7 @@ pub async fn find(content: &str) -> Result<Option<String>> {
|
||||||
let paste_files: PasteResponse = resp.json().await?;
|
let paste_files: PasteResponse = resp.json().await?;
|
||||||
let file_id = &paste_files
|
let file_id = &paste_files
|
||||||
.result
|
.result
|
||||||
.ok_or_else(|| eyre!("Couldn't find any files associated with paste {paste_id}!"))?[0]
|
.ok_or_eyre("Couldn't find any files associated with paste {paste_id}!")?[0]
|
||||||
.id;
|
.id;
|
||||||
|
|
||||||
let raw_url = format!("{PASTE_GG}{PASTES_ENDPOINT}/{paste_id}/files/{file_id}/raw");
|
let raw_url = format!("{PASTE_GG}{PASTES_ENDPOINT}/{paste_id}/files/{file_id}/raw");
|
||||||
|
|
|
@ -5,12 +5,12 @@ pub async fn handle(ctx: &Context, reaction: &Reaction) -> Result<()> {
|
||||||
let user = reaction
|
let user = reaction
|
||||||
.user(ctx)
|
.user(ctx)
|
||||||
.await
|
.await
|
||||||
.wrap_err_with(|| "Couldn't fetch user from reaction!")?;
|
.wrap_err("Couldn't fetch user from reaction!")?;
|
||||||
|
|
||||||
let message = reaction
|
let message = reaction
|
||||||
.message(ctx)
|
.message(ctx)
|
||||||
.await
|
.await
|
||||||
.wrap_err_with(|| "Couldn't fetch message from reaction!")?;
|
.wrap_err("Couldn't fetch message from reaction!")?;
|
||||||
|
|
||||||
if let Some(interaction) = &message.interaction {
|
if let Some(interaction) = &message.interaction {
|
||||||
if interaction.kind == InteractionType::Command
|
if interaction.kind == InteractionType::Command
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
use eyre::{eyre, Result};
|
use eyre::{eyre, OptionExt, Result};
|
||||||
use log::debug;
|
use log::debug;
|
||||||
use poise::serenity_prelude::{
|
use poise::serenity_prelude::{
|
||||||
ChannelType, Context, CreateAllowedMentions, CreateMessage, GuildChannel,
|
ChannelType, Context, CreateAllowedMentions, CreateMessage, GuildChannel,
|
||||||
|
@ -24,7 +24,7 @@ pub async fn handle(ctx: &Context, thread: &GuildChannel) -> Result<()> {
|
||||||
|
|
||||||
let owner = thread
|
let owner = thread
|
||||||
.owner_id
|
.owner_id
|
||||||
.ok_or_else(|| eyre!("Couldn't get owner of thread!"))?;
|
.ok_or_eyre("Couldn't get owner of thread!")?;
|
||||||
|
|
||||||
let msg = format!(
|
let msg = format!(
|
||||||
"<@{}> We've received your support ticket! {} {}",
|
"<@{}> We've received your support ticket! {} {}",
|
||||||
|
|
|
@ -90,8 +90,8 @@ async fn main() -> Result<()> {
|
||||||
color_eyre::install()?;
|
color_eyre::install()?;
|
||||||
env_logger::init();
|
env_logger::init();
|
||||||
|
|
||||||
let token = std::env::var("DISCORD_BOT_TOKEN")
|
let token =
|
||||||
.wrap_err_with(|| "Couldn't find bot token in environment!")?;
|
std::env::var("DISCORD_BOT_TOKEN").wrap_err("Couldn't find bot token in environment!")?;
|
||||||
|
|
||||||
let intents =
|
let intents =
|
||||||
serenity::GatewayIntents::non_privileged() | serenity::GatewayIntents::MESSAGE_CONTENT;
|
serenity::GatewayIntents::non_privileged() | serenity::GatewayIntents::MESSAGE_CONTENT;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue