|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace App\Console\Commands; |
| 4 | + |
| 5 | +use Illuminate\Console\Command; |
| 6 | +use LaravelRestcord\Discord; |
| 7 | +use RestCord\DiscordClient; |
| 8 | + |
| 9 | +class UpdateDiscordRoles extends Command |
| 10 | +{ |
| 11 | + /** |
| 12 | + * The name and signature of the console command. |
| 13 | + * |
| 14 | + * @var string |
| 15 | + */ |
| 16 | + protected $signature = 'discord:update'; |
| 17 | + |
| 18 | + /** |
| 19 | + * The console command description. |
| 20 | + * |
| 21 | + * @var string |
| 22 | + */ |
| 23 | + protected $description = 'Command description'; |
| 24 | + |
| 25 | + /** |
| 26 | + * Create a new command instance. |
| 27 | + * |
| 28 | + * @return void |
| 29 | + */ |
| 30 | + public function __construct(DiscordClient $discord) |
| 31 | + { |
| 32 | + $this->discord = $discord; |
| 33 | + parent::__construct(); |
| 34 | + } |
| 35 | + |
| 36 | + /** |
| 37 | + * Execute the console command. |
| 38 | + * |
| 39 | + * @return mixed |
| 40 | + */ |
| 41 | + public function handle() |
| 42 | + { |
| 43 | + $collective = file_get_contents("https://opencollective.com/radarr/members/users.json"); |
| 44 | + $donators = json_decode($collective, true); |
| 45 | + $d_users = []; |
| 46 | + $regex = "/^.*?\s(?P<name>.+?#\d{4}).*?$/"; |
| 47 | + foreach ($donators as $donator) |
| 48 | + { |
| 49 | + preg_match($regex, $donator["description"], $matches); |
| 50 | + if ($matches != null) |
| 51 | + { |
| 52 | + $d_users[$matches["name"]] = $donator["role"]; |
| 53 | + } |
| 54 | + else |
| 55 | + { |
| 56 | + $this->error("Could not find discord user name for backer: ". $donator["name"]); |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + $this->info("Found donators: ".(string)count($d_users)); |
| 61 | + |
| 62 | + $guilds = []; |
| 63 | + $members = $this->discord->guild->listGuildMembers(["guild.id" => 264387956343570434, "limit" => 1000]); |
| 64 | + $guilds = $members; |
| 65 | + while (count($members) == 1000) |
| 66 | + { |
| 67 | + $members = $this->discord->guild->listGuildMembers(["guild.id" => 264387956343570434, "limit" => 1000, "after" => $members[999]->user->id]); |
| 68 | + $guilds = array_merge($guilds, $members); |
| 69 | + } |
| 70 | + |
| 71 | + $g_roles = $this->discord->guild->getGuildRoles(["guild.id" => 264387956343570434]); |
| 72 | + |
| 73 | + foreach ($guilds as $user) { |
| 74 | + $username = $user->user->username . "#" . $user->user->discriminator; |
| 75 | + if (isset($d_users[$username])) { |
| 76 | + $type = $d_users[$username]; |
| 77 | + $roles = $user->roles; |
| 78 | + $role = $type == "BACKER" ? 425290590050058242 : 425290634350559232; |
| 79 | + if (!in_array($role, $roles)) { |
| 80 | + $roles[] = $role; |
| 81 | + |
| 82 | + $res = $this->discord->guild->modifyGuildMember(["guild.id" => 264387956343570434, "user.id" => $user->user->id, "roles" => $roles]); |
| 83 | + } |
| 84 | + |
| 85 | + unset($d_users[$username]); |
| 86 | + } else { |
| 87 | + //$this->error("No discord user found with: ".$username); |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + foreach ($d_users as $name=>$type) { |
| 92 | + $this->error("Couldn't find discord user: ".$name); |
| 93 | + } |
| 94 | + } |
| 95 | +} |
0 commit comments