Skip to content

Commit

Permalink
bug fixing and refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
GREBIAR-Git committed Feb 29, 2024
1 parent 9e1d584 commit b6d0034
Show file tree
Hide file tree
Showing 14 changed files with 232 additions and 170 deletions.
16 changes: 9 additions & 7 deletions Musician/Musician/Audio/AudioClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,27 @@ namespace Musician.Audio;

public static class AudioClient
{
public readonly static Dictionary<ulong, Channel> channels = [];
public static readonly Dictionary<ulong, Channel> Channels = [];

public static async Task UserVoiceStateUpdated(SocketUser user, SocketVoiceState oldState, SocketVoiceState newState)
public static Task UserVoiceStateUpdated(SocketUser user, SocketVoiceState oldState,
SocketVoiceState newState)
{
if (user.IsBot)
{

if (oldState.VoiceChannel != null && newState.VoiceChannel == null)
{
channels.Remove(oldState.VoiceChannel.Id);
Channels.Remove(oldState.VoiceChannel.Id);
}
}

return Task.CompletedTask;
}

public static async Task<Channel> Connect(IVoiceChannel voiceChannel)
{
IAudioClient audioClient = await voiceChannel.ConnectAsync(true, false, false);
IAudioClient audioClient = await voiceChannel.ConnectAsync(true);
Channel channel = new(audioClient);
channels.Add(voiceChannel.Id, channel);
Channels.Add(voiceChannel.Id, channel);
return channel;
}
}
}
35 changes: 18 additions & 17 deletions Musician/Musician/Audio/Channel.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using Discord.Audio;
using YoutubeExplode;
using YoutubeExplode.Common;
using YoutubeExplode.Search;
using YoutubeExplode.Videos;
using YoutubeExplode.Videos.Streams;

namespace Musician.Audio;
Expand All @@ -11,18 +13,19 @@ public class Channel(IAudioClient audioClient)

public bool IsPause { get; set; }

public CancellationTokenSource CancellationTokenSource { get; set; } = new CancellationTokenSource();
public bool IsSkip { get; set; }

public List<YoutubeInfo> Queue { get; set; } = [];

public void AllStop()
{
Queue.Clear();
CancellationTokenSource.Cancel();
IsSkip = true;
}

public void Skip()
{
CancellationTokenSource.Cancel();
IsSkip = true;
}

public void Pause()
Expand All @@ -35,25 +38,22 @@ public void Resume()
IsPause = false;
}


public List<YoutubeInfo> Queue { get; set; } = [];

public async Task AddAudioInQueue(string request)
{
var youtube = new YoutubeClient();
YoutubeClient youtube = new();

var videos = await youtube.Search.GetVideosAsync(request);
IReadOnlyList<VideoSearchResult> videos = await youtube.Search.GetVideosAsync(request);

string url = videos[0].Url;

var video = await youtube.Videos.GetAsync(url);
Video video = await youtube.Videos.GetAsync(url);

var streamManifest = await youtube.Videos.Streams.GetManifestAsync(url);
StreamManifest streamManifest = await youtube.Videos.Streams.GetManifestAsync(url);

var audioStreamInfo = streamManifest.GetAudioStreams()
.Where(s => s.Container == Container.Mp4)
.GetWithHighestBitrate();
var stream = await youtube.Videos.Streams.GetAsync(audioStreamInfo);
IStreamInfo audioStreamInfo = streamManifest.GetAudioStreams()
.Where(s => s.Container == Container.Mp4)
.GetWithHighestBitrate();
Stream stream = await youtube.Videos.Streams.GetAsync(audioStreamInfo);

Queue.Add(new YoutubeInfo(video, stream));
}
Expand All @@ -69,7 +69,7 @@ public void SongIsOver()

public YoutubeInfo? GetCurrentAudio()
{
return Queue.FirstOrDefault(); ;
return Queue.FirstOrDefault();
}

public string GetQueueString()
Expand All @@ -79,13 +79,14 @@ public string GetQueueString()
{
for (int i = 0; i < Queue.Count; i++)
{
queue += (i + 1) + " «" + Queue[i].Video.Title.ToString() + "» Время: " + Queue[i].Video.Duration.ToString() + "\n";
queue += i + 1 + " «" + Queue[i].Video.Title + "» Время: " + Queue[i].Video.Duration + "\n";
}
}
else
{
queue = "Очередь пуста";
}

return queue;
}
}
}
24 changes: 13 additions & 11 deletions Musician/Musician/Audio/YouTubeAutocompleteHandler.cs
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
using Discord;
using Discord.Interactions;
using YoutubeExplode;
using YoutubeExplode.Search;

namespace Musician.Audio
namespace Musician.Audio;

public class YouTubeAutocompleteHandler : AutocompleteHandler
{
public class YouTubeAutocompleteHandler : AutocompleteHandler
public override Task<AutocompletionResult> GenerateSuggestionsAsync(IInteractionContext context,
IAutocompleteInteraction autocompleteInteraction, IParameterInfo parameter, IServiceProvider services)
{
public override async Task<AutocompletionResult> GenerateSuggestionsAsync(IInteractionContext context, IAutocompleteInteraction autocompleteInteraction, IParameterInfo parameter, IServiceProvider services)
{
var input = autocompleteInteraction.Data.Current.Value as string;
var youtube = new YoutubeClient();
var videos = youtube.Search.GetVideosAsync(input);
string? input = autocompleteInteraction.Data.Current.Value as string;
YoutubeClient youtube = new();
IAsyncEnumerable<VideoSearchResult> videos = youtube.Search.GetVideosAsync(input);

var autocompleteResults = videos.Take(5).Select(video => new AutocompleteResult(video.Title, video.Url));
IAsyncEnumerable<AutocompleteResult> autocompleteResults =
videos.Take(5).Select(video => new AutocompleteResult(video.Title, video.Url));

return AutocompletionResult.FromSuccess(autocompleteResults.ToEnumerable());
}
return Task.FromResult(AutocompletionResult.FromSuccess(autocompleteResults.ToEnumerable()));
}
}
}
17 changes: 6 additions & 11 deletions Musician/Musician/Audio/YoutubeInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@ namespace Musician.Audio;

public class YoutubeInfo(Video video, Stream stream)
{
public Video Video = video;

public Stream Stream = stream;
public Video Video = video;

public long CurrentStreamPos { get; set; }

Expand All @@ -20,15 +19,11 @@ public string CurrentTime
{
return "0";
}
return new TimeSpan(0, 0, (int)Math.Floor(CurrentStreamPos / (AllStreamPos / Video.Duration.Value.TotalSeconds))).ToString();
}
}

public string Info
{
get
{
return "Трек: " + Video.Title + "\nДлительность: " + Video.Duration;
return new TimeSpan(0, 0,
(int)Math.Floor(CurrentStreamPos / (AllStreamPos / Video.Duration.Value.TotalSeconds))).ToString();
}
}
}

public string Info => "Трек: " + Video.Title + "\nДлительность: " + Video.Duration;
}
Loading

0 comments on commit b6d0034

Please sign in to comment.