<commit_before><?php

/*
 * This file is apart of the DiscordPHP project.
 *
 * Copyright (c) 2016-2020 <NAME> <<EMAIL>>
 *
 * This source file is subject to the MIT license that is bundled
 * with this source code in the LICENSE.md file.
 */

namespace Discord\Parts\User;

use Carbon\Carbon;
use Discord\Helpers\Collection;
use Discord\Parts\Channel\Channel;
use Discord\Parts\Channel\Overwrite;
use Discord\Parts\Guild\Ban;
use Discord\Parts\Guild\Guild;
use Discord\Parts\Guild\Role;
use Discord\Parts\Part;
use Discord\Parts\Permissions\RolePermission;
use Discord\Parts\WebSockets\PresenceUpdate;
use Discord\Helpers\Deferred;
use React\Promise\ExtendedPromiseInterface;
use function React\Partial\bind as Bind;

/**
 * A member is a relationship between a user and a guild. It contains user-to-guild specific data like roles.
 *
 * @property string                       $id            The unique identifier of the member.
 * @property string                       $username      The username of the member.
 * @property string                       $discriminator The discriminator of the member.
 * @property User                         $user          The user part of the member.
 * @property Collection|Role[]            $roles         A collection of Roles that the member has.
 * @property bool                         $deaf          Whether the member is deaf.
 * @property bool                         $mute          Whether the member is mute.
 * @property Carbon                       $joined_at     A timestamp of when the member joined the guild.
 * @property Guild                        $guild         The guild that the member belongs to.
 * @property string                       $guild_id      The unique identifier of the guild that the member belongs to.
 * @property string                       $status        The status of the member.
 * @property Activity                     $game          The game the member is playing.
 * @property string|null                  $nick          The nickname of the member.
 * @property Carbon                       $premium_since When the user started boosting the server.
 * @property Collection|Activity[]        $activities User's current activities.
 * @property object                       $client_status Current client status
 */
class Member extends Part
{
    /**
     * {@inheritdoc}
     */
    protected $fillable = ['user', 'roles', 'deaf', 'mute', 'joined_at', 'guild_id', 'status', 'game', 'nick', 'premium_since', 'activities', 'client_status'];

    /**
     * {@inheritdoc}
     */
    protected $fillAfterSave = false;

    /**
     * Updates the member from a new presence update object.
     * This is an internal function and is not meant to be used by a public application.
     *
     * @param PresenceUpdate $presence
     *
     * @return PresenceUpdate Old presence.
     * @throws \Exception
     */
    public function updateFromPresence(PresenceUpdate $presence): Part
    {
        $rawPresence = $presence->getRawAttributes();
        $oldPresence = $this->factory->create(PresenceUpdate::class, $this->attributes, true);

        $this->attributes = array_merge($this->attributes, $rawPresence);

        return $oldPresence;
    }

    /**
     * Bans the member.
     *
     * @param int|null $daysToDeleteMessages The amount of days to delete messages from.
     * @param string|null $reason
     *
     * @return ExtendedPromiseInterface
     * @throws \Exception
     */
    public function ban(?int $daysToDeleteMessages = null, ?string $reason = null): ExtendedPromiseInterface
    {
        $deferred = new Deferred();
        $content = [];

        $url = $this->replaceWithVariables('guilds/:guild_id/bans/:id');

        if (! is_null($daysToDeleteMessages)) {
            $content['delete-message-days'] = $daysToDeleteMessages;
        }

        if (! is_null($reason)) {
            $content['reason'] = $reason;
        }

        $this->http->put($url, $content)->done(
            function () use ($deferred) {
                $ban = $this->factory->create(Ban::class, [
                    'user' => $this->attributes['user'],
                    'guild' => $this->guild,
                ], true);

                $deferred->resolve($ban);
            },
            Bind([$deferred, 'reject'])
        );

        return $deferred->promise();
    }

    /**
     * Sets the nickname of the member.<commit_msg>Fix error when banning users<commit_after><?php

/*
 * This file is apart of the DiscordPHP project.
 *
 * Copyright (c) 2016-2020 <NAME> <<EMAIL>>
 *
 * This source file is subject to the MIT license that is bundled
 * with this source code in the LICENSE.md file.
 */

namespace Discord\Parts\User;

use Carbon\Carbon;
use Discord\Helpers\Collection;
use Discord\Parts\Channel\Channel;
use Discord\Parts\Channel\Overwrite;
use Discord\Parts\Guild\Ban;
use Discord\Parts\Guild\Guild;
use Discord\Parts\Guild\Role;
use Discord\Parts\Part;
use Discord\Parts\Permissions\RolePermission;
use Discord\Parts\WebSockets\PresenceUpdate;
use Discord\Helpers\Deferred;
use React\Promise\ExtendedPromiseInterface;
use function React\Partial\bind as Bind;

/**
 * A member is a relationship between a user and a guild. It contains user-to-guild specific data like roles.
 *
 * @property string                       $id            The unique identifier of the member.
 * @property string                       $username      The username of the member.
 * @property string                       $discriminator The discriminator of the member.
 * @property User                         $user          The user part of the member.
 * @property Collection|Role[]            $roles         A collection of Roles that the member has.
 * @property bool                         $deaf          Whether the member is deaf.
 * @property bool                         $mute          Whether the member is mute.
 * @property Carbon                       $joined_at     A timestamp of when the member joined the guild.
 * @property Guild                        $guild         The guild that the member belongs to.
 * @property string                       $guild_id      The unique identifier of the guild that the member belongs to.
 * @property string                       $status        The status of the member.
 * @property Activity                     $game          The game the member is playing.
 * @property string|null                  $nick          The nickname of the member.
 * @property Carbon                       $premium_since When the user started boosting the server.
 * @property Collection|Activity[]        $activities User's current activities.
 * @property object                       $client_status Current client status
 */
class Member extends Part
{
    /**
     * {@inheritdoc}
     */
    protected $fillable = ['user', 'roles', 'deaf', 'mute', 'joined_at', 'guild_id', 'status', 'game', 'nick', 'premium_since', 'activities', 'client_status'];

    /**
     * {@inheritdoc}
     */
    protected $fillAfterSave = false;

    /**
     * Updates the member from a new presence update object.
     * This is an internal function and is not meant to be used by a public application.
     *
     * @param PresenceUpdate $presence
     *
     * @return PresenceUpdate Old presence.
     * @throws \Exception
     */
    public function updateFromPresence(PresenceUpdate $presence): Part
    {
        $rawPresence = $presence->getRawAttributes();
        $oldPresence = $this->factory->create(PresenceUpdate::class, $this->attributes, true);

        $this->attributes = array_merge($this->attributes, $rawPresence);

        return $oldPresence;
    }

    /**
     * Bans the member.
     *
     * @param int|null $daysToDeleteMessages The amount of days to delete messages from.
     * @param string|null $reason
     *
     * @return ExtendedPromiseInterface
     * @throws \Exception
     */
    public function ban(?int $daysToDeleteMessages = null, ?string $reason = null): ExtendedPromiseInterface
    {
        $deferred = new Deferred();
        $content = [];

        $url = $this->replaceWithVariables('guilds/:guild_id/bans/:id');

        if (! is_null($daysToDeleteMessages)) {
            $content['delete-message-days'] = $daysToDeleteMessages;
        }

        if (! is_null($reason)) {
            $content['reason'] = $reason;
        }

        $this->http->put($url, empty($content) ? null : $content)->done(
            function () use ($deferred) {
                $ban = $this->factory->create(Ban::class, [
                    'user' => $this->attributes['user'],
                    'guild' => $this->guild,
                ], true);

                $deferred->resolve($ban);
            },
            Bind([$deferred, 'reject'])
        );

        return $deferred->promise();
    }

    /**
     * Sets the nickname of the member.
{'fraction_non_alphanumeric': 0.1046067415730337, 'fraction_numerical': 0.0017977528089887641, 'mean_word_length': 2.102474729871035, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 45, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2116517', 'n_tokens_mistral': 2412, 'n_tokens_neox': 2298, 'n_words': 739}