-
Notifications
You must be signed in to change notification settings - Fork 0
Database #10
base: master
Are you sure you want to change the base?
Conversation
Created a new generate_from_word function
src/commands/main.rs
Outdated
} | ||
} | ||
None => { | ||
ctx.set_presence(None, OnlineStatus::Online, false); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Lots of repeated code here. Change this to a function that returns an OnlineStatus
, and then only call ctx.set_presence()
once based on that.
src/commands/main.rs
Outdated
command!(name(ctx, message, args) { | ||
if ALLOWED_USER_IDS.contains(&message.author.id.0) { | ||
let arg = args.join(" "); | ||
let _ = ctx.edit_profile(|p| p.username(arg.as_str())).expect("could not set name"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
.expect()
will panic, killing the program. Is this really what you want if you can't set your name?
src/commands/main.rs
Outdated
let _ = ctx; | ||
if ALLOWED_USER_IDS.contains(&message.author.id.0) { | ||
let arg = args.join(" "); | ||
let _ = message.guild_id().unwrap().edit_nickname(Some(arg.as_str())); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
message
will only have a guild_id()
if it is from a guild. However, you perform no checks to make sure this is the case. If somebody sent this bot a DM with this command, it would crash.
src/commands/markov.rs
Outdated
None => { | ||
let _ = message.channel_id.say(ERROR_MESSAGE); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This match
should return a &str
, and that should be fed into .say()
Otherwise you're duplicating code.
src/commands/markov.rs
Outdated
|
||
None => { | ||
let _ = message.channel_id.say(ERROR_MESSAGE); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ditto.
src/markov.rs
Outdated
None => { | ||
// Message is empty | ||
println!("Empty message"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of println!()
this should use some sort of proper logging.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
src/markov.rs
Outdated
pub fn generate_from_word(&self, | ||
length: u32, | ||
starting_word: Option<&String>) | ||
-> Option<String> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
wtf is this what rustfmt
gave you?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes
pub content: String, | ||
pub guild_id: i64, | ||
pub author_id: i64, | ||
pub channel_id: i64, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Discord IDs are always u64
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
postgres doesn't support u64
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Then make the internal API support u64
. Have only the code that directly glues the database to the rest of the program use i64
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
src/markov.rs
Outdated
starting_word: Option<&String>) | ||
-> Option<String> { | ||
let mut rng = thread_rng(); | ||
let mut word: &String; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
wtf &String
Just do &str
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
src/markov.rs
Outdated
break; | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is just moved from the original generate
, right? No changes?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Some new issues. There are a fair number of things you haven't fixed from my first review.
command!(help(ctx, msg, args) { | ||
lazy_static! { | ||
static ref ALLOWED_USER_IDS: Vec<u64> = vec![76043245804589056, 98633956773093376]; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll let it slide for this PR, but you should really make a proper configuration file.
src/commands/main.rs
Outdated
|
||
command!(status(ctx, message, args) { | ||
if ALLOWED_USER_IDS.contains(&message.author.id.0) { | ||
ctx.set_presence(None, Status::from_str(args.get(0).unwrap()).unwrap_or(OnlineStatus::Online), false); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Clean this line up. Like the part where you get the OnlineStatus
should be its own line.
} else { | ||
let _ = message.guild_id().unwrap().edit_nickname(Some(arg.as_str())); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- Why do all of these only work only apply to "allowed users"?
- If you're constantly doing membership checks, why not put them in a
HashSet<T>
?
src/commands/markov.rs
Outdated
} | ||
}, | ||
None => { | ||
length += DEFAULT_GENERATION_LENGTH; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It still isn't fixed...
src/commands/markov.rs
Outdated
command!(generate_from_word(ctx, message, args) { | ||
let mut data = ctx.data.lock().unwrap(); | ||
let mut markov = data.get_mut::<Markov>().unwrap(); | ||
let mut word: Option<&str> = None; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't see why word
needs to be mutable.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it's written to later on
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No I mean like, why not set it's value either within a function or within a {}
scope
and then set it to a non-mut
binding then?
src/main.rs
Outdated
markov::parse_messages(&connection.lock().unwrap(), | ||
data.get_mut::<Markov>().unwrap()); | ||
markov::parse_user_messages(&connection.lock().unwrap(), | ||
data.get_mut::<UserMap>().unwrap()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is going to take forever. Why can't you async this?
src/main.rs
Outdated
} | ||
} | ||
let message_id = msg.id.0; | ||
let message_content: String = String::from(msg.content_safe()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Type annotation unneeded here.
src/main.rs
Outdated
} | ||
} | ||
let message_id = msg.id.0; | ||
let message_content: String = String::from(msg.content_safe()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also you call .content_safe()
twice. Why not just reuse stripped
?
src/main.rs
Outdated
channel_id); | ||
|
||
let mut data = ctx.data.lock().unwrap(); | ||
let mut new = ctx.data.lock().unwrap(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
uhh...
To ask the obvious, why is this being called twice?
src/main.rs
Outdated
markov.parse(&stripped); | ||
usermap | ||
.entry(author_id) | ||
.or_insert(Markov::new()) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use a callback function to avoid creating unnecessary Markov
s every time.
src/commands/markov.rs
Outdated
.map_or(DEFAULT_GENERATION_LENGTH, | ||
|x| x.parse::<u32>() | ||
.ok() | ||
.unwrap_or(DEFAULT_GENERATION_LENGTH)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
src/main.rs
Outdated
let locked_data1 = client.data.clone(); | ||
let locked_data2 = client.data.clone(); | ||
let locked_connection1 = connection.clone(); | ||
let locked_connection2 = connection.clone(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why do you need two copies of data
and connection
?
src/main.rs
Outdated
.entry(author_id) | ||
.or_insert(Markov::new()) | ||
.parse(&stripped); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍 for using local { }
scopes
src/main.rs
Outdated
let mut usermap = data.get_mut::<UserMap>().expect("UserMap does not exist"); | ||
usermap | ||
.entry(author_id) | ||
.or_insert(Markov::new()) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use a callback to avoid creating unnecessary Markov
instances.
No description provided.