Skip to content

Commit 2a739ed

Browse files
committed
Fixed docstrings, removed unnecessary async definitions for methods that didn't need to be coroutines, updated example
1 parent a935e80 commit 2a739ed

File tree

2 files changed

+57
-48
lines changed

2 files changed

+57
-48
lines changed

examples/music_queue.py

+9-14
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,16 @@
66
class Bot(commands.Bot):
77

88
def __init__(self):
9-
super().__init__(token="TOKEN", prefix="!", initial_channels=["CHANNEL"])
9+
super().__init__(token="TOKEN", prefix="!", initial_channels=["sockheadrps"])
1010
self.audio_manager = queuemanager.AudioQueueManager()
11-
12-
# Adding sound files paths to the queue for uses to choose from
13-
song_dict = {
14-
"song_one": "\\PATH\\TO\\FILE.mp3",
15-
"song_two": "\\PATH\\TO\\FILE.mp3",
16-
"song_three": "\\PATH\\TO\\FILE.mp3",
11+
self.song_dict = {
12+
"song_one": "C:\\PATH\\TO\\FILE.mp3",
13+
"song_two": "C:\\PATH\\TO\\FILE.mp3",
14+
"song_three": "C:\\PATH\\TO\\FILE.mp3",
1715
}
1816

1917
async def event_ready(self):
2018
loop = asyncio.get_event_loop()
21-
22-
# Start the queue loop
2319
self.task = loop.create_task(self.audio_manager.queue_loop())
2420

2521
@commands.command(name="sr")
@@ -31,22 +27,21 @@ async def addsound(self, ctx: commands.Context, sound: str):
3127
@commands.command(name="skip")
3228
async def skip(self, ctx: commands.Context):
3329
await ctx.send(f"Skipped the current sound. {self.audio_manager.current_sound}")
34-
await self.audio_manager.skip_audio()
30+
self.audio_manager.skip_audio()
3531

3632
@commands.command(name="pause")
3733
async def pause(self, ctx: commands.Context):
38-
await self.audio_manager.pause_audio()
34+
self.audio_manager.pause_audio()
3935

4036
@commands.command(name="resume")
4137
async def resume(self, ctx: commands.Context):
42-
await self.audio_manager.resume_audio()
38+
self.audio_manager.resume_audio()
4339

4440
@commands.command(name="queue")
4541
async def queue(self, ctx: commands.Context):
46-
queue_contents = await self.audio_manager.get_queue_contents()
42+
queue_contents = self.audio_manager.get_queue_contents()
4743
await ctx.send(f"Queue contents: {queue_contents}")
4844

49-
# Override close method to gracefully cancel the task
5045
async def close(self):
5146
self.task.cancel()
5247
await super().close()

twitchio/ext/sounds/queuemanager.py

+48-34
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,75 @@
11
import asyncio
22
from twitchio.ext import sounds
3+
from typing import Optional
34

45

56
class AudioQueueManager:
67
"""
78
Manages a queue of audio files to be played sequentially with optional repeat and pause functionalities.
89
9-
Attributes:
10-
queue (asyncio.Queue[str]): A queue to hold paths of audio files to be played.
11-
is_playing (bool): Indicates whether an audio file is currently being played.
12-
repeat_queue (bool): If True, adds the current playing audio file back to the queue after playing.
13-
queue_paused (bool): If True, pauses the processing of the queue.
14-
player (sounds.AudioPlayer): An instance of AudioPlayer to play audio files.
15-
current_sound (str): Path of the currently playing audio file.
10+
Attributes
11+
----------
12+
queue: asyncio.Queue[:class:`str`]
13+
A queue to hold paths of audio files to be played.
14+
is_playing: :class:`bool`
15+
Indicates whether an audio file is currently being played.
16+
repeat_queue: :class:`bool`
17+
If True, adds the current playing audio file back to the queue after playing.
18+
queue_paused: :class:`bool`
19+
If True, pauses the processing of the queue.
20+
player: :class:`sounds.AudioPlayer`
21+
An instance of AudioPlayer to play audio files.
22+
current_sound: :class:`str`
23+
Path of the currently playing audio file.
1624
"""
1725

18-
def __init__(self):
26+
def __init__(self, repeat_queue: Optional[bool] = True) -> None:
1927
"""
2028
Initializes an instance of AudioQueueManager with an empty queue and default settings.
29+
30+
Parameters
31+
----------
32+
repeat_queue: Optional[:class:`bool`]
33+
If True, adds the current playing audio file back to the queue after playing, by default True
2134
"""
2235
self.queue: asyncio.Queue[str] = asyncio.Queue()
2336
self.is_playing: bool = False
24-
self.repeat_queue: bool = True
37+
self.repeat_queue: bool = repeat_queue if repeat_queue is not None else True
2538
self.queue_paused: bool = False
26-
self.player: sounds.AudioPlayer = sounds.AudioPlayer(
27-
callback=self.player_done)
39+
self.player: sounds.AudioPlayer = sounds.AudioPlayer(callback=self.player_done)
2840
self.current_sound: str = ""
2941

3042
async def player_done(self) -> None:
3143
"""
44+
|coro|
45+
3246
Callback method called when the player finishes playing an audio file.
3347
Resets the is_playing flag and marks the current task as done in the queue.
3448
"""
35-
await asyncio.sleep(0.1)
3649
self.is_playing = False
3750
self.queue.task_done()
3851

3952
async def add_audio(self, sound_path: str) -> None:
4053
"""
54+
|coro|
55+
4156
Adds a new audio file to the queue.
4257
43-
Args:
44-
sound_path (str): Path of the audio file to add to the queue.
58+
Parameters
59+
----------
60+
sound_path: :class:`str`
61+
Path of the audio file to add to the queue.
4562
"""
46-
await asyncio.sleep(0.1)
4763
await self.queue.put(sound_path)
4864

4965
async def play_next(self) -> None:
5066
"""
67+
|coro|
68+
5169
Plays the next audio file in the queue if the queue is not empty and not paused.
5270
Sets the is_playing flag, retrieves the next audio file from the queue, and plays it.
5371
If repeat_queue is True, adds the current audio file back to the queue after playing.
5472
"""
55-
await asyncio.sleep(0.1)
5673
if not self.queue.empty() and not self.queue_paused:
5774
self.is_playing = True
5875
sound_path = await self.queue.get()
@@ -62,74 +79,71 @@ async def play_next(self) -> None:
6279
if self.repeat_queue:
6380
await self.queue.put(self.current_sound)
6481

65-
async def skip_audio(self) -> None:
82+
def skip_audio(self) -> None:
6683
"""
6784
Stops the currently playing audio file if there is one.
6885
"""
69-
await asyncio.sleep(0.1)
7086
if self.is_playing:
7187
self.player.stop()
7288
self.is_playing = False
7389

74-
async def stop_audio(self) -> None:
90+
def stop_audio(self) -> None:
7591
"""
7692
Stops the currently playing audio file.
7793
Resets the playing flag but leaves the queue intact.
7894
"""
79-
await asyncio.sleep(0.1)
8095
if self.is_playing:
8196
self.player.stop()
8297
self.is_playing = False
8398

84-
async def pause_audio(self) -> None:
99+
def pause_audio(self) -> None:
85100
"""
86101
Pauses the currently playing audio file.
87102
"""
88-
await asyncio.sleep(0.1)
89103
self.player.pause()
90104

91-
async def resume_audio(self) -> None:
105+
def resume_audio(self) -> None:
92106
"""
93107
Resumes the currently paused audio file.
94108
"""
95-
await asyncio.sleep(0.1)
96109
self.player.resume()
97110

98111
async def clear_queue(self) -> None:
99112
"""
113+
|coro|
114+
100115
Clears all audio files from the queue.
101116
"""
102-
await asyncio.sleep(0.1)
103117
while not self.queue.empty():
104118
await self.queue.get()
105119
self.queue.task_done()
106120

107-
async def pause_queue(self) -> None:
121+
def pause_queue(self) -> None:
108122
"""
109123
Pauses the processing of the queue.
110124
"""
111-
await asyncio.sleep(0.1)
112125
self.queue_paused = True
113126

114-
async def resume_queue(self) -> None:
127+
def resume_queue(self) -> None:
115128
"""
116129
Resumes the processing of the queue.
117130
"""
118-
await asyncio.sleep(0.1)
119131
self.queue_paused = False
120132

121-
async def get_queue_contents(self) -> list:
133+
def get_queue_contents(self) -> list[str]:
122134
"""
123-
Retrieves the current contents of the queue.
135+
Retrieves the current contents of the queue as a list.
124136
125-
Returns:
126-
list: List of paths of audio files in the queue.
137+
Returns
138+
-------
139+
List[:class:`str`]
127140
"""
128-
await asyncio.sleep(0.1)
129141
return list(self.queue._queue)
130142

131143
async def queue_loop(self) -> None:
132144
"""
145+
|coro|
146+
133147
Continuously checks the queue and plays the next audio file if not currently playing and not paused.
134148
"""
135149
try:

0 commit comments

Comments
 (0)