once_cell -> std

This commit is contained in:
seth 2024-03-30 03:04:32 -04:00
parent a41a84fd2d
commit 90387c5a3b
No known key found for this signature in database
GPG key ID: D31BD0D494BBEE86
28 changed files with 157 additions and 153 deletions

View file

@ -34,7 +34,7 @@ pub async fn handle(error: FrameworkError<'_, Data, Report>) {
.title("Something went wrong!")
.description("oopsie")
.timestamp(Timestamp::now())
.color(consts::COLORS["red"]);
.color(consts::colors()["red"]);
let reply = CreateReply::default().embed(embed);

View file

@ -1,8 +1,9 @@
use crate::{api, Data};
use std::sync::OnceLock;
use eyre::Result;
use log::trace;
use once_cell::sync::Lazy;
use regex::Regex;
pub type Issue = Option<(String, String)>;
@ -103,12 +104,15 @@ fn intel_hd(log: &str) -> Issue {
}
fn java_option(log: &str) -> Issue {
static VM_OPTION_REGEX: Lazy<Regex> =
Lazy::new(|| Regex::new(r"Unrecognized VM option '(.+)'[\r\n]").unwrap());
static OPTION_REGEX: Lazy<Regex> =
Lazy::new(|| Regex::new(r"Unrecognized option: (.+)[\r\n]").unwrap());
static VM_OPTION_REGEX: OnceLock<Regex> = OnceLock::new();
static UNRECOGNIZED_OPTION_REGEX: OnceLock<Regex> = OnceLock::new();
if let Some(captures) = VM_OPTION_REGEX.captures(log) {
let vm_option =
VM_OPTION_REGEX.get_or_init(|| Regex::new(r"Unrecognized VM option '(.+)'[\r\n]").unwrap());
let unrecognized_option = UNRECOGNIZED_OPTION_REGEX
.get_or_init(|| Regex::new(r"Unrecognized option: (.+)[\r\n]").unwrap());
if let Some(captures) = vm_option.captures(log) {
let title = if &captures[1] == "UseShenandoahGC" {
"Wrong Java Arguments"
} else {
@ -120,7 +124,7 @@ fn java_option(log: &str) -> Issue {
));
}
if let Some(captures) = OPTION_REGEX.captures(log) {
if let Some(captures) = unrecognized_option.captures(log) {
return Some((
"Wrong Java Arguments".to_string(),
format!("Remove `{}` from your Java arguments", &captures[1]),
@ -180,10 +184,11 @@ fn optinotfine(log: &str) -> Issue {
}
async fn outdated_launcher(log: &str, data: &Data) -> Result<Issue> {
static OUTDATED_LAUNCHER_REGEX: Lazy<Regex> =
Lazy::new(|| Regex::new("Prism Launcher version: [0-9].[0-9].[0-9]").unwrap());
static OUTDATED_LAUNCHER_REGEX: OnceLock<Regex> = OnceLock::new();
let outdated_launcher = OUTDATED_LAUNCHER_REGEX
.get_or_init(|| Regex::new("Prism Launcher version: [0-9].[0-9].[0-9]").unwrap());
let Some(captures) = OUTDATED_LAUNCHER_REGEX.captures(log) else {
let Some(captures) = outdated_launcher.captures(log) else {
return Ok(None);
};
@ -235,13 +240,12 @@ which is why the issue was not present."
}
fn wrong_java(log: &str) -> Issue {
static SWITCH_VERSION_REGEX: Lazy<Regex> = Lazy::new(|| {
Regex::new(
r"(?m)Please switch to one of the following Java versions for this instance:[\r\n]+(Java version [\d.]+)",
).unwrap()
});
static SWITCH_VERSION_REGEX: OnceLock<Regex> = OnceLock::new();
let switch_version = SWITCH_VERSION_REGEX.get_or_init(|| Regex::new(
r"(?m)Please switch to one of the following Java versions for this instance:[\r\n]+(Java version [\d.]+)",
).unwrap());
if let Some(captures) = SWITCH_VERSION_REGEX.captures(log) {
if let Some(captures) = switch_version.captures(log) {
let versions = captures[1].split('\n').collect::<Vec<&str>>().join(", ");
return Some((
"Wrong Java Version".to_string(),

View file

@ -1,5 +1,4 @@
use crate::consts::COLORS;
use crate::Data;
use crate::{consts, Data};
use eyre::Result;
use log::{debug, trace};
@ -49,10 +48,10 @@ pub async fn handle(ctx: &Context, message: &Message, data: &Data) -> Result<()>
if issues.is_empty() {
e = e
.color(COLORS["green"])
.color(consts::colors()["green"])
.description("No issues found automatically");
} else {
e = e.color(COLORS["red"]);
e = e.color(consts::colors()["red"]);
for (title, description) in issues {
e = e.field(title, description, false);

View file

@ -1,8 +1,9 @@
use crate::utils;
use crate::api;
use std::sync::OnceLock;
use eyre::Result;
use log::trace;
use once_cell::sync::Lazy;
use poise::serenity_prelude::Message;
use regex::Regex;
@ -10,17 +11,18 @@ pub struct _0x0;
impl super::LogProvider for _0x0 {
async fn find_match(&self, message: &Message) -> Option<String> {
static REGEX: Lazy<Regex> = Lazy::new(|| Regex::new(r"https://0x0\.st/\w*.\w*").unwrap());
static REGEX: OnceLock<Regex> = OnceLock::new();
let regex = REGEX.get_or_init(|| Regex::new(r"https://0x0\.st/\w*.\w*").unwrap());
trace!("Checking if message {} is a 0x0 paste", message.id);
REGEX
regex
.find_iter(&message.content)
.map(|m| m.as_str().to_string())
.nth(0)
}
async fn fetch(&self, content: &str) -> Result<String> {
let log = utils::text_from_url(content).await?;
let log = api::text_from_url(content).await?;
Ok(log)
}

View file

@ -2,7 +2,7 @@ use eyre::Result;
use log::trace;
use poise::serenity_prelude::Message;
use crate::utils;
use crate::api;
pub struct Attachment;
@ -22,7 +22,7 @@ impl super::LogProvider for Attachment {
}
async fn fetch(&self, content: &str) -> Result<String> {
let attachment = utils::bytes_from_url(content).await?;
let attachment = api::bytes_from_url(content).await?;
let log = String::from_utf8(attachment)?;
Ok(log)
}

View file

@ -1,8 +1,9 @@
use crate::utils;
use crate::api;
use std::sync::OnceLock;
use eyre::Result;
use log::trace;
use once_cell::sync::Lazy;
use poise::serenity_prelude::Message;
use regex::Regex;
@ -13,16 +14,17 @@ pub struct Haste;
impl super::LogProvider for Haste {
async fn find_match(&self, message: &Message) -> Option<String> {
static REGEX: Lazy<Regex> =
Lazy::new(|| Regex::new(r"https://hst\.sh(?:/raw)?/(\w+(?:\.\w*)?)").unwrap());
static REGEX: OnceLock<Regex> = OnceLock::new();
let regex =
REGEX.get_or_init(|| Regex::new(r"https://hst\.sh(?:/raw)?/(\w+(?:\.\w*)?)").unwrap());
trace!("Checking if message {} is a hst.sh paste", message.id);
super::get_first_capture(&REGEX, &message.content)
super::get_first_capture(regex, &message.content)
}
async fn fetch(&self, content: &str) -> Result<String> {
let url = format!("{HASTE}{RAW}/{content}");
let log = utils::text_from_url(&url).await?;
let log = api::text_from_url(&url).await?;
Ok(log)
}

View file

@ -1,8 +1,9 @@
use crate::utils;
use crate::api;
use std::sync::OnceLock;
use eyre::Result;
use log::trace;
use once_cell::sync::Lazy;
use poise::serenity_prelude::Message;
use regex::Regex;
@ -13,15 +14,16 @@ pub struct MCLogs;
impl super::LogProvider for MCLogs {
async fn find_match(&self, message: &Message) -> Option<String> {
static REGEX: Lazy<Regex> = Lazy::new(|| Regex::new(r"https://mclo\.gs/(\w+)").unwrap());
static REGEX: OnceLock<Regex> = OnceLock::new();
let regex = REGEX.get_or_init(|| Regex::new(r"https://mclo\.gs/(\w+)").unwrap());
trace!("Checking if message {} is an mclo.gs paste", message.id);
super::get_first_capture(&REGEX, &message.content)
super::get_first_capture(regex, &message.content)
}
async fn fetch(&self, content: &str) -> Result<String> {
let url = format!("{MCLOGS}{RAW}/{content}");
let log = utils::text_from_url(&url).await?;
let log = api::text_from_url(&url).await?;
Ok(log)
}

View file

@ -2,7 +2,6 @@ use std::slice::Iter;
use enum_dispatch::enum_dispatch;
use eyre::Result;
use once_cell::sync::Lazy;
use poise::serenity_prelude::Message;
use regex::Regex;
@ -25,7 +24,7 @@ pub trait LogProvider {
async fn fetch(&self, content: &str) -> Result<String>;
}
fn get_first_capture(regex: &Lazy<Regex>, string: &str) -> Option<String> {
fn get_first_capture(regex: &Regex, string: &str) -> Option<String> {
regex
.captures_iter(string)
.find_map(|c| c.get(1).map(|c| c.as_str().to_string()))

View file

@ -1,8 +1,9 @@
use crate::api::paste_gg;
use std::sync::OnceLock;
use eyre::{OptionExt, Result};
use log::trace;
use once_cell::sync::Lazy;
use poise::serenity_prelude::Message;
use regex::Regex;
@ -10,11 +11,11 @@ pub struct PasteGG;
impl super::LogProvider for PasteGG {
async fn find_match(&self, message: &Message) -> Option<String> {
static REGEX: Lazy<Regex> =
Lazy::new(|| Regex::new(r"https://paste.gg/p/\w+/(\w+)").unwrap());
static REGEX: OnceLock<Regex> = OnceLock::new();
let regex = REGEX.get_or_init(|| Regex::new(r"https://paste.gg/p/\w+/(\w+)").unwrap());
trace!("Checking if message {} is a paste.gg paste", message.id);
super::get_first_capture(&REGEX, &message.content)
super::get_first_capture(regex, &message.content)
}
async fn fetch(&self, content: &str) -> Result<String> {

View file

@ -1,8 +1,9 @@
use crate::utils;
use crate::api;
use std::sync::OnceLock;
use eyre::Result;
use log::trace;
use once_cell::sync::Lazy;
use poise::serenity_prelude::Message;
use regex::Regex;
@ -13,16 +14,17 @@ pub struct PasteBin;
impl super::LogProvider for PasteBin {
async fn find_match(&self, message: &Message) -> Option<String> {
static REGEX: Lazy<Regex> =
Lazy::new(|| Regex::new(r"https://pastebin\.com(?:/raw)?/(\w+)").unwrap());
static REGEX: OnceLock<Regex> = OnceLock::new();
let regex =
REGEX.get_or_init(|| Regex::new(r"https://pastebin\.com(?:/raw)?/(\w+)").unwrap());
trace!("Checking if message {} is a pastebin paste", message.id);
super::get_first_capture(&REGEX, &message.content)
super::get_first_capture(regex, &message.content)
}
async fn fetch(&self, content: &str) -> Result<String> {
let url = format!("{PASTEBIN}{RAW}/{content}");
let log = utils::text_from_url(&url).await?;
let log = api::text_from_url(&url).await?;
Ok(log)
}

View file

@ -1,13 +1,17 @@
use std::sync::OnceLock;
use eyre::Result;
use log::trace;
use once_cell::sync::Lazy;
use poise::serenity_prelude::{Context, Message};
use rand::seq::SliceRandom;
use regex::Regex;
static ETA_REGEX: Lazy<Regex> = Lazy::new(|| Regex::new(r"\beta\b").unwrap());
fn regex() -> &'static Regex {
static REGEX: OnceLock<Regex> = OnceLock::new();
REGEX.get_or_init(|| Regex::new(r"\beta\b").unwrap())
}
const ETA_MESSAGES: [&str; 16] = [
const MESSAGES: [&str; 16] = [
"Sometime",
"Some day",
"Not far",
@ -27,7 +31,7 @@ const ETA_MESSAGES: [&str; 16] = [
];
pub async fn handle(ctx: &Context, message: &Message) -> Result<()> {
if !ETA_REGEX.is_match(&message.content) {
if !regex().is_match(&message.content) {
trace!(
"The message '{}' (probably) doesn't say ETA",
message.content
@ -37,7 +41,7 @@ pub async fn handle(ctx: &Context, message: &Message) -> Result<()> {
let response = format!(
"{} <:pofat:1031701005559144458>",
ETA_MESSAGES
MESSAGES
.choose(&mut rand::thread_rng())
.unwrap_or(&"sometime")
);