public async Task GetAndSetDevice()
        {
            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;

            var tableBuilder = new AsciiTableBuilder <Tuple <string, string, string> >(
                new Tuple <string, int>("Name", 15),
                new Tuple <string, int>("Type", 13),
                new Tuple <string, int>("ID", 40));

            var deviceInfo = devices
                             .Select(d => new Tuple <string, string, string>(d.Name, d.Type, d.Id))
                             .ToList();

            var tableString = tableBuilder.GetTableString(deviceInfo);

            var devicesString = $"Reply with just the name of the device you want to use:```{tableString}```";

            await ReplyAsync(devicesString);

            var userResponse = await NextMessageAsync();

            if (userResponse == null)
            {
                await ReplyAsync("You did not reply before the timeout");

                return;
            }
            else
            {
                var deviceCheck = devices.Find(d => d.Name == userResponse.Content);
                if (deviceCheck == null)
                {
                    await ReplyAsync($"{Context.User.Mention} device not found. Make sure you're entering the device name correctly.");

                    return;
                }
                var result = await _synthbotWebClient.SetDeviceId(Context.User.Id.ToString(), deviceCheck.Id);
                await ReplyAsync($"Success: {result}");
            }
        }
예제 #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));
            }
        }