/// <summary>
        /// Gets the current API application.
        /// </summary>
        /// <returns>Current API application.</returns>
        public async Task <DiscordApplication> GetCurrentApplicationAsync()
        {
            var tapp = await this.ApiClient.GetCurrentApplicationInfoAsync().ConfigureAwait(false);

            var app = new DiscordApplication
            {
                Discord           = this,
                Id                = tapp.Id,
                Name              = tapp.Name,
                Description       = tapp.Description,
                Summary           = tapp.Summary,
                IconHash          = tapp.IconHash,
                TermsOfServiceUrl = tapp.TermsOfServiceUrl,
                PrivacyPolicyUrl  = tapp.PrivacyPolicyUrl,
                RpcOrigins        = tapp.RpcOrigins != null ? new ReadOnlyCollection <string>(tapp.RpcOrigins) : null,
                Flags             = tapp.Flags,
                RequiresCodeGrant = tapp.BotRequiresCodeGrant,
                IsPublic          = tapp.IsPublicBot,
                CoverImageHash    = null
            };

            // do team and owners
            // tbh f**k doing this properly
            if (tapp.Team == null)
            {
                // singular owner

                app.Owners = new ReadOnlyCollection <DiscordUser>(new[] { new DiscordUser(tapp.Owner) });
                app.Team   = null;
            }
            else
            {
                // team owner

                app.Team = new DiscordTeam(tapp.Team);

                var members = tapp.Team.Members
                              .Select(x => new DiscordTeamMember(x)
                {
                    Team = app.Team, User = new DiscordUser(x.User)
                })
                              .ToArray();

                var owners = members
                             .Where(x => x.MembershipStatus == DiscordTeamMembershipStatus.Accepted)
                             .Select(x => x.User)
                             .ToArray();

                app.Owners       = new ReadOnlyCollection <DiscordUser>(owners);
                app.Team.Owner   = owners.FirstOrDefault(x => x.Id == tapp.Team.OwnerId);
                app.Team.Members = new ReadOnlyCollection <DiscordTeamMember>(members);
            }

            return(app);
        }
        /// <summary>
        /// Initializes this client. This method fetches information about current user and application.
        /// </summary>
        /// <returns></returns>
        public virtual async Task InitializeAsync()
        {
            if (this._current_user == null)
            {
                this._current_user = await this.ApiClient.GetCurrentUserAsync();
            }

            if (this.Configuration.TokenType != TokenType.User && this._current_application == null)
            {
                this._current_application = await this.GetCurrentApplicationAsync();
            }
        }
Exemple #3
0
        /// <summary>
        /// Initializes and connects all shards.
        /// </summary>
        /// <returns></returns>
        public async Task StartAsync()
        {
            var shardc = await this.InitializeShardsAsync().ConfigureAwait(false);

            this.DebugLogger.LogMessage(LogLevel.Info, "Autoshard", $"Booting {shardc.ToString(CultureInfo.InvariantCulture)} shards", DateTime.Now);

            for (var i = 0; i < shardc; i++)
            {
                if (!this.Shards.TryGetValue(i, out var client))
                {
                    throw new Exception("Could not initialize shards");
                }

                if (this.CurrentUser != null)
                {
                    client.CurrentUser = this.CurrentUser;
                }

                if (this._current_application != null)
                {
                    client.CurrentApplication = this.CurrentApplication;
                }

                if (this.InternalVoiceRegions != null)
                {
                    client.InternalVoiceRegions = this.InternalVoiceRegions;
                    client._voice_regions_lazy  = new Lazy <IReadOnlyDictionary <string, DiscordVoiceRegion> >(() => new ReadOnlyDictionary <string, DiscordVoiceRegion>(client.InternalVoiceRegions));
                }

                client.ClientErrored                  += this.Client_ClientError;
                client.SocketErrored                  += this.Client_SocketError;
                client.SocketOpened                   += this.Client_SocketOpened;
                client.SocketClosed                   += this.Client_SocketClosed;
                client.Ready                          += this.Client_Ready;
                client.Resumed                        += this.Client_Resumed;
                client.ChannelCreated                 += this.Client_ChannelCreated;
                client.DmChannelCreated               += this.Client_DMChannelCreated;
                client.ChannelUpdated                 += this.Client_ChannelUpdated;
                client.ChannelDeleted                 += this.Client_ChannelDeleted;
                client.DmChannelDeleted               += this.Client_DMChannelDeleted;
                client.ChannelPinsUpdated             += this.Client_ChannelPinsUpdated;
                client.GuildCreated                   += this.Client_GuildCreated;
                client.GuildAvailable                 += this.Client_GuildAvailable;
                client.GuildUpdated                   += this.Client_GuildUpdated;
                client.GuildDeleted                   += this.Client_GuildDeleted;
                client.GuildUnavailable               += this.Client_GuildUnavailable;
                client.GuildDownloadCompleted         += this.Client_GuildDownloadCompleted;
                client.MessageCreated                 += this.Client_MessageCreated;
                client.PresenceUpdated                += this.Client_PresenceUpdate;
                client.GuildBanAdded                  += this.Client_GuildBanAdd;
                client.GuildBanRemoved                += this.Client_GuildBanRemove;
                client.GuildEmojisUpdated             += this.Client_GuildEmojisUpdate;
                client.GuildIntegrationsUpdated       += this.Client_GuildIntegrationsUpdate;
                client.GuildMemberAdded               += this.Client_GuildMemberAdd;
                client.GuildMemberRemoved             += this.Client_GuildMemberRemove;
                client.GuildMemberUpdated             += this.Client_GuildMemberUpdate;
                client.GuildRoleCreated               += this.Client_GuildRoleCreate;
                client.GuildRoleUpdated               += this.Client_GuildRoleUpdate;
                client.GuildRoleDeleted               += this.Client_GuildRoleDelete;
                client.MessageUpdated                 += this.Client_MessageUpdate;
                client.MessageDeleted                 += this.Client_MessageDelete;
                client.MessagesBulkDeleted            += this.Client_MessageBulkDelete;
                client.TypingStarted                  += this.Client_TypingStart;
                client.UserSettingsUpdated            += this.Client_UserSettingsUpdate;
                client.UserUpdated                    += this.Client_UserUpdate;
                client.VoiceStateUpdated              += this.Client_VoiceStateUpdate;
                client.VoiceServerUpdated             += this.Client_VoiceServerUpdate;
                client.GuildMembersChunked            += this.Client_GuildMembersChunk;
                client.UnknownEvent                   += this.Client_UnknownEvent;
                client.MessageReactionAdded           += this.Client_MessageReactionAdd;
                client.MessageReactionRemoved         += this.Client_MessageReactionRemove;
                client.MessageReactionsCleared        += this.Client_MessageReactionRemoveAll;
                client.WebhooksUpdated                += this.Client_WebhooksUpdate;
                client.Heartbeated                    += this.Client_HeartBeated;
                client.DebugLogger.LogMessageReceived += this.DebugLogger_LogMessageReceived;

                await client.ConnectAsync().ConfigureAwait(false);

                this.DebugLogger.LogMessage(LogLevel.Info, "Autoshard", $"Booted shard {i.ToString(CultureInfo.InvariantCulture)}", DateTime.Now);

                if (this._current_user == null)
                {
                    this._current_user = client.CurrentUser;
                }

                if (this._current_application == null)
                {
                    this._current_application = client.CurrentApplication;
                }

                if (this.InternalVoiceRegions == null)
                {
                    this.InternalVoiceRegions = client.InternalVoiceRegions;
                    this._voice_regions_lazy  = new Lazy <IReadOnlyDictionary <string, DiscordVoiceRegion> >(() => new ReadOnlyDictionary <string, DiscordVoiceRegion>(this.InternalVoiceRegions));
                }
            }
        }
 public Task <IReadOnlyList <DiscordApplicationAsset> > GetApplicationAssetsAsync(DiscordApplication app)
 => ApiClient.GetApplicationAssetsAsync(app);