-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathlib.rs
More file actions
112 lines (92 loc) · 3.07 KB
/
lib.rs
File metadata and controls
112 lines (92 loc) · 3.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
//! This is the library for the application. The majority of the logic can be found here
//! It is split into two main parts. The parts that receive commands from discord [`commands`] and
//! the part that handles the actual logic of what to do in the [`model`]
#![warn(unused_crate_dependencies)]
mod used_in_bin {
use loadenv as _;
use tracing_subscriber as _;
}
use secrecy::SecretString;
use clap::Parser;
use tracing::{info, instrument};
pub use self::{
commands::commands_list,
config::{SharedConfig, StartupConfig},
model::Data,
};
mod commands;
mod config;
mod db;
pub mod heartbeat;
mod model;
/// Type used by poise framework as the context when commands are triggered
type Context<'a> = poise::Context<'a, Data, anyhow::Error>;
trait RemoveElement<T: PartialEq> {
/// Returns true iff the element was found and removed
fn remove_element(&mut self, element: &T) -> bool;
}
impl<T: PartialEq> RemoveElement<T> for Vec<T> {
fn remove_element(&mut self, element: &T) -> bool {
let index = self
.iter()
.enumerate()
.find_map(|(i, x)| if x == element { Some(i) } else { None });
if let Some(i) = index {
self.remove(i);
true
} else {
false
}
}
}
trait AuthorPreferredDisplay {
async fn author_preferred_display(&self) -> String;
}
impl AuthorPreferredDisplay for Context<'_> {
async fn author_preferred_display(&self) -> String {
match self.author_member().await {
Some(member) => member.display_name().to_string(),
None => self.author().name.clone(),
}
}
}
trait Resettable: Default {
fn reset(&mut self) {
*self = Default::default();
}
}
/// Removes identified problems with inputs
/// Not trying to remove all markdown just the parts that are
/// likely to cause issues. More will be added as needed
#[must_use]
#[instrument]
fn sanitize_markdown(s: String) -> String {
const PATTERNS: [&str; 4] = ["**", "__", "```", "\n"];
let mut result = s;
for pattern in PATTERNS.iter() {
result = result.replace(pattern, "");
}
info!(result);
result
}
#[derive(Parser, Debug, Clone)]
#[clap(author, version, about, long_about = None)]
pub struct ClapConfig {
#[arg(long, env = "DISCORD_TOKEN")]
pub discord_token: SecretString,
/// Used mostly for testing to register the commands directly for the guild
#[arg(long, env = "REGISTRATION_GUILD_ID")]
pub registration_guild_id: Option<String>,
/// The RoleId of the role that can run privileged commands
#[arg(long, env = "AUTH_ROLE_ID")]
pub auth_role_id: String,
/// Comma separated list of owner IDs
#[arg(long, env = "OWNERS")]
pub owners: String,
/// The channel to be used for unranked (Indented to be used to restrict messages for unranked to that channel)
#[arg(long, env = "CHANNEL_UNRANKED_ID")]
pub channel_unranked_id: String,
/// For bot status messages like on connection
#[arg(long, env = "CHANNEL_BOT_STATUS_ID")]
pub channel_bot_status_id: String,
}