Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion docs/windows-hosted-verifier.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,11 @@ older than 30 minutes cannot authorize even if the periodic callback is late.

Read-only mode does not register the legacy verifier's member/thread mutation
listeners, approve guild join requests or run verification-driven role/ban
timers. It does not synchronize Discord roles. Registration, token challenges,
timers. Portable instances cannot add, remove, or replace Discord member roles,
including indirect ban-check, game moderation, and faction command paths.
The official hosted CivilizationBot owns those writes. A local game moderation
action can still change local game state, but does not apply a Discord role;
use the hosted bot for Discord role changes. Registration, token challenges,
provisioning and deletion are rejected before local or remote writes. Direct
users to the primary hosted bot to complete or change verification. Other bot
commands retain their existing authorization checks; this mode grants none.
Expand Down
73 changes: 56 additions & 17 deletions src/Civ13/Civ13.php
Original file line number Diff line number Diff line change
Expand Up @@ -1098,6 +1098,32 @@ public function statusChanger(Activity $activity, string $state = 'online'): voi
{
$this->discord->updatePresence($activity, false, $state);
}
public const DISCORD_ROLE_AUTHORITY_MESSAGE = 'Discord roles are managed by the primary CivilizationBot on civ13.com.';

/** Only the hosted bot can write roles. Replica freshness never grants write authority. */
public function canWriteDiscordRoles(): bool
{
return $this->portable_state_dir === ''
&& ! ($this->options['verification_read_only'] ?? false)
&& ! (isset($this->verifier->readOnly) && $this->verifier->readOnly);
}

/** Skip Discord side effects on replicas while preserving read-only checks and game moderation. */
public function addDiscordRole(Member $member, mixed $role, ?string $reason = null): PromiseInterface
{
return $this->canWriteDiscordRoles() ? $member->addRole($role, $reason) : resolve($member);
}

public function removeDiscordRole(Member $member, mixed $role, ?string $reason = null): PromiseInterface
{
return $this->canWriteDiscordRoles() ? $member->removeRole($role, $reason) : resolve($member);
}

public function replaceDiscordRoles(Member $member, mixed $roles, ?string $reason = null): PromiseInterface
{
return $this->canWriteDiscordRoles() ? $member->setRoles($roles, $reason) : resolve($member);
}

/**
* Removes specified roles from a member.
*
Expand All @@ -1111,17 +1137,21 @@ public function removeRoles(
Collection|array|Role|string|int $roles,
bool $patch = true
): PromiseInterface {
if (! $this->canWriteDiscordRoles()) {
return resolve($member);
}

if (! $role_ids = array_filter(self::__rolesToIdArray($roles), fn ($role_id) => $member->roles->has($role_id))) {
return resolve($member);
}

return $patch
? ((($new_roles = $member->roles->filter(static fn (Role $role) => ! in_array($role->id, $role_ids))) !== $member->roles)
? $member->setRoles($new_roles->toArray())
? $this->replaceDiscordRoles($member, $new_roles->toArray())
: resolve($member))
: array_reduce(
$role_ids,
fn ($promise, $role_id) => $promise->then(static fn () => $member->removeRole($role_id)),
fn ($promise, $role_id) => $promise->then(fn () => $this->removeDiscordRole($member, $role_id)),
resolve($member) // Each role is removed exactly once, in order
)
->then(static fn () => $member->guild->members->get('id', $member->id));
Expand All @@ -1139,12 +1169,17 @@ public function addRoles(
Collection|array|Role|string|int $roles,
bool $patch = true
): PromiseInterface {
if (! $this->canWriteDiscordRoles()) {
return resolve($member);
}

if (! $role_ids = array_filter(self::__rolesToIdArray($roles), fn ($role_id) => ! $member->roles->has($role_id))) {
return resolve($member);
}

return $patch
? $member->setRoles(
? $this->replaceDiscordRoles(
$member,
array_merge(
$member->roles
->map(fn ($role) => $role->id)
Expand All @@ -1154,7 +1189,7 @@ public function addRoles(
)
: array_reduce(
$role_ids,
fn ($promise, $role_id) => $promise->then(static fn () => $member->addRole($role_id)),
fn ($promise, $role_id) => $promise->then(fn () => $this->addDiscordRole($member, $role_id)),
resolve($member) // Each role is added exactly once, in order
)
->then(static fn () => $member->guild->members->get('id', $member->id));
Expand All @@ -1173,6 +1208,10 @@ public function setRoles(
Collection|array|Role|string|int $add_roles = [],
Collection|array|Role|string|int $remove_roles = []
): PromiseInterface {
if (! $this->canWriteDiscordRoles()) {
return resolve($member);
}

$add_roles = array_filter(self::__rolesToIdArray($add_roles), static fn ($role_id) => ! $member->roles->has($role_id));
$remove_roles = array_filter(self::__rolesToIdArray($remove_roles), static fn ($role_id) => $member->roles->has($role_id));
if (! $add_roles && ! $remove_roles) {
Expand All @@ -1183,7 +1222,7 @@ public function setRoles(
$current[] = (string) $role->id;
}

return $member->setRoles(array_values(array_unique(array_diff(array_merge($current, $add_roles), $remove_roles))));
return $this->replaceDiscordRoles($member, array_values(array_unique(array_diff(array_merge($current, $add_roles), $remove_roles))));
}
/**
* Convert roles to an array of role IDs.
Expand Down Expand Up @@ -1812,13 +1851,13 @@ private function __bancheckTimer(): bool
}
//$this->logger->debug("Checking bans for {$item['ss13']}...");
if (($banned = $this->bancheck($item['ss13'], true, false)) && ! ($member->roles->has($this->role_ids['Banished']) || $member->roles->has($this->role_ids['Permabanished']))) {
$member->addRole($this->role_ids['Banished'], 'bancheck timer');
$this->addDiscordRole($member, $this->role_ids['Banished'], 'bancheck timer');
if (isset($this->channel_ids['staff_bot']) && $channel = $this->discord->getChannel($this->channel_ids['staff_bot'])) {
$this->sendMessage($channel, "Added the banished role to $member.");
}
} elseif (! $banned && ($member->roles->has($this->role_ids['Banished']) || $member->roles->has($this->role_ids['Permabanished']))) {
$member->removeRole($this->role_ids['Banished'], 'bancheck timer');
$member->removeRole($this->role_ids['Permabanished'], 'bancheck timer');
$this->removeDiscordRole($member, $this->role_ids['Banished'], 'bancheck timer');
$this->removeDiscordRole($member, $this->role_ids['Permabanished'], 'bancheck timer');
if (isset($this->channel_ids['staff_bot']) && $channel = $this->discord->getChannel($this->channel_ids['staff_bot'])) {
$this->sendMessage($channel, "Removed the banished role from $member.");
}
Expand All @@ -1845,13 +1884,13 @@ public function bancheck(string $ckey, bool $bypass = false, bool $use_cache = f
return false;
}
$banned = array_reduce($this->enabled_gameservers, fn ($carry, $gameserver) => $carry || $gameserver->bancheck($ckey, false, $use_cache), false);
if ($bypass || ! isset($this->verifier) || ! $member = $this->verifier->getVerifiedMember($ckey)) {
if ($bypass || ! $this->canWriteDiscordRoles() || ! isset($this->verifier) || ! $member = $this->verifier->getVerifiedMember($ckey)) {
return $banned;
}
if ($banned && ! $member->roles->has($this->role_ids['Banished']) && ! $member->roles->has($this->role_ids['Admin'])) {
$member->addRole($this->role_ids['Banished'], "bancheck ($ckey)");
$this->addDiscordRole($member, $this->role_ids['Banished'], "bancheck ($ckey)");
} elseif (! $banned && $member->roles->has($this->role_ids['Banished'])) {
$member->removeRole($this->role_ids['Banished'], "bancheck ($ckey)");
$this->removeDiscordRole($member, $this->role_ids['Banished'], "bancheck ($ckey)");
}

return $banned;
Expand All @@ -1876,13 +1915,13 @@ public function permabancheck(string $ckey, bool $bypass = false): bool
return false;
}
$permabanned = array_reduce($this->enabled_gameservers, fn ($carry, $gameserver) => $carry || $gameserver->permabancheck($ckey), false);
if ($bypass || ! isset($this->verifier) || ! $member = $this->verifier->getVerifiedMember($ckey)) {
if ($bypass || ! $this->canWriteDiscordRoles() || ! isset($this->verifier) || ! $member = $this->verifier->getVerifiedMember($ckey)) {
return $permabanned;
}
if ($permabanned && ! $member->roles->has($this->role_ids['Permabanished']) && ! $member->roles->has($this->role_ids['Admin'])) {
$member->setRoles([$this->role_ids['Banished'], $this->role_ids['Permabanished']], "permabancheck ($ckey)");
$this->replaceDiscordRoles($member, [$this->role_ids['Banished'], $this->role_ids['Permabanished']], "permabancheck ($ckey)");
} elseif (! $permabanned && $member->roles->has($this->role_ids['Permabanished'])) {
$member->removeRole($this->role_ids['Permabanished'], "permabancheck ($ckey)");
$this->removeDiscordRole($member, $this->role_ids['Permabanished'], "permabancheck ($ckey)");
}

return $permabanned;
Expand Down Expand Up @@ -1963,12 +2002,12 @@ public function ban(array $array /* = ['ckey' => '', 'duration' => '', 'reason'
}
$array['ckey'] = $item['ss13'];
}
if (isset($this->verifier) && $member = $this->verifier->getVerifiedMember($array['ckey'])) {
if ($this->canWriteDiscordRoles() && isset($this->verifier) && $member = $this->verifier->getVerifiedMember($array['ckey'])) {
if (! $member->roles->has($this->role_ids['Banished'])) {
if (! $permanent) {
$member->addRole($this->role_ids['Banished'], "Banned for {$array['duration']} with the reason {$array['reason']}");
$this->addDiscordRole($member, $this->role_ids['Banished'], "Banned for {$array['duration']} with the reason {$array['reason']}");
} else {
$member->setRoles([$this->role_ids['Banished'], $this->role_ids['Permabanished']], "Banned for {$array['duration']} with the reason {$array['reason']}");
$this->replaceDiscordRoles($member, [$this->role_ids['Banished'], $this->role_ids['Permabanished']], "Banned for {$array['duration']} with the reason {$array['reason']}");
}
}
}
Expand Down
12 changes: 6 additions & 6 deletions src/Civ13/GameServer.php
Original file line number Diff line number Diff line change
Expand Up @@ -1653,10 +1653,10 @@ public function ban(array $array /* = ['ckey' => '', 'duration' => '', 'reason'
if (!$this->legacy) { return $this->sqlBan($array, $admin); }
$result = $this->legacyBan($array, $admin);
if (str_starts_with($result, 'Unable to open')) { return $result; }
if (isset($this->civ13->verifier) && $member = $this->civ13->verifier->getVerifiedMember($array['ckey'])) {
if ($this->civ13->canWriteDiscordRoles() && isset($this->civ13->verifier) && $member = $this->civ13->verifier->getVerifiedMember($array['ckey'])) {
if (! $member->roles->has($this->civ13->role_ids['Banished'])) {
$string = "Banned for {$array['duration']} with the reason {$array['reason']}";
$permanent ? $member->setRoles([$this->civ13->role_ids['Banished'], $this->civ13->role_ids['Permabanished']], $string) : $member->addRole($this->civ13->role_ids['Banished'], $string);
$permanent ? $this->civ13->replaceDiscordRoles($member, [$this->civ13->role_ids['Banished'], $this->civ13->role_ids['Permabanished']], $string) : $this->civ13->addDiscordRole($member, $this->civ13->role_ids['Banished'], $string);
}
}

Expand Down Expand Up @@ -1720,13 +1720,13 @@ public function unban(string $ckey, ?string $admin = null): PromiseInterface
if (!$this->legacy) { return reject(new \LogicException('SQL unban is not implemented.')); }
return $this->legacyUnban($ckey, $admin)->then(function () use ($ckey, $admin): PromiseInterface {
$roles = [];
if (isset($this->civ13->verifier) && $member = $this->civ13->verifier->getVerifiedMember($ckey)) {
if ($this->civ13->canWriteDiscordRoles() && isset($this->civ13->verifier) && $member = $this->civ13->verifier->getVerifiedMember($ckey)) {
if ($member->roles->has($this->civ13->role_ids['Banished'])) {
$roles[] = $member->removeRole($this->civ13->role_ids['Banished'], "Unbanned by $admin");
$roles[] = $this->civ13->removeDiscordRole($member, $this->civ13->role_ids['Banished'], "Unbanned by $admin");
}
if ($member->roles->has($this->civ13->role_ids['Permabanished'])) {
$roles[] = $member->removeRole($this->civ13->role_ids['Permabanished'], "Unbanned by $admin")->then(function () use (&$member, $admin) {
return $member->addRole($this->civ13->role_ids['Verified'], "Unbanned by $admin");
$roles[] = $this->civ13->removeDiscordRole($member, $this->civ13->role_ids['Permabanished'], "Unbanned by $admin")->then(function () use (&$member, $admin) {
return $this->civ13->addDiscordRole($member, $this->civ13->role_ids['Verified'], "Unbanned by $admin");
});
}
}
Expand Down
6 changes: 5 additions & 1 deletion src/Civ13/MessageCommand/Commands/ApproveMe.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,15 @@ class ApproveMe extends Civ13MessageCommand
*/
public function __invoke(Message $message, string $command, array $message_filtered): PromiseInterface
{
if (! $this->civ13->canWriteDiscordRoles()) {
return $this->civ13->reply($message, \Civ13\Civ13::DISCORD_ROLE_AUTHORITY_MESSAGE);
}

if (isset($this->civ13->role_ids['Verified']) && $message->member->roles->has($this->civ13->role_ids['Verified'])) {
return $this->civ13->reply($message, 'You already have the verification role!');
}
if ($item = $this->civ13->verifier->getVerifiedItem($message->author)) {
return $message->member->setRoles([$this->civ13->role_ids['Verified']], "approveme {$item['ss13']}")
return $this->civ13->replaceDiscordRoles($message->member, [$this->civ13->role_ids['Verified']], "approveme {$item['ss13']}")
->then(static fn () => $message->react('👍'));
}
if (! $ckey = self::messageWithoutCommand($command, $message_filtered, true, true)) {
Expand Down
6 changes: 3 additions & 3 deletions src/Civ13/MessageCommand/Commands/BanCheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,17 +115,17 @@ protected function fillContent(GameServer &$gameserver, string &$content, bool &
*/
protected function updateBanished(bool $found, string $ckey): ?PromiseInterface
{
if (! isset($this->civ13->role_ids['Banished'])) {
if (! $this->civ13->canWriteDiscordRoles() || ! isset($this->civ13->role_ids['Banished'])) {
return null;
}
if (! $member = $this->civ13->verifier->getVerifiedMember($ckey)) {
return null;
}
if ($found && ! $member->roles->has($this->civ13->role_ids['Banished'])) {
return $member->addRole($this->civ13->role_ids['Banished']);
return $this->civ13->addDiscordRole($member, $this->civ13->role_ids['Banished']);
}

//if (! $found && $member->roles->has($this->civ13->role_ids['Banished'])) return $member->removeRole($this->civ13->role_ids['Banished']); // They might be banned on a disabled server
//if (! $found && $member->roles->has($this->civ13->role_ids['Banished'])) return $this->civ13->removeDiscordRole($member, $this->civ13->role_ids['Banished']); // They might be banned on a disabled server
return null;
}
}
4 changes: 2 additions & 2 deletions src/Civ13/MessageCommand/Commands/Civ13GameServerBan.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,9 @@ public function __invoke(Message $message, string $command, array $message_filte
}
$arr = ['ckey' => $split_message[0], 'duration' => $split_message[1], 'reason' => $split_message[2]." Appeal at {$this->civ13->discord_formatted}"];
$result = $this->civ13->ban($arr, $this->authorCkey($message), $this->gameserver->key);
if ($member = $this->civ13->verifier->getVerifiedMember($split_message[0])) {
if ($this->civ13->canWriteDiscordRoles() && $member = $this->civ13->verifier->getVerifiedMember($split_message[0])) {
if (! $member->roles->has($this->civ13->role_ids['Banished'])) {
$member->addRole($this->civ13->role_ids['Banished'], $result);
$this->civ13->addDiscordRole($member, $this->civ13->role_ids['Banished'], $result);
}
}

Expand Down
8 changes: 6 additions & 2 deletions src/Civ13/MessageCommand/Commands/FixRoles.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ class FixRoles extends Civ13MessageCommand
*/
public function __invoke(Message $message, string $command, array $message_filtered): PromiseInterface
{
if (! $this->civ13->canWriteDiscordRoles()) {
return $this->civ13->reply($message, \Civ13\Civ13::DISCORD_ROLE_AUTHORITY_MESSAGE);
}

if (! $guild = $this->civ13->discord->guilds->get('id', $this->civ13->civ13_guild_id)) {
return $message->react('🔥');
}
Expand All @@ -38,7 +42,7 @@ public function __invoke(Message $message, string $command, array $message_filte
})) {
foreach ($unverified_members as $member) {
if ($this->civ13->verifier->getVerifiedItem($member)) {
$member->addRole($this->civ13->role_ids['Verified'], 'fixroles');
$this->civ13->addDiscordRole($member, $this->civ13->role_ids['Verified'], 'fixroles');
}
}
}
Expand All @@ -47,7 +51,7 @@ public function __invoke(Message $message, string $command, array $message_filte
) {
foreach ($verified_members as $member) {
if (! $this->civ13->verifier->getVerifiedItem($member)) {
$member->removeRole($this->civ13->role_ids['Verified'], 'fixroles');
$this->civ13->removeDiscordRole($member, $this->civ13->role_ids['Verified'], 'fixroles');
}
}
}
Expand Down
4 changes: 4 additions & 0 deletions src/Civ13/MessageCommand/Commands/JoinRoles.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ class JoinRoles extends Civ13MessageCommand
*/
public function __invoke(Message $message, string $command, array $message_filtered): PromiseInterface
{
if (! $this->civ13->canWriteDiscordRoles()) {
return $this->civ13->reply($message, \Civ13\Civ13::DISCORD_ROLE_AUTHORITY_MESSAGE);
}

$this->civ13->verifier->getVerified();
foreach ($this->civ13->verifier->provisional as $item) {
$this->civ13->verifier->provisionalRegistration($item['ss13'], $item['discord']);
Expand Down
Loading
Loading