forked from sorceress/rustcord
Move hash function into utils module, replace .to_string() calls with s!() macro
This commit is contained in:
parent
034da120ac
commit
eb77c2ca37
52
src/main.rs
52
src/main.rs
|
@ -23,10 +23,12 @@ use serenity::{
|
|||
},
|
||||
prelude::*,
|
||||
};
|
||||
use std::collections::hash_map::DefaultHasher;
|
||||
use std::hash::{Hash, Hasher};
|
||||
use std::{env, process, sync::Arc};
|
||||
|
||||
#[macro_use]
|
||||
mod utils;
|
||||
use utils::*;
|
||||
|
||||
struct Handler;
|
||||
|
||||
struct ShardManagerContainer;
|
||||
|
@ -74,12 +76,6 @@ lazy_static! {
|
|||
vec![UserId(254310746450690048), UserId(687740609703706630)];
|
||||
}
|
||||
|
||||
// Calculates hash of a type that implements Hash
|
||||
fn calculate_hash<T: Hash>(t: &T) -> u64 {
|
||||
let mut s = DefaultHasher::new();
|
||||
t.hash(&mut s);
|
||||
s.finish()
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let mut client = Client::new(&env::var("DISCORD_TOKEN").expect("Invalid token"), Handler)
|
||||
|
@ -111,7 +107,7 @@ fn main() {
|
|||
"{}",
|
||||
format!(
|
||||
"Rate limited in {} with message {}",
|
||||
msg.channel_id.to_string().purple().bold(),
|
||||
s!(msg.channel_id).purple().bold(),
|
||||
msg.content.purple()
|
||||
)
|
||||
);
|
||||
|
@ -181,7 +177,7 @@ fn ping(ctx: &mut Context, message: &Message) -> CommandResult {
|
|||
Some(v) => v,
|
||||
None => {
|
||||
return Err(CommandError(
|
||||
"There was a problem getting the shard manager!".to_string(),
|
||||
s!("There was a problem getting the shard manager!"),
|
||||
))
|
||||
}
|
||||
};
|
||||
|
@ -191,12 +187,12 @@ fn ping(ctx: &mut Context, message: &Message) -> CommandResult {
|
|||
|
||||
let runner = match runners.get(&ShardId(ctx.shard_id)) {
|
||||
Some(v) => v,
|
||||
None => return Err(CommandError("No shard found!".to_string())),
|
||||
None => return Err(CommandError(s!("No shard found!"))),
|
||||
};
|
||||
|
||||
let ping = match runner.latency {
|
||||
Some(v) => v.as_millis(),
|
||||
None => return Err(CommandError("Could not get latency!".to_string())),
|
||||
None => return Err(CommandError(s!("Could not get latency!"))),
|
||||
};
|
||||
|
||||
let _ = message
|
||||
|
@ -211,7 +207,7 @@ fn ping(ctx: &mut Context, message: &Message) -> CommandResult {
|
|||
fn echo(ctx: &mut Context, message: &Message, args: Args) -> CommandResult {
|
||||
let input: String = args.rest().trim().to_string();
|
||||
if args.is_empty() {
|
||||
return Err(CommandError("Called without input".to_string()));
|
||||
return Err(CommandError(s!("Called without input")));
|
||||
}
|
||||
|
||||
let _ = message.channel_id.say(&ctx.http, input);
|
||||
|
@ -239,7 +235,7 @@ fn status(ctx: &mut Context, message: &Message, mut args: Args) -> CommandResult
|
|||
use serenity::model::gateway::Activity;
|
||||
|
||||
if args.is_empty() {
|
||||
return Err(CommandError("Called without args!".to_string()));
|
||||
return Err(CommandError(s!("Called without args!")));
|
||||
}
|
||||
|
||||
let mut input = args.single::<String>()?;
|
||||
|
@ -294,7 +290,7 @@ fn host(ctx: &mut Context, message: &Message) -> CommandResult {
|
|||
host = sys_info::hostname()?,
|
||||
release = sys_info::linux_os_release()?
|
||||
.pretty_name
|
||||
.unwrap_or_else(|| "Unknown".to_string()),
|
||||
.unwrap_or_else(|| s!("Unknown")),
|
||||
cpu = sys_info::cpu_speed()?
|
||||
),
|
||||
);
|
||||
|
@ -323,7 +319,7 @@ fn embed(ctx: &mut Context, message: &Message, args: Args) -> CommandResult {
|
|||
}
|
||||
|
||||
// print documentation from src/embed-docs.txt
|
||||
if &args.rest().trim().to_string() == "help" {
|
||||
if s!(&args.rest().trim()) == "help" {
|
||||
let mut file = fs::File::open("./src/embed-docs.txt")?;
|
||||
let mut help_string = String::new();
|
||||
file.read_to_string(&mut help_string)?;
|
||||
|
@ -470,7 +466,7 @@ fn ship(ctx: &mut Context, message: &Message, args: Args) -> CommandResult {
|
|||
|
||||
// Concatenate names together
|
||||
let shipname: Result<String, String> = match names.len() {
|
||||
0 => Err("Invalid input!".to_string()),
|
||||
0 => Err(s!("Invalid input!")),
|
||||
1 => Ok(names[0].clone()),
|
||||
_ => {
|
||||
let mut first_halves = String::new();
|
||||
|
@ -510,7 +506,7 @@ fn headpat(ctx: &mut Context, message: &Message, args: Args) -> CommandResult {
|
|||
let args = args.rest().trim();
|
||||
|
||||
if args.is_empty() {
|
||||
return Err(CommandError("Please specify a username!".to_string()));
|
||||
return Err(CommandError(s!("Please specify a username!")));
|
||||
}
|
||||
|
||||
// Get username from first mention, otherwise use input text
|
||||
|
@ -643,7 +639,7 @@ fn help(ctx: &mut Context, message: &Message) -> CommandResult {
|
|||
#[command]
|
||||
fn info(ctx: &mut Context, message: &Message, args: Args) -> CommandResult {
|
||||
if !args.is_empty() {
|
||||
return Err(CommandError("Called with args!".to_string()));
|
||||
return Err(CommandError(s!("Called with args!")));
|
||||
}
|
||||
|
||||
let num = ctx.cache.read().guilds.len();
|
||||
|
@ -673,7 +669,7 @@ fn define(ctx: &mut Context, message: &Message, args: Args) -> CommandResult {
|
|||
if !args.is_empty() {
|
||||
match defs {
|
||||
Err(_e) => {
|
||||
return Err(CommandError("Invalid query >w<".to_string()));
|
||||
return Err(CommandError(s!("Invalid query >w<")));
|
||||
}
|
||||
Ok(v) => {
|
||||
if !v.is_empty() {
|
||||
|
@ -690,7 +686,7 @@ fn define(ctx: &mut Context, message: &Message, args: Args) -> CommandResult {
|
|||
})
|
||||
});
|
||||
} else {
|
||||
return Err(CommandError("No results!".to_string()));
|
||||
return Err(CommandError(s!("No results!")));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -709,7 +705,7 @@ fn pfp(ctx: &mut Context, message: &Message) -> CommandResult {
|
|||
|
||||
let pfp = match user.avatar_url() {
|
||||
Some(v) => v,
|
||||
None => return Err(CommandError("The user does not have an avatar".to_string())),
|
||||
None => return Err(CommandError(s!("The user does not have an avatar"))),
|
||||
};
|
||||
|
||||
let _ = message.channel_id.send_message(&ctx.http, |m| {
|
||||
|
@ -741,13 +737,13 @@ fn desc(ctx: &mut Context, message: &Message) -> CommandResult {
|
|||
let channel_lock = match message.channel(&ctx) {
|
||||
Some(ch) => ch,
|
||||
None => {
|
||||
return Err(CommandError("Could not get channel!".to_string()));
|
||||
return Err(CommandError(s!("Could not get channel!")));
|
||||
}
|
||||
};
|
||||
let channel_lock = match channel_lock.guild() {
|
||||
Some(g) => g,
|
||||
None => {
|
||||
return Err(CommandError("Could not get guild!".to_string()));
|
||||
return Err(CommandError(s!("Could not get guild!")));
|
||||
}
|
||||
};
|
||||
let channel = channel_lock.read();
|
||||
|
@ -786,15 +782,15 @@ fn pinned(ctx: &mut Context, message: &Message, mut args: Args) -> CommandResult
|
|||
Ok(v) => v,
|
||||
Err(e) => {
|
||||
return Err(CommandError(
|
||||
format!("Could not get pinned messages! Error: {}", e).to_string(),
|
||||
s!(format!("Could not get pinned messages! Error: {}", e)),
|
||||
));
|
||||
}
|
||||
};
|
||||
if pinned.is_empty() {
|
||||
return Err(CommandError("No pinned messages found!".to_string()));
|
||||
return Err(CommandError(s!("No pinned messages found!")));
|
||||
}
|
||||
if idx > pinned.len() - 1 {
|
||||
return Err(CommandError("Index out of bounds!".to_string()));
|
||||
return Err(CommandError(s!("Index out of bounds!")));
|
||||
}
|
||||
|
||||
let _ = message.channel_id.send_message(&ctx.http, |m| {
|
||||
|
@ -826,7 +822,7 @@ fn brainfuck(ctx: &mut Context, message: &Message, args: Args) -> CommandResult
|
|||
|
||||
let input = match args.rest().trim() {
|
||||
"" => {
|
||||
return Err(CommandError("Called without input!".to_string()));
|
||||
return Err(CommandError(s!("Called without input!")));
|
||||
}
|
||||
v @ _ => v,
|
||||
};
|
||||
|
|
|
@ -0,0 +1,10 @@
|
|||
use std::{collections::hash_map::DefaultHasher, hash::{Hasher, Hash}};
|
||||
|
||||
macro_rules!s( ( $e:expr ) => ( ($e).to_string() ) );
|
||||
|
||||
// Calculates hash of a type that implements Hash
|
||||
pub fn calculate_hash<T: Hash>(t: &T) -> u64 {
|
||||
let mut s = DefaultHasher::new();
|
||||
t.hash(&mut s);
|
||||
s.finish()
|
||||
}
|
Loading…
Reference in New Issue