Skip to content
This repository has been archived by the owner on Nov 16, 2021. It is now read-only.

Commit

Permalink
sanity checking for aggregation features
Browse files Browse the repository at this point in the history
  • Loading branch information
kawaii committed Jun 27, 2019
1 parent a800378 commit fad8dde
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
21 changes: 20 additions & 1 deletion lib/Rose/Controller/Commands.pm6
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ submethod TWEAK () {
}

method despatch($str, :$payload) {
CATCH {
when X::Command::Despatch::InvalidCommand {
return "Invalid command."
}
}

$.commands.run($str, :$payload);
}

Expand All @@ -26,6 +32,19 @@ method toxicity-aggregate($cmd-obj) {
$user-id = $/;
}

unless $user-id ~~ / <(\d+)> / { return my $response = "Invalid user."; }

my $result = $!db.toxicity-aggregate(:user($user-id), :guild($cmd-obj.payload<guild-id>));
my $response = "<@$user-id> aggregate threat level $result.";

if $result == 0 {
my $response = "Invalid user.";
} else {
my $risk = do given $result {
when .0 < * <= .4 { $risk = "**LOW**" }
when .4 < * <= .7 { $risk = "**MEDIUM**" }
when .7 < * { $risk = "**HIGH**" }
}

my $response = "<@$user-id> aggregate threat level $result.round(0.01).\nRisk level classification: $risk.";
}
}
2 changes: 1 addition & 1 deletion lib/Rose/Persistence/PostgreSQL.pm6
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,5 @@ method toxicity-aggregate(:$guild, :$user) {
my $query = $!dbi.db.prepare('SELECT AVG("perspective-score") FROM rose_messages WHERE "guild-id" = $1 AND "user-id" = $2 LIMIT 200;');
my $result = $query.execute($guild, $user);
$query.finish;
return $result.value;
return $result.value // 0;
}

0 comments on commit fad8dde

Please sign in to comment.