Conversation
Created a new generate_from_word function
| } | ||
| } | ||
| None => { | ||
| ctx.set_presence(None, OnlineStatus::Online, false); |
There was a problem hiding this comment.
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.
| 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.
.expect() will panic, killing the program. Is this really what you want if you can't set your name?
| 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.
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.
| None => { | ||
| let _ = message.channel_id.say(ERROR_MESSAGE); | ||
| } | ||
| } |
There was a problem hiding this comment.
This match should return a &str, and that should be fed into .say()
Otherwise you're duplicating code.
|
|
||
| None => { | ||
| let _ = message.channel_id.say(ERROR_MESSAGE); | ||
| } |
|
|
||
| None => { | ||
| // Message is empty | ||
| println!("Empty message"); |
There was a problem hiding this comment.
Instead of println!() this should use some sort of proper logging.
| pub fn generate_from_word(&self, | ||
| length: u32, | ||
| starting_word: Option<&String>) | ||
| -> Option<String> { |
There was a problem hiding this comment.
wtf is this what rustfmt gave you?
| pub content: String, | ||
| pub guild_id: i64, | ||
| pub author_id: i64, | ||
| pub channel_id: i64, |
There was a problem hiding this comment.
Discord IDs are always u64
There was a problem hiding this comment.
postgres doesn't support u64
There was a problem hiding this comment.
Then make the internal API support u64. Have only the code that directly glues the database to the rest of the program use i64.
| starting_word: Option<&String>) | ||
| -> Option<String> { | ||
| let mut rng = thread_rng(); | ||
| let mut word: &String; |
| break; | ||
| } | ||
| } | ||
| } |
There was a problem hiding this comment.
This is just moved from the original generate, right? No changes?
emmiegit
left a comment
There was a problem hiding this comment.
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.
I'll let it slide for this PR, but you should really make a proper configuration file.
|
|
||
| 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.
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.
- 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>?
| } | ||
| }, | ||
| None => { | ||
| length += DEFAULT_GENERATION_LENGTH; |
| 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.
I don't see why word needs to be mutable.
There was a problem hiding this comment.
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?
| 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.
This is going to take forever. Why can't you async this?
| } | ||
| } | ||
| let message_id = msg.id.0; | ||
| let message_content: String = String::from(msg.content_safe()); |
There was a problem hiding this comment.
Type annotation unneeded here.
| } | ||
| } | ||
| let message_id = msg.id.0; | ||
| let message_content: String = String::from(msg.content_safe()); |
There was a problem hiding this comment.
Also you call .content_safe() twice. Why not just reuse stripped?
| channel_id); | ||
|
|
||
| let mut data = ctx.data.lock().unwrap(); | ||
| let mut new = ctx.data.lock().unwrap(); |
There was a problem hiding this comment.
uhh...
To ask the obvious, why is this being called twice?
| markov.parse(&stripped); | ||
| usermap | ||
| .entry(author_id) | ||
| .or_insert(Markov::new()) |
There was a problem hiding this comment.
Use a callback function to avoid creating unnecessary Markovs every time.
| .map_or(DEFAULT_GENERATION_LENGTH, | ||
| |x| x.parse::<u32>() | ||
| .ok() | ||
| .unwrap_or(DEFAULT_GENERATION_LENGTH)); |
| 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.
Why do you need two copies of data and connection?
| .entry(author_id) | ||
| .or_insert(Markov::new()) | ||
| .parse(&stripped); | ||
| } |
There was a problem hiding this comment.
👍 for using local { } scopes
| 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.
Use a callback to avoid creating unnecessary Markov instances.
No description provided.