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

Database#10

Open
kerrhau wants to merge 30 commits into
masterfrom
database
Open

Database#10
kerrhau wants to merge 30 commits into
masterfrom
database

Conversation

@kerrhau

@kerrhau kerrhau commented Jul 3, 2017

Copy link
Copy Markdown
Member

No description provided.

Comment thread src/commands/main.rs Outdated
}
}
None => {
ctx.set_presence(None, OnlineStatus::Online, false);

Copy link
Copy Markdown
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.

Comment thread 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");

Copy link
Copy Markdown
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?

Comment thread 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()));

Copy link
Copy Markdown
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.

Comment thread src/commands/markov.rs Outdated
None => {
let _ = message.channel_id.say(ERROR_MESSAGE);
}
}

Copy link
Copy Markdown
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.

Comment thread src/commands/markov.rs Outdated

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

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Ditto.

Comment thread src/markov.rs Outdated

None => {
// Message is empty
println!("Empty message");

Copy link
Copy Markdown
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
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

👍

Comment thread src/markov.rs Outdated
pub fn generate_from_word(&self,
length: u32,
starting_word: Option<&String>)
-> Option<String> {

Copy link
Copy Markdown
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
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

yes

Comment thread src/message.rs
pub content: String,
pub guild_id: i64,
pub author_id: i64,
pub channel_id: i64,

Copy link
Copy Markdown
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
Copy Markdown
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

@emmiegit emmiegit Jul 4, 2017

Copy link
Copy Markdown
Collaborator

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
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

👍

Comment thread src/markov.rs Outdated
starting_word: Option<&String>)
-> Option<String> {
let mut rng = thread_rng();
let mut word: &String;

Copy link
Copy Markdown
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
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

👍

Comment thread src/markov.rs
break;
}
}
}

Copy link
Copy Markdown
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
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

yes

@emmiegit emmiegit left a comment

Copy link
Copy Markdown
Collaborator

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.

Comment thread src/commands/main.rs
command!(help(ctx, msg, args) {
lazy_static! {
static ref ALLOWED_USER_IDS: Vec<u64> = vec![76043245804589056, 98633956773093376];
}

Copy link
Copy Markdown
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.

Comment thread 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);

Copy link
Copy Markdown
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.

Comment thread src/commands/main.rs
} else {
let _ = message.guild_id().unwrap().edit_nickname(Some(arg.as_str()));
}
}

Copy link
Copy Markdown
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>?

Comment thread src/commands/markov.rs Outdated
}
},
None => {
length += DEFAULT_GENERATION_LENGTH;

Copy link
Copy Markdown
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...

Comment thread 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;

Copy link
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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?

Comment thread 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
Copy Markdown
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?

Comment thread src/main.rs
}
}
let message_id = msg.id.0;
let message_content: String = String::from(msg.content_safe());

Copy link
Copy Markdown
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.

Comment thread src/main.rs
}
}
let message_id = msg.id.0;
let message_content: String = String::from(msg.content_safe());

Copy link
Copy Markdown
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?

Comment thread src/main.rs Outdated
channel_id);

let mut data = ctx.data.lock().unwrap();
let mut new = ctx.data.lock().unwrap();

Copy link
Copy Markdown
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?

Comment thread src/main.rs Outdated
markov.parse(&stripped);
usermap
.entry(author_id)
.or_insert(Markov::new())

Copy link
Copy Markdown
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.

Comment thread src/commands/markov.rs
.map_or(DEFAULT_GENERATION_LENGTH,
|x| x.parse::<u32>()
.ok()
.unwrap_or(DEFAULT_GENERATION_LENGTH));

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

👍

Comment thread 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
Copy Markdown
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?

Comment thread src/main.rs
.entry(author_id)
.or_insert(Markov::new())
.parse(&stripped);
}

Copy link
Copy Markdown
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

Comment thread 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
Copy Markdown
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