Assign list of roles to new user on join

This commit is contained in:
Miguel Nogueira 2020-12-27 22:53:11 +00:00
parent 8470b8a423
commit 7a79963c47
Signed by: miguel456
GPG Key ID: 2CF61B825316C6A0
3 changed files with 32 additions and 0 deletions

View File

@ -8,6 +8,9 @@ COMMAND_PREFIX=""
# Sign up for an API key at the CatAPI website. Used for commands in the Fun category. # Sign up for an API key at the CatAPI website. Used for commands in the Fun category.
CAT_API_KEY="" CAT_API_KEY=""
# Assign these roles to members on join
JOIN_ROLES="ID1,ID2"
# JSON Web Token Configuration # JSON Web Token Configuration
RASPBERRY_STAFF_URL="" RASPBERRY_STAFF_URL=""
RASPBERRY_STAFF_USERNAME="" RASPBERRY_STAFF_USERNAME=""

View File

@ -11,6 +11,7 @@ $dotenv->load();
$dotenv->required('BOT_AUTH_TOKEN')->notEmpty(); $dotenv->required('BOT_AUTH_TOKEN')->notEmpty();
$dotenv->required('COMMAND_PREFIX')->notEmpty(); $dotenv->required('COMMAND_PREFIX')->notEmpty();
$dotenv->required('CAT_API_KEY')->notEmpty(); $dotenv->required('CAT_API_KEY')->notEmpty();
$dotenv->required('JOIN_ROLES')->notEmpty();
define('PROJECT_ROOT', realpath(__DIR__ . '/../../')); define('PROJECT_ROOT', realpath(__DIR__ . '/../../'));

View File

@ -2,8 +2,12 @@
namespace nogueiracodes\RaspberryBot\Core; namespace nogueiracodes\RaspberryBot\Core;
use Analog\Analog;
use Analog\Handler\Mongo;
use Discord\Discord; use Discord\Discord;
use Discord\DiscordCommandClient; use Discord\DiscordCommandClient;
use Discord\Parts\User\Member;
use Discord\WebSockets\Event;
use Exception; use Exception;
use nogueiracodes\RaspberryBot\Core\Interfaces\Action; use nogueiracodes\RaspberryBot\Core\Interfaces\Action;
use nogueiracodes\RaspberryBot\Core\Interfaces\Command; use nogueiracodes\RaspberryBot\Core\Interfaces\Command;
@ -127,6 +131,30 @@ class RaspberryBot
{ {
$this->commandClient->on('ready', function() $this->commandClient->on('ready', function()
{ {
$this->commandClient->on(Event::GUILD_MEMBER_ADD, function(Member $member, Discord $discord) {
Analog::info('Bot: New member ' . $member->username . '#' . $member->discriminator . ' has joined the current guild.');
if (isset($_ENV['JOIN_ROLES']) && is_int($_ENV['JOIN_ROLES']))
{
$roles = explode(',', $_ENV['JOIN_ROLES']);
foreach($roles as $role)
{
// This approach is prone to errors since calling done() passes error responsibility to us, so if an error escapes any of the callbacks, it causes a fatal error
$member->addRole($role)->done(function ($promise) use ($member){
Analog::info('Bot: Successfully added role to ' . $member->id);
}, function($rejectedPromise) use ($member) {
Analog::error('Bot: Rejected role promise, failed to add role to user ' . $member->id . '. Rejection reason: ' . $rejectedPromise->getMessage());
});
}
}
});
$this->commandClient->on('message', function($message) $this->commandClient->on('message', function($message)
{ {