Fixed some Clippy lints
This commit is contained in:
parent
ebf8bf1cb2
commit
244dfac270
2
Cargo.lock
generated
2
Cargo.lock
generated
@ -1514,7 +1514,7 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "shibe-bot"
|
name = "shibe-bot"
|
||||||
version = "0.3.0"
|
version = "0.3.1"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"build-time",
|
"build-time",
|
||||||
"dotenvy",
|
"dotenvy",
|
||||||
|
@ -1,6 +1,9 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "shibe-bot"
|
name = "shibe-bot"
|
||||||
version = "0.3.0"
|
description = "A Discord bot written in Rust, using Poise."
|
||||||
|
license = "MIT"
|
||||||
|
readme = "README.md"
|
||||||
|
version = "0.3.1"
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
|
|
||||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
# Shibe Bot 0.3.0
|
# Shibe Bot 0.3.1
|
||||||
|
|
||||||
[](https://github.com/shibedrill/shibe-bot/actions/workflows/rust.yml)
|
[](https://github.com/shibedrill/shibe-bot/actions/workflows/rust.yml)
|
||||||
[](LICENSE.txt)
|
[](LICENSE.txt)
|
||||||
|
@ -4,10 +4,10 @@ use crate::Error;
|
|||||||
use poise::serenity_prelude as serenity;
|
use poise::serenity_prelude as serenity;
|
||||||
|
|
||||||
use rand::prelude::SliceRandom;
|
use rand::prelude::SliceRandom;
|
||||||
use rand::*;
|
use rand::Rng;
|
||||||
|
|
||||||
use roux::util::*;
|
use roux::util::{FeedOption, TimePeriod};
|
||||||
use roux::*;
|
use roux::Subreddit;
|
||||||
|
|
||||||
/// mrow
|
/// mrow
|
||||||
#[poise::command(slash_command)]
|
#[poise::command(slash_command)]
|
||||||
@ -31,11 +31,12 @@ pub async fn meow(ctx: Context<'_>) -> Result<(), Error> {
|
|||||||
];
|
];
|
||||||
let response = {
|
let response = {
|
||||||
let mut rng = rand::thread_rng();
|
let mut rng = rand::thread_rng();
|
||||||
match rng.gen_bool(0.05) {
|
if rng.gen_bool(0.05) {
|
||||||
true => "woof",
|
"woof"
|
||||||
// Will never return None. The source is statically defined.
|
// Will never return None. The source is statically defined.
|
||||||
// We know it will always have items in it.
|
// We know it will always have items in it.
|
||||||
false => meows
|
} else {
|
||||||
|
meows
|
||||||
.choose(&mut rng)
|
.choose(&mut rng)
|
||||||
.ok_or("`meows` array is empty")
|
.ok_or("`meows` array is empty")
|
||||||
.inspect_err(|e| {
|
.inspect_err(|e| {
|
||||||
@ -108,7 +109,7 @@ pub async fn eightball(ctx: Context<'_>) -> Result<(), Error> {
|
|||||||
.choose(&mut rng)
|
.choose(&mut rng)
|
||||||
.ok_or("Response array is empty".to_string())
|
.ok_or("Response array is empty".to_string())
|
||||||
.inspect_err(|e| {
|
.inspect_err(|e| {
|
||||||
error!("Executing command `eightball` failed: {}", e)
|
error!("Executing command `eightball` failed: {}", e);
|
||||||
})?
|
})?
|
||||||
};
|
};
|
||||||
ctx.say(format!("Magic 8-ball says: '{}'", *response))
|
ctx.say(format!("Magic 8-ball says: '{}'", *response))
|
||||||
@ -151,7 +152,7 @@ pub async fn deer(ctx: Context<'_>) -> Result<(), Error> {
|
|||||||
.choose(&mut rng)
|
.choose(&mut rng)
|
||||||
.ok_or("Unable to get any hot posts")
|
.ok_or("Unable to get any hot posts")
|
||||||
.inspect_err(|e| {
|
.inspect_err(|e| {
|
||||||
error!("Executing command `deer` failed: {}", e)
|
error!("Executing command `deer` failed: {}", e);
|
||||||
})?
|
})?
|
||||||
};
|
};
|
||||||
ctx.say(format!("https://vxreddit.com{}", &chosen_post.data.permalink))
|
ctx.say(format!("https://vxreddit.com{}", &chosen_post.data.permalink))
|
||||||
|
@ -13,10 +13,9 @@ const INVITE_LINK: &str = "https://discord.com/oauth2/authorize?client_id=103070
|
|||||||
pub async fn invite(ctx: Context<'_>) -> Result<(), Error> {
|
pub async fn invite(ctx: Context<'_>) -> Result<(), Error> {
|
||||||
ctx.defer_ephemeral().await?;
|
ctx.defer_ephemeral().await?;
|
||||||
ctx.say(format!(
|
ctx.say(format!(
|
||||||
"To add me to your server, click [this link]({}), or open it in the \
|
"To add me to your server, click [this link]({INVITE_LINK}), or open it in the \
|
||||||
browser and enable all the requested permissions. Then select your \
|
browser and enable all the requested permissions. Then select your \
|
||||||
server to add it.",
|
server to add it.",
|
||||||
INVITE_LINK
|
|
||||||
))
|
))
|
||||||
.await?;
|
.await?;
|
||||||
info!("Executed command `invite` successfully");
|
info!("Executed command `invite` successfully");
|
||||||
@ -69,14 +68,11 @@ pub async fn add_channel(
|
|||||||
match config.channels.iter().find(|item| **item == channel_id) {
|
match config.channels.iter().find(|item| **item == channel_id) {
|
||||||
None => {
|
None => {
|
||||||
config.channels.push(channel_id);
|
config.channels.push(channel_id);
|
||||||
ctx.say(format!(
|
ctx.say(format!("Successfully added <#{channel_id}> to the channel registry."))
|
||||||
"Successfully added <#{}> to the channel registry.",
|
|
||||||
channel_id
|
|
||||||
))
|
|
||||||
.await?;
|
.await?;
|
||||||
}
|
}
|
||||||
Some(_) => {
|
Some(_) => {
|
||||||
ctx.say(format!("Channel <#{}> is already in registry.", channel_id))
|
ctx.say(format!("Channel <#{channel_id}> is already in registry."))
|
||||||
.await?;
|
.await?;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -96,18 +92,12 @@ pub async fn remove_channel(
|
|||||||
let channel_id = { u64::from(channel.id()) };
|
let channel_id = { u64::from(channel.id()) };
|
||||||
match config.channels.iter().position(|item| *item == channel_id) {
|
match config.channels.iter().position(|item| *item == channel_id) {
|
||||||
None => {
|
None => {
|
||||||
ctx.say(format!(
|
ctx.say(format!("Channel <#{channel_id}> was not in the channel registry."))
|
||||||
"Channel <#{}> was not in the channel registry.",
|
|
||||||
channel_id
|
|
||||||
))
|
|
||||||
.await?;
|
.await?;
|
||||||
}
|
}
|
||||||
Some(found) => {
|
Some(found) => {
|
||||||
config.channels.remove(found);
|
config.channels.remove(found);
|
||||||
ctx.say(format!(
|
ctx.say(format!("Successfully removed <#{channel_id}> from the channel registry."))
|
||||||
"Successfully removed <#{}> from the channel registry.",
|
|
||||||
channel_id
|
|
||||||
))
|
|
||||||
.await?;
|
.await?;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -155,12 +145,7 @@ pub async fn dice(
|
|||||||
}
|
}
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
_ => {
|
_ => format!("Rolled a random number from 1 to {sides}, got: {answer}")
|
||||||
format!(
|
|
||||||
"Rolled a random number from 1 to {}, got: {}",
|
|
||||||
sides, answer
|
|
||||||
)
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
ctx.say(response).await?;
|
ctx.say(response).await?;
|
||||||
info!("Executed command `dice` successfully");
|
info!("Executed command `dice` successfully");
|
||||||
|
12
src/main.rs
12
src/main.rs
@ -17,9 +17,9 @@ extern crate pretty_env_logger;
|
|||||||
extern crate log;
|
extern crate log;
|
||||||
|
|
||||||
// For managing config storage
|
// For managing config storage
|
||||||
use serde::*;
|
use serde::{Deserialize, Serialize};
|
||||||
mod settings;
|
mod settings;
|
||||||
use crate::settings::*;
|
use crate::settings::Manager;
|
||||||
|
|
||||||
// Bot commands
|
// Bot commands
|
||||||
mod command;
|
mod command;
|
||||||
@ -34,7 +34,7 @@ use crate::command::{
|
|||||||
|
|
||||||
// Data passed to every command (shared state)
|
// Data passed to every command (shared state)
|
||||||
struct Data {
|
struct Data {
|
||||||
config_manager: Arc<Mutex<SettingsManager<Settings>>>,
|
config_manager: Arc<Mutex<Manager<Settings>>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
// Errors returnable by a command
|
// Errors returnable by a command
|
||||||
@ -87,9 +87,9 @@ async fn main() {
|
|||||||
pretty_env_logger::init();
|
pretty_env_logger::init();
|
||||||
|
|
||||||
// Configure persistent options
|
// Configure persistent options
|
||||||
let config_manager: Arc<Mutex<SettingsManager<Settings>>> = Arc::new(Mutex::new(
|
let config_manager: Arc<Mutex<Manager<Settings>>> = Arc::new(Mutex::new(
|
||||||
SettingsManager::load(SETTINGS_PATH)
|
Manager::load(SETTINGS_PATH)
|
||||||
.unwrap_or(SettingsManager::manage(SETTINGS_PATH, Settings::default())),
|
.unwrap_or(Manager::manage(SETTINGS_PATH, Settings::default())),
|
||||||
));
|
));
|
||||||
config_manager.lock().await.store();
|
config_manager.lock().await.store();
|
||||||
|
|
||||||
|
@ -5,12 +5,12 @@ use serde::de::Deserialize;
|
|||||||
use serde::ser::Serialize;
|
use serde::ser::Serialize;
|
||||||
|
|
||||||
/// A utility structure to manage a settings structure.
|
/// A utility structure to manage a settings structure.
|
||||||
pub struct SettingsManager<T: Default + Serialize + for<'a> Deserialize<'a>> {
|
pub struct Manager<T: Default + Serialize + for<'a> Deserialize<'a>> {
|
||||||
internal: T,
|
internal: T,
|
||||||
path: String,
|
path: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T: Default + Serialize + for<'a> Deserialize<'a>> SettingsManager<T> {
|
impl<T: Default + Serialize + for<'a> Deserialize<'a>> Manager<T> {
|
||||||
/// Instantiate new self if the path contains a valid serialization of
|
/// Instantiate new self if the path contains a valid serialization of
|
||||||
/// the settings structure.
|
/// the settings structure.
|
||||||
pub fn load(path: &str) -> Option<Self> {
|
pub fn load(path: &str) -> Option<Self> {
|
||||||
@ -52,7 +52,7 @@ impl<T: Default + Serialize + for<'a> Deserialize<'a>> SettingsManager<T> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T: Default + Serialize + for<'a> Deserialize<'a>> Deref for SettingsManager<T> {
|
impl<T: Default + Serialize + for<'a> Deserialize<'a>> Deref for Manager<T> {
|
||||||
type Target = T;
|
type Target = T;
|
||||||
|
|
||||||
fn deref(&self) -> &Self::Target {
|
fn deref(&self) -> &Self::Target {
|
||||||
@ -60,7 +60,7 @@ impl<T: Default + Serialize + for<'a> Deserialize<'a>> Deref for SettingsManager
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T: Default + Serialize + for<'a> Deserialize<'a>> DerefMut for SettingsManager<T> {
|
impl<T: Default + Serialize + for<'a> Deserialize<'a>> DerefMut for Manager<T> {
|
||||||
fn deref_mut(&mut self) -> &mut Self::Target {
|
fn deref_mut(&mut self) -> &mut Self::Target {
|
||||||
&mut self.internal
|
&mut self.internal
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user