From b6941fc9fd84f1706d5f3fd50b481a27edc81571 Mon Sep 17 00:00:00 2001 From: BlackHoleFox Date: Sun, 3 May 2020 19:53:16 -0500 Subject: [PATCH] Tidyup --- Cargo.lock | 15 --------------- Cargo.toml | 7 +++---- src/commands/basic/coinflip.rs | 4 ++-- src/commands/basic/echo.rs | 1 - src/commands/meta/nodes.rs | 5 ++--- src/core/context.rs | 24 +++++++++--------------- src/core/gearbot.rs | 3 --- src/core/handlers/general.rs | 6 ++++-- src/core/handlers/modlog.rs | 1 - src/main.rs | 21 +++++++++------------ src/utils/matchers.rs | 3 +-- src/utils/mod.rs | 8 ++++---- 12 files changed, 34 insertions(+), 64 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 2c2f6205..3782c1a7 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -588,7 +588,6 @@ dependencies = [ "lazy_static", "log", "once_cell", - "postgres", "postgres-types", "rand", "refinery", @@ -1267,20 +1266,6 @@ dependencies = [ "universal-hash", ] -[[package]] -name = "postgres" -version = "0.17.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a08e48317fe57088aa1ff4a84a88a8401aee565f4ae5c201aa18d11c573ce350" -dependencies = [ - "bytes", - "fallible-iterator", - "futures", - "log", - "tokio", - "tokio-postgres", -] - [[package]] name = "postgres-protocol" version = "0.5.1" diff --git a/Cargo.toml b/Cargo.toml index 5340ffb3..170c3f8a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -17,17 +17,16 @@ git-version = "0.3" lazy_static = "1.4" log = "0.4" once_cell = "1.3" -postgres = {version="0.17", features=["with-serde_json-1"]} -postgres-types = "0.1" +postgres-types = { version = "0.1", features = ["with-serde_json-1"] } rand = "0.7" -refinery = {version= "0.2", features=["tokio-postgres"]} +refinery = { version= "0.2", features=["tokio-postgres"] } regex = "1.3" serde = { version = "1.0", features = ["derive"] } serde_json = "1" tokio = { version = "0.2", default_features = false, features = ["macros", "sync", "rt-core"] } toml = "0.5" twilight = { git = "https://github.com/AEnterprise/twilight/", branch="gearbot" } -tokio-postgres = "0.5" +tokio-postgres = { version = "0.5", default_features = false } url = "2.1" [profile.dev] diff --git a/src/commands/basic/coinflip.rs b/src/commands/basic/coinflip.rs index c1094406..a64502d5 100644 --- a/src/commands/basic/coinflip.rs +++ b/src/commands/basic/coinflip.rs @@ -6,9 +6,9 @@ use twilight::model::channel::Message; use crate::commands::meta::nodes::CommandResult; use crate::core::Context; use crate::parser::Parser; +use crate::utils; pub async fn coinflip(ctx: Arc, msg: Message, parser: Parser) -> CommandResult { - // TODO: This needs sanatized with the clean function. let thing_todo: String = parser .parts .into_iter() @@ -17,7 +17,7 @@ pub async fn coinflip(ctx: Arc, msg: Message, parser: Parser) -> Comman .join(" "); let thing_todo = if !thing_todo.is_empty() { - thing_todo + utils::clean(&thing_todo, true, true, true, true) } else { ctx.http .create_message(msg.channel_id) diff --git a/src/commands/basic/echo.rs b/src/commands/basic/echo.rs index 9318f900..4272e34d 100644 --- a/src/commands/basic/echo.rs +++ b/src/commands/basic/echo.rs @@ -8,7 +8,6 @@ use crate::parser::Parser; use crate::utils; pub async fn echo(ctx: Arc, msg: Message, parser: Parser) -> CommandResult { - // TODO: Sanitize this let ec: Vec = parser.parts.into_iter().skip(1).collect(); let echo_contents = utils::clean(&ec.join(" "), true, true, true, true); ctx.http diff --git a/src/commands/meta/nodes.rs b/src/commands/meta/nodes.rs index 892a650f..c0177509 100644 --- a/src/commands/meta/nodes.rs +++ b/src/commands/meta/nodes.rs @@ -1,6 +1,5 @@ use std::collections::HashMap; use std::fmt; -use std::fmt::{Display, Formatter}; use std::future::Future; use std::pin::Pin; use std::sync::Arc; @@ -103,8 +102,8 @@ impl CommandNode { } } -impl Display for CommandNode { - fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { +impl fmt::Display for CommandNode { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { match self { CommandNode::CommandNodeInner { command } => write!(f, "{}", command.name), CommandNode::GroupNode { diff --git a/src/core/context.rs b/src/core/context.rs index 70fc8d7d..c68604fa 100644 --- a/src/core/context.rs +++ b/src/core/context.rs @@ -5,31 +5,26 @@ use aes_gcm::{ aead::{Aead, NewAead}, Aes256Gcm, }; - use chrono::{DateTime, Utc}; +use dashmap::mapref::one::Ref; +use dashmap::DashMap; +use deadpool_postgres::Pool; +use log::info; +use postgres_types::Type; use rand::{thread_rng, RngCore}; +use serde_json; use tokio::sync::RwLock; use twilight::cache::InMemoryCache; use twilight::gateway::Cluster; use twilight::http::Client as HttpClient; -use twilight::model::channel::Message; use twilight::model::{ - channel::message::MessageType, + channel::message::{Message, MessageType}, id::{ChannelId, GuildId, MessageId, UserId}, user::CurrentUser, }; use crate::utils::{Error, FetchError}; -use crate::{core::GuildConfig, EncryptionKey}; -use dashmap::mapref::one::Ref; -use dashmap::DashMap; -use deadpool_postgres::Pool; -use git_version::git_version; -use log::info; -use postgres_types::Type; -use serde_json; - -const GIT_VERSION: &str = git_version!(); +use crate::{core::GuildConfig, EncryptionKey, GIT_VERSION}; #[derive(Debug)] pub struct BotStats { @@ -100,8 +95,7 @@ pub struct LoadingState { to_load: u32, loaded: u32, } -// In the future, any database handles or anything that holds real state will need -// put behind a `RwLock`. + pub struct Context { pub cache: InMemoryCache, pub cluster: Cluster, diff --git a/src/core/gearbot.rs b/src/core/gearbot.rs index c5ec1d94..c14814e6 100644 --- a/src/core/gearbot.rs +++ b/src/core/gearbot.rs @@ -98,9 +98,6 @@ impl GearBot { config.__master_key, )); - // TODO: Look into splitting this into two streams: - // One for user messages, and the other for internal bot things - // context.cluster.command() gearbot_info!("The cluster is going online!"); let mut bot_events = context.cluster.events().await?; while let Some(event) = bot_events.next().await { diff --git a/src/core/handlers/general.rs b/src/core/handlers/general.rs index a28e49b5..4fa96659 100644 --- a/src/core/handlers/general.rs +++ b/src/core/handlers/general.rs @@ -2,8 +2,10 @@ use std::sync::Arc; use log::debug; use twilight::gateway::cluster::Event; -use twilight::model::gateway::payload::UpdateStatus; -use twilight::model::gateway::presence::{Activity, ActivityType, Status}; +use twilight::model::gateway::{ + payload::UpdateStatus, + presence::{Activity, ActivityType, Status}, +}; use crate::core::Context; use crate::utils::Error; diff --git a/src/core/handlers/modlog.rs b/src/core/handlers/modlog.rs index 2510944a..23952254 100644 --- a/src/core/handlers/modlog.rs +++ b/src/core/handlers/modlog.rs @@ -32,7 +32,6 @@ pub async fn handle_event(shard_id: u64, event: &Event, ctx: Arc) -> Re // According to the docs, cache commands can never error, but just to be safe and // not spam unwraps everywhere, wrap it. let old_member = ctx.cache.member(update.guild_id, update.user.id).await?; - // TODO: Figure out why this is always `None`. let old_member = match old_member { Some(om) => om, diff --git a/src/main.rs b/src/main.rs index 9ce909a6..730c4742 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,29 +1,26 @@ use std::str::FromStr; +use aes_gcm::aead::generic_array::{typenum::U32, GenericArray}; use deadpool_postgres::{Manager, Pool}; +use git_version::git_version; use log::{debug, info}; use tokio_postgres::{Config, NoTls}; -use twilight::http::Client as HttpClient; - -use git_version::git_version; -use utils::Error; - -use aes_gcm::aead::generic_array::{typenum::U32, GenericArray}; +use twilight::http::{ + request::channel::message::allowed_mentions::AllowedMentionsBuilder, Client as HttpClient, +}; -use crate::core::logging; -use crate::core::BotConfig; -use crate::core::GearBot; +use crate::core::{logging, BotConfig, GearBot}; use crate::database::migrations::embedded; -use twilight::http::request::channel::message::allowed_mentions::AllowedMentionsBuilder; mod commands; mod core; mod database; mod parser; mod utils; +use utils::Error; -pub static VERSION: &str = env!("CARGO_PKG_VERSION"); -pub static GIT_VERSION: &str = git_version!(); +pub const VERSION: &str = env!("CARGO_PKG_VERSION"); +pub const GIT_VERSION: &str = git_version!(); pub type CommandResult = Result<(), Error>; diff --git a/src/utils/matchers.rs b/src/utils/matchers.rs index 0d00bb5a..ed3102b4 100644 --- a/src/utils/matchers.rs +++ b/src/utils/matchers.rs @@ -1,8 +1,7 @@ +use lazy_static::lazy_static; use regex::{Match, Regex, RegexBuilder}; use url::{Host, Url}; -use lazy_static::lazy_static; - const KNOWN_INVITE_DOMAINS: [&str; 6] = [ "discordapp.com", "discord.com", diff --git a/src/utils/mod.rs b/src/utils/mod.rs index d791a349..a951d756 100644 --- a/src/utils/mod.rs +++ b/src/utils/mod.rs @@ -1,13 +1,13 @@ -pub use emoji::*; -pub use errors::*; - -// TODO: Remove this when they are all used. +// Remove this when they are all used. #[allow(dead_code)] pub mod matchers; pub mod emoji; mod errors; +pub use emoji::*; +pub use errors::*; + const MARKDOWN_REPALCEMENTS: &[&str; 7] = &["\\", "*", "_", "~", "|", "{", ">"]; fn replace_markdown(msg: &mut String) {