Skip to content

Commit c300056

Browse files
authored
Merge pull request #86 from standleypg-dev/Feat/Rewind
Added simple rewind mechanism
2 parents 9fe2d8a + 581cfd0 commit c300056

3 files changed

Lines changed: 46 additions & 0 deletions

File tree

src/Application/Interfaces/Services/IMusicQueueService.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,6 @@ public interface IMusicQueueService
99
void DequeueAsync(CancellationToken cancellationToken);
1010
int Count { get; }
1111
PlayRequest[] GetAllRequests();
12+
void Rewind();
1213
void Clear();
1314
}

src/Infrastructure/Commands/MusicActionCommands.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,27 @@ await executor.ExecuteAsync(async serviceProvider =>
7777
}
7878
}
7979

80+
[SlashCommand("rewind", "Rewind the current track")]
81+
public async Task Rewind()
82+
{
83+
if (await CommandUtils.NotInVoiceChannel(Context, (message) => RespondAsync(message)))
84+
{
85+
return;
86+
}
87+
88+
InteractionMessageProperties message;
89+
if(queue.Count == 0)
90+
{
91+
message = CommandUtils.CreateMessage<InteractionMessageProperties>("No songs in queue.");
92+
await RespondAsync(InteractionCallback.Message(message));
93+
return;
94+
}
95+
96+
queue.Rewind();
97+
message = CommandUtils.CreateMessage<InteractionMessageProperties>("Rewinding the current track.");
98+
await RespondAsync(InteractionCallback.Message(message));
99+
}
100+
80101
[SlashCommand("statistics", "Show some statistics")]
81102
public async Task Statistics()
82103
{

src/Infrastructure/Services/MusicQueueService.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,30 @@ public PlayRequest[] GetAllRequests()
7474
}
7575
}
7676

77+
public void Rewind()
78+
{
79+
lock (_lock)
80+
{
81+
if (_queue.Count > 1)
82+
{
83+
var current = _queue.Peek();
84+
var list = _queue.ToList();
85+
list.Reverse();
86+
list.Add(current);
87+
list.Reverse();
88+
_queue.Clear();
89+
foreach (var item in list)
90+
{
91+
_queue.Enqueue(item);
92+
}
93+
}
94+
else
95+
{
96+
logger.LogWarning("Attempted to rewind with less than two items in the queue.");
97+
}
98+
}
99+
}
100+
77101
public void Clear()
78102
{
79103
lock (_lock)

0 commit comments

Comments
 (0)