-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplayer.py
147 lines (120 loc) · 4.41 KB
/
player.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import asyncio
from os import removedirs
import discord
class Player(object):
def __init__(self, queue):
self.queue = queue
self.skip = False
self.voice_client = None
self._loop = False
async def start(self):
while True:
song = await self.queue.get()
self.voice_client = song.ctx.voice_client
self.voice_client.stop() if self.voice_client.is_playing(
) or self.voice_client.is_paused() else None
embed = discord.Embed(
title="Now playing",
description=song.title,
color=0x00DAFF
).add_field(name="Requested by", value=song.ctx.author.mention)
await song.ctx.send(
embed=embed
)
self.voice_client.play(discord.FFmpegPCMAudio(source=song.source))
while self.voice_client.is_playing() or self.voice_client.is_paused():
if self.skip:
self.skip = False
break
await asyncio.sleep(0.5)
if self._loop:
await self.queue.put(song)
async def pause(self, ctx):
if self.voice_client.is_playing():
self.voice_client.pause()
await ctx.message.add_reaction('\U000023F8')
async def resume(self, ctx):
if not self.voice_client.is_playing():
self.voice_client.resume()
await ctx.message.add_reaction('\U000025B6')
else:
await ctx.send("Nothing is being played")
async def disconnect(self, ctx):
# If bot is connected to the voice channel
# the it disconnects and returns True, otherwise
# returns False
if self.voice_client.is_connected():
for _ in range(self.queue.qsize()):
self.queue.get_nowait()
await self.voice_client.disconnect()
await ctx.message.add_reaction('\U0001F50C')
else:
await ctx.send("Porygon is not connnected")
async def next(self, ctx):
self.skip = True
await ctx.message.add_reaction('\U000023ED')
async def enqueue(self, item):
await self.queue.put(item)
def get_queue_length(self):
q_size = self.queue.qsize()
if self.voice_client:
return q_size + 1 if self.voice_client.is_playing() else q_size
else:
return q_size
async def loop_queue(self, ctx):
# Toggles queue looping.
self._loop = not self._loop
title = "Now looping." if self._loop else "Looping now disabled."
embed = discord.Embed(
title=title,
color=0x00DAFF
)
await ctx.message.add_reaction("\U0001F501")
await ctx.send(embed=embed)
async def queue_list(self, ctx):
song_names = ''
for index in range(self.queue.qsize()):
song = await self.queue.get()
song_names += f"{index+1}. {song.title}\n"
await self.queue.put(song)
embed = discord.Embed(
title="Queue",
description=song_names,
color=0x00DAFF
)
await ctx.message.add_reaction("\U0001F4C3")
await ctx.send(embed=embed)
async def dequeue(self, ctx, title):
for _ in range(self.queue.qsize()):
song = await self.queue.get()
if song.title != title:
await self.queue.put(song)
else:
embed = discord.Embed(
title="Song removed",
description=title,
color=0xFF0017
)
await ctx.send(embed=embed)
async def clear_queue(self, ctx):
for _ in range(self.queue.qsize()):
self.queue.get_nowait()
embed = discord.Embed(
title="Queue cleared",
description='All songs removed',
color=0xFF0017
)
await ctx.send(embed=embed)
async def skipto(self, ctx, title):
songs = asyncio.Queue()
for _ in range(self.queue.qsize()):
song = await self.queue.get()
if song.title == title:
await self.queue.put(song)
else:
await songs.put(song)
for __ in range(songs.qsize()):
item = await songs.get()
await self.queue.put(item)
self.skip=True
await ctx.message.add_reaction("\U0001F44D")