예제 #1
0
        internal DiscordClientBase(DiscordClientBase client)
        {
            if (client == null)
            {
                throw new ArgumentNullException(nameof(client));
            }

            _client = client;

            _getGateway = client._getGateway;
            State       = client.State;
            // This is set, so that events give you the proper (bot) client, and not the underyling one.
            State._client = this;
            RestClient    = client.RestClient;
            Logger        = client.Logger;
            Serializer    = client.Serializer;
            _extensions   = client._extensions;

            _ready                 = client._ready;
            _channelCreated        = client._channelCreated;
            _channelUpdated        = client._channelUpdated;
            _channelDeleted        = client._channelDeleted;
            _channelPinsUpdated    = client._channelPinsUpdated;
            _guildAvailable        = client._guildAvailable;
            _guildUnavailable      = client._guildUnavailable;
            _joinedGuild           = client._joinedGuild;
            _guildUpdated          = client._guildUpdated;
            _leftGuild             = client._leftGuild;
            _roleCreated           = client._roleCreated;
            _roleUpdated           = client._roleUpdated;
            _roleDeleted           = client._roleDeleted;
            _inviteCreated         = client._inviteCreated;
            _inviteDeleted         = client._inviteDeleted;
            _memberBanned          = client._memberBanned;
            _memberUnbanned        = client._memberUnbanned;
            _guildEmojisUpdated    = client._guildEmojisUpdated;
            _memberJoined          = client._memberJoined;
            _memberLeft            = client._memberLeft;
            _memberUpdated         = client._memberUpdated;
            _messageReceived       = client._messageReceived;
            _messageDeleted        = client._messageDeleted;
            _messagesBulkDeleted   = client._messagesBulkDeleted;
            _messageUpdated        = client._messageUpdated;
            _reactionAdded         = client._reactionAdded;
            _reactionRemoved       = client._reactionRemoved;
            _reactionsCleared      = client._reactionsCleared;
            _emojiReactionsCleared = client._emojiReactionsCleared;
            _presenceUpdated       = client._presenceUpdated;
            _typingStarted         = client._typingStarted;
            _userUpdated           = client._userUpdated;
            _voiceStateUpdated     = client._voiceStateUpdated;
            _voiceServerUpdated    = client._voiceServerUpdated;
            _webhooksUpdated       = client._webhooksUpdated;

            if (this is IDiscordSharder sharder && client is IDiscordSharder clientSharder)
            {
                sharder._shardReady = clientSharder._shardReady;
            }
        }
예제 #2
0
 internal static CachedPrivateChannel Create(DiscordClientBase client, ChannelModel model)
 {
     return(model.Type.Value switch
     {
         ChannelType.Dm => new CachedDmChannel(client, model),
         ChannelType.Group => new CachedGroupChannel(client, model),
         _ => null,
     });
예제 #3
0
        internal CachedGroupChannel(DiscordClientBase client, ChannelModel model) : base(client, model)
        {
            _recipients = new LockedDictionary <Snowflake, CachedUser>(model.Recipients.Value.Length);
            for (var i = 0; i < model.Recipients.Value.Length; i++)
            {
                var recipient = model.Recipients.Value[i];
                _recipients.TryAdd(recipient.Id, client.State.GetOrAddSharedUser(recipient));
            }

            Update(model);
        }
예제 #4
0
        /// <summary>
        ///     Binds this extension to the provided client and initializes it.
        /// </summary>
        /// <param name="client"> The calling client to bind to. </param>
        /// <param name="cancellationToken"> The cancellation token. </param>
        /// <returns>
        ///     A <see cref="Task"/> representing the initialization work.
        /// </returns>
        public async Task InitializeAsync(DiscordClientBase client, CancellationToken cancellationToken = default)
        {
            if (IsInitialized)
            {
                throw new InvalidOperationException($"This extension ({GetType().Name}) has already been initialized.");
            }

            Client = client;
            await InitializeAsync(cancellationToken).ConfigureAwait(false);

            IsInitialized = true;
        }
예제 #5
0
        /// <summary>
        ///     Instantiates a new <see cref="DiscordClientBase"/>, wrapping a pre-existing client.
        ///     Rebinds the specified client's dispatcher to <see langword="this"/>.
        /// </summary>
        /// <param name="logger"> The logger of this client. </param>
        /// <param name="client"> The client to wrap. </param>
        protected DiscordClientBase(
            ILogger logger,
            DiscordClientBase client)
        {
            Logger        = logger;
            RestClient    = client.RestClient;
            GatewayClient = client.GatewayClient;
            _extensions   = client._extensions;

            // Binds `this` to the dispatcher, where `this` is the client implementing DiscordBotBase,
            // wrapping an existing DiscordClientBase.
            client.GatewayClient.Dispatcher.Bind(this);
        }
예제 #6
0
        public DiscordClientState(DiscordClientBase client, MessageCache messageCache)
        {
            _client       = client;
            _messageCache = messageCache ?? DummyMessageCache.Instance;

            _guilds          = new LockedDictionary <Snowflake, CachedGuild>();
            _users           = new LockedDictionary <Snowflake, CachedSharedUser>();
            _privateChannels = new LockedDictionary <Snowflake, CachedPrivateChannel>();

            Guilds          = new ReadOnlyDictionary <Snowflake, CachedGuild>(_guilds);
            Users           = new ReadOnlyUpcastingDictionary <Snowflake, CachedSharedUser, CachedUser>(_users);
            PrivateChannels = new ReadOnlyDictionary <Snowflake, CachedPrivateChannel>(_privateChannels);
            DmChannels      = new ReadOnlyOfTypeDictionary <Snowflake, CachedPrivateChannel, CachedDmChannel>(_privateChannels);
            GroupChannels   = new ReadOnlyOfTypeDictionary <Snowflake, CachedPrivateChannel, CachedGroupChannel>(_privateChannels);
        }
예제 #7
0
        internal CachedGuild(DiscordClientBase client, WebSocketGuildModel model) : base(client, model.Id)
        {
            _roles    = new LockedDictionary <Snowflake, CachedRole>(model.Roles.Value.Length);
            _emojis   = new LockedDictionary <Snowflake, CachedGuildEmoji>(model.Emojis.Value.Length);
            _channels = new LockedDictionary <Snowflake, CachedGuildChannel>(model.Channels.Length);
            _members  = new LockedDictionary <Snowflake, CachedMember>(model.MemberCount);

            Update(model);
            if (client.IsBot && IsLarge)
            {
                ChunksExpected = (int)Math.Ceiling(model.MemberCount / 1000.0);
                ChunkTcs       = new TaskCompletionSource <bool>();
            }
            else if (!client.IsBot)
            {
                SyncTcs = new TaskCompletionSource <bool>();
            }
        }
예제 #8
0
 internal CachedChannel(DiscordClientBase client, ChannelModel model) : base(client, model.Id)
 {
 }
예제 #9
0
 internal CachedUnknownUser(DiscordClientBase client, UserModel model) : base(client, model)
 {
     Update(model);
 }
예제 #10
0
 internal CachedUser(DiscordClientBase client, UserModel model) : base(client, model.Id)
 {
     IsBot = model.Bot;
 }
예제 #11
0
 internal CachedDiscordEntity(DiscordClientBase client)
 {
     Client = client;
 }
예제 #12
0
 internal CachedPrivateChannel(DiscordClientBase client, ChannelModel model) : base(client, model)
 {
 }
예제 #13
0
 internal CachedSnowflakeEntity(DiscordClientBase client, Snowflake id) : base(client)
 {
     Id = id;
 }
예제 #14
0
 internal CachedRelationship(DiscordClientBase client, RelationshipModel model) : base(client, model.Id)
 {
     User = client.State.GetOrAddSharedUser(model.User);
     User.SharedUser.References++;
     Update(model);
 }
예제 #15
0
 internal CachedDmChannel(DiscordClientBase client, ChannelModel model) : base(client, model)
 {
     Recipient = client.State.GetOrAddSharedUser(model.Recipients.Value[0]);
     Update(model);
 }