示例#1
0
        private async Task HandleStreamerRole(SocketGuildUser guildUser, StreamerSettings settings)
        {
            // Get the streaming role.
            var streamingRole = guildUser.Guild.Roles.FirstOrDefault(r => r.Id == settings.RoleId);

            // Remove the streaming role if it does not exist.
            if (streamingRole == null)
            {
                await _settingsService.RemoveSetting(settings);

                Logger.LogError("Streamer Module: Role could not be found!");
            }
            else
            {
                // Add use to role.
                if (guildUser.Activity != null && guildUser.Activity.Type == ActivityType.Streaming)
                {
                    await AddUserToStreamingRole(guildUser, streamingRole);
                }

                // Remove user from role.
                else if (guildUser.Roles.Any(r => r.Id == streamingRole.Id))
                {
                    await RemoveUserFromStreamingRole(guildUser, streamingRole);
                }
            }
        }
示例#2
0
        private async Task HandleStreamerRole(SocketGuildUser guildUser, StreamerSettings settings)
        {
            // Get the streaming role.
            var streamingRole = guildUser.Guild.Roles.FirstOrDefault(r => r.Id == settings.RoleId);

            // Remove the streaming role if it does not exist.
            if (streamingRole == null)
            {
                await _settingsService.RemoveSetting(settings);

                Logger.LogError("Streamer Module: Role could not be found!");
            }
            else
            {
                using (var scope = _scopeFactory.CreateScope())
                {
                    var botService = scope.ServiceProvider.GetRequiredService <IBot>();

                    var botRolePosition = botService.GetBotRoleHierarchy(guildUser.Guild.Id);

                    if (streamingRole.Position < botRolePosition)
                    {
                        // Add use to role.
                        if (guildUser.Activity != null && guildUser.Activity.Type == ActivityType.Streaming &&
                            IsUserWhiteListed(settings, guildUser))
                        {
                            await AddUserToStreamingRole(guildUser, streamingRole);
                        }

                        // Remove user from role.
                        else if (guildUser.Roles.Any(r => r.Id == streamingRole.Id))
                        {
                            await RemoveUserFromStreamingRole(guildUser, streamingRole);
                        }
                    }
                    else
                    {
                        Logger.LogError(
                            $"Streamer Module: Could not add/remove role as bot has insufficient hierarchical position. " +
                            $"Guild Id: {guildUser.Guild.Id}");

                        await botService.GetGuild(guildUser.Guild.Id).Owner.SendMessageAsync(
                            $"We couldn't add/remove Role '{streamingRole.Name}' to a user as the role's hierarchical position is greater than the bot's.{Environment.NewLine}" +
                            $"To fix this, please adjust your role's position and then re-enable the module via our website.{Environment.NewLine}" +
                            $"Guild: {guildUser.Guild.Name}");

                        var settingsDb = await _settingsService.GetSettingsByGuild(guildUser.Guild.Id);

                        settingsDb.StreamerRoleEnabled = false;

                        await _settingsService.SaveSettings(settingsDb);
                    }
                }
            }
        }
        /// <summary>
        ///     Initialize the module on GuildMemberUpdated event.
        /// </summary>
        /// <param name="client">Client that the module will be registered to.</param>
        public override Task Init(DiscordSocketClient client)
        {
            client.GuildMemberUpdated += async(user, guildUser) =>
            {
                var settings = await _settingsService.GetSettingsByGuild(guildUser.Guild.Id);

                if (settings != null && settings.Enabled)
                {
                    // Get the streaming role.
                    var streamingRole = guildUser.Guild.Roles.FirstOrDefault(r => r.Id == settings.RoleId);

                    // Remove the streaming role if it does not exist.
                    if (streamingRole == null)
                    {
                        await _settingsService.RemoveSetting(settings);

                        Logger.LogError("StreamingRole Module: Role could not be found!");
                    }
                    else
                    {
                        // Add use to role.
                        if (guildUser.Game != null && guildUser.Game.Value.StreamType == StreamType.Twitch)
                        {
                            await AddUserToStreamingRole(guildUser, streamingRole);
                        }
                        // Remove user from role.
                        else if (guildUser.Roles.Any(r => r == streamingRole))
                        {
                            await RemoveUserFromStreamingRole(guildUser, streamingRole);
                        }
                    }
                }
            };

            return(Task.CompletedTask);
        }