コード例 #1
0
        public async Task PlayMusic(int ownerid, int startindex = 0, bool forceupdate = false)
        {
            Audio[] audio = null;

            if (forceupdate)
            {
                await Context.Channel.SendMessageAsync("Trying to force update playlist...");
                await UploadPlaylist(ownerid);
            }
            else
            {
                await UploadPlaylistSilent(ownerid);
            }

            audio = Program._data.RestoreObject <Audio[]>($"{Context.Guild.Id}_{ownerid}.dat");

            var channelId = (Context.User as IGuildUser)?.VoiceChannel.Id;
            var path      = $@"{Directory.GetCurrentDirectory()}\servers\{Context.Guild.Id}\music.mp3";

            await StopMusic(null, false);
            await JoinChannel();

            var          msg    = Context.Channel.SendMessageAsync("", false, new EmbedBuilder().Build());
            IAudioClient client = Program._data.RestoreObject <IAudioClient>($"{channelId}");

            Program._data.StoreObject($"{Context.Guild.Id}.aos", client.CreatePCMStream(AudioApplication.Music));
            for (int i = startindex; i < audio.Length; ++i)
            {
                if (audio[i].ContentRestricted != null)
                {
                    await Context.Channel.SendMessageAsync($"{audio[i].Title} is restricted");

                    continue;
                }

                CancellationTokenSource tokenSource = new CancellationTokenSource();
                Program._data.StoreObject($"{Context.Guild.Id}.cts", tokenSource);
                VKMusic.DownloadSongs(audio[i], path).Wait();
                try
                {
                    var vkuser = Program._vkApi.Users.Get(new long[] { ownerid }, VkNet.Enums.Filters.ProfileFields.Photo200).FirstOrDefault();

                    Task counter = SongCounter(msg.Result, audio[i], vkuser, i, tokenSource.Token);
                    Task sending = SendAsync(path);
                    await Task.WhenAny(new Task[] { sending, counter });

                    if (tokenSource.IsCancellationRequested)
                    {
                        break;
                    }
                    tokenSource.Cancel();
                    Console.WriteLine(new LogMessage(LogSeverity.Info, "BOT", "Token cancelled"));
                }
                catch (Exception e)
                {
                    await Context.Channel.SendMessageAsync(e.Message);
                }
            }
        }
コード例 #2
0
        public async Task UploadPlaylist(int ownerid)
        {
            Directory.CreateDirectory($@"{Directory.GetCurrentDirectory()}\servers\{Context.Guild.Id}");

            await VKMusic.GetPlaylistInFile(Program._vkApi, ownerid, Context.Guild.Id);

            using (FileStream fs = new FileStream($@"{Directory.GetCurrentDirectory()}\servers\{Context.Guild.Id}\{ownerid}.dat", FileMode.OpenOrCreate, FileAccess.Read))
            {
                Program._data.StoreObject($"{Context.Guild.Id}_{ownerid}.dat", (Audio[])formatter.Deserialize(fs));
            }

            await Context.Channel.SendMessageAsync("Playlist downloaded successful");

            await Task.CompletedTask;
        }