public async Task HandleJoined(SocketUser user, SocketVoiceChannel newChannel)
        {
            // Get user status
            var status = await GetDiscordUserStatus(user.Id);

            var synthbotUser = status != DiscordUserStatus.New || status != DiscordUserStatus.NoResponse
                                ? await _synthbotRestClient.GetUserFromDiscordIdAsync(user.Id.ToString())
                                : null;

            var playlist = await GetPlaylist(newChannel.Name);

            switch (status)
            {
            case DiscordUserStatus.New when _notificationsEnabled:
            {
                var uri = _spotifyInfoService.GetLoginUriAuto(user.Id.ToString(), user.Username);
                await user.SendMessageAsync(
                    $"Reply with \"@{_discord.CurrentUser.Username} notify\" to opt in to future notifications from me",
                    false,
                    EmbedFactory.Welcome.NewEmbed(user.Username, _discord.CurrentUser, uri));

                await _synthbotRestClient.SetDiscordUserStatus(user.Id.ToString(), DiscordUserStatus.NoResponse);

                return;
            }

            case DiscordUserStatus.RegisteredWithNotify when(synthbotUser?.AutoJoin ?? false):
            {
                await _synthbotRestClient.AddUserToChannel(newChannel.Name);

                if (_notificationsEnabled)
                {
                    await user.SendMessageAsync("", false, EmbedFactory.Notifications.AutoJoinNotify(_discord.CurrentUser, newChannel, playlist));
                }
                return;
            }

            case DiscordUserStatus.RegisteredWithNotify when(!synthbotUser?.AutoJoin ?? false) && _notificationsEnabled:
                await user.SendMessageAsync("", false, EmbedFactory.Notifications.Notify(_discord.CurrentUser, newChannel, playlist));

                break;

            case DiscordUserStatus.RegisteredWithoutNotify when synthbotUser?.AutoJoin ?? false:
                await _synthbotRestClient.AddUserToChannel(newChannel.Name);

                return;

            case DiscordUserStatus.RegisteredWithoutNotify:
            case DiscordUserStatus.NoResponse:
                return;

            default:
                throw new ArgumentOutOfRangeException(nameof(status), status, null);
            }
        }
Ejemplo n.º 2
0
        public async Task JoinChannel(string overrideChannelName = null)
        {
            // Get the the name of the user's joined voicechannel and send an error message if not joined
            var channelName = await GetUserVoiceChannelName(overrideChannelName);

            if (string.IsNullOrWhiteSpace(channelName))
            {
                return;
            }

            // Gets the synthbot user and sends an error message if their default device isn't set
            var synthbotUser = await _synthbotWebClient.GetUserFromDiscordIdAsync(Context.User.Id.ToString());

            if (string.IsNullOrWhiteSpace(synthbotUser.DefaultSpotifyDevice))
            {
                await ReplyAsync($"{Context.User.Mention} you have no playback device set, run \"{Context.Client.CurrentUser.Mention} set-device\" to set one.");

                return;
            }

            // Gets a list of user's available Spotify devices
            var token = await _synthbotWebClient.GetSpotifyToken();

            _spotifyApi.AccessToken = token;

            var response = await _spotifyApi.GetDevicesAsync();

            if (response.HasError())
            {
                await ReplyAsync($"Error | Status: {response.Error.Status} Message: {response.Error.Message}");

                return;
            }

            var devices = response?.Devices;

            if (devices == null || !devices.Any())
            {
                await ReplyAsync($"{Context.User.Mention} Unable to find any Spotify devices for your account");

                return;
            }

            // Check if the user's default device is in their list of devices. If their device isn't open, it wont be in the list, so we alert the user
            var currentDevice = devices.FirstOrDefault(a => a.Id == synthbotUser.DefaultSpotifyDevice);

            if (currentDevice == null)
            {
                await ReplyAsync($"{Context.User.Mention} your selected Spotify player is not online. Make sure it is open before running this command");

                return;
            }

            // Tries to add user to playback and sends an error message if unable to
            var result = await _synthbotWebClient.AddUserToChannel(channelName);

            if (result == true)
            {
                await ReplyAsync("", false, EmbedFactory.Join.JoinSuccess(Context, channelName, currentDevice.Name));
            }
            else
            {
                await ReplyAsync("", false, EmbedFactory.Join.JoinFailed(Context, channelName));
            }
        }