+command setdiscordid => {
+ help => q(Usage: .setdiscordid username discordid | Changes a user's discord id/tag Nick#id),
+ acl => q(bot_setdiscordid)
+}, class extends NDIRC::Command {
+ method execute ($c,$msg) {
+ my ($nick,$discordid) = $msg =~ /^(\S+) (\S+)$/ or die 'ARGS';
+ my $dbh = $c->model;
+
+ my $f = $dbh->prepare(q{SELECT uid,username FROM users WHERE username ILIKE ?});
+ $f->execute($nick);
+ my $user = $f->fetchrow_hashref;
+ if ($f->rows == 1){
+ eval{
+ $dbh->do(q{UPDATE users SET discord_id = NULLIF($1, 'NULL') WHERE uid = $2}
+ ,undef,$discordid,$user->{uid});
+ $c->reply("Updated <b>$user->{username}</b>'s discord id to: <b>$discordid</b>");
+ };
+ if($@){
+ if ($@ =~ /duplicate key value violates unique constraint/){
+ my ($username, $discordid) = $dbh->selectrow_array(q{
+SELECT username,discord_id FROM users WHERE discord_id = $1
+ },undef,$discordid);
+ $c->reply("<c04>Problem</c>, <b>$username</b> already uses discord id <b>$discordid</b>.");
+ }else{
+ die $@;
+ }
+ }
+ }elsif ($f->rows == 0){
+ $c->reply("No hit, maybe spelling mistake, or add % as wildcard");
+ }else{
+ $c->reply("More than 1 user matched, please refine the search");
+ }
+ $f->finish;
+ }
+};
+