98 lines
2.7 KiB
PHP
98 lines
2.7 KiB
PHP
<?php
|
|
|
|
namespace Omnicron\Toadling;
|
|
|
|
use \Discord\Discord;
|
|
use \Discord\Parts\Channel\Message;
|
|
use \Discord\Parts\Guild\Role;
|
|
use \Discord\Parts\User\Member;
|
|
use \Discord\WebSockets\Intents;
|
|
use \Discord\WebSockets\Event;
|
|
|
|
class Toadling
|
|
{
|
|
|
|
public const COMMAND_NAME = 'toadling';
|
|
|
|
protected Discord $discord;
|
|
|
|
public function __construct() {
|
|
$this->discord = new Discord([
|
|
'token' => 'OTAyODgxNTUzMjU5MDQ0OTM1.YXk3-Q.VYjX0smJWx4eCRVbxP_ACA3veXI',
|
|
'intents' => Intents::getDefaultIntents() | Intents::GUILD_MEMBERS | Intents::GUILD_PRESENCES,
|
|
'loadAllMembers' => true,
|
|
]);
|
|
$this->discord->on('ready', [$this, 'initialize']);
|
|
}
|
|
|
|
public function initialize(Discord $discord) {
|
|
$discord->on(Event::GUILD_MEMBER_ADD, [$this, 'moveMemberToNotToad']);
|
|
$discord->on(Event::MESSAGE_CREATE, [$this, 'setRoleColor']);
|
|
}
|
|
|
|
public function moveMemberToNotToad(Member $member, Discord $discord) {
|
|
if('902654364685074522' !== $member->guild_id) {
|
|
return false;
|
|
}
|
|
$notToadRole = $member->guild->roles->find(function (Role $role) {
|
|
return '902655062982148106' === $role->id;
|
|
});
|
|
if(null === $notToadRole) {
|
|
return false;
|
|
}
|
|
$member->addRole($notToadRole);
|
|
return true;
|
|
}
|
|
|
|
public function setRoleColor(Message $message, Discord $discord) {
|
|
if('902654364685074522' !== $message->guild_id) {
|
|
return false;
|
|
}
|
|
preg_match_all('/[^\s]+/', $message->content, $matches);
|
|
$messageWords = $matches[0];
|
|
if($messageWords[0] !== '!'.self::COMMAND_NAME) {
|
|
return false;
|
|
}
|
|
if($messageWords[1] !== 'color') {
|
|
return false;
|
|
}
|
|
$color = strtoupper($messageWords[2]);
|
|
if(preg_match('/^#[0-9A-F]{6}$/', $color) !== 1) {
|
|
return false;
|
|
}
|
|
$guild = $discord->guilds->get('id', '902654364685074522');
|
|
$colorRole = $guild->roles->find(function (Role $role) use ($color) {
|
|
return $role->name === $color;
|
|
});
|
|
$colorInt = intval(substr($color, 1), 16);
|
|
if(null !== $colorRole) {
|
|
$message->member->addRole($colorRole);
|
|
$message->delete();
|
|
return true;
|
|
}
|
|
$guild->createRole([
|
|
'name' => $color,
|
|
'color' => $colorInt,
|
|
])->done(function (Role $colorRole) use ($message, $guild) {
|
|
$roles = $guild->roles;
|
|
$rolesArray = $roles->toArray();
|
|
$rolesArrayPositions = [];
|
|
foreach($rolesArray as $role) {
|
|
$rolesArrayPositions[$role->position+1] = $role->id;
|
|
if($role->id === $colorRole->id) {
|
|
$rolesArrayPositions[1] = 1;
|
|
}
|
|
}
|
|
$guild->updateRolePositions($rolesArrayPositions);
|
|
$message->member->addRole($colorRole);
|
|
$message->delete();
|
|
});
|
|
return true;
|
|
}
|
|
|
|
public function run() {
|
|
$this->discord->run();
|
|
}
|
|
|
|
}
|