Skip to content
This repository was archived by the owner on Oct 11, 2018. It is now read-only.

Database #10

Open
wants to merge 30 commits into
base: master
Choose a base branch
from
Open

Database #10

wants to merge 30 commits into from

Conversation

kerrhau
Copy link
Member

@kerrhau kerrhau commented Jul 3, 2017

No description provided.

}
}
None => {
ctx.set_presence(None, OnlineStatus::Online, false);
Copy link
Collaborator

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.

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");
Copy link
Collaborator

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?

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()));
Copy link
Collaborator

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.

None => {
let _ = message.channel_id.say(ERROR_MESSAGE);
}
}
Copy link
Collaborator

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.


None => {
let _ = message.channel_id.say(ERROR_MESSAGE);
}
Copy link
Collaborator

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");
Copy link
Collaborator

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.

Copy link
Member Author

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> {
Copy link
Collaborator

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?

Copy link
Member Author

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,
Copy link
Collaborator

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

Copy link
Member Author

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

Copy link
Collaborator

@emmiegit emmiegit Jul 4, 2017

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.

Copy link
Member Author

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;
Copy link
Collaborator

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

Copy link
Member Author

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;
}
}
}
Copy link
Collaborator

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?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes

Copy link
Collaborator

@emmiegit emmiegit left a 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];
}
Copy link
Collaborator

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.


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);
Copy link
Collaborator

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()));
}
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. Why do all of these only work only apply to "allowed users"?
  2. If you're constantly doing membership checks, why not put them in a HashSet<T>?

}
},
None => {
length += DEFAULT_GENERATION_LENGTH;
Copy link
Collaborator

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...

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;
Copy link
Collaborator

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.

Copy link
Member Author

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

Copy link
Collaborator

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());
Copy link
Collaborator

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());
Copy link
Collaborator

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());
Copy link
Collaborator

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();
Copy link
Collaborator

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())
Copy link
Collaborator

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 Markovs every time.

.map_or(DEFAULT_GENERATION_LENGTH,
|x| x.parse::<u32>()
.ok()
.unwrap_or(DEFAULT_GENERATION_LENGTH));
Copy link
Collaborator

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();
Copy link
Collaborator

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);
}
Copy link
Collaborator

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())
Copy link
Collaborator

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.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants