]> ruin.nu Git - NDIRC.git/blobdiff - Commands/Usermgm.pm
Add setdiscordid
[NDIRC.git] / Commands / Usermgm.pm
index 2b6176041287019a27c1c5af76e5a191612ba0bf..8dda53aaac5b3ac29c475a967a32b7ec397bf2f9 100644 (file)
@@ -417,6 +417,42 @@ SELECT username,hostmask,pnick FROM users WHERE hostmask = $1 OR pnick = $2
        }
 };
 
+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;
+       }
+};
+
 my $points = class extends NDIRC::Command {
        has point => (
                is => 'ro',