Skip to content

Commit 8c87ec1

Browse files
committed
Merge branch 'bot-dev'
2 parents d5816c4 + 1b71bc4 commit 8c87ec1

File tree

4 files changed

+95
-26
lines changed

4 files changed

+95
-26
lines changed

cogs/voice/voice.py

Lines changed: 70 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
import discord
2+
3+
from discord import Reaction, User, Message
24
from discord.ext import commands
35

46
from cogs.voice.voice_fun import bot_audible_update
5-
from cogs.voice.voice_fun import create_playing_embed
67
from cogs.voice.voice_fun import create_queue_embed
78
from cogs.voice.voice_fun import droid_speak_translate
89
from cogs.voice.voice_fun import format_duration
910
from cogs.voice.voice_fun import play_queue
1011
from cogs.voice.voice_fun import add_queue
1112
from cogs.voice.voice_fun import YTDLSource
13+
from cogs.voice.voice_fun import setup_player
1214

1315

1416
class Music(commands.Cog):
@@ -116,6 +118,31 @@ async def volume(self, ctx, *volume: int):
116118

117119
return await ctx.send("Current volume is {}%".format(bot_volume))
118120

121+
@commands.command()
122+
async def flick(self, ctx):
123+
"""Switch between paused and playing states
124+
125+
:param ctx: command invocation message context
126+
:return: None
127+
"""
128+
if ctx.voice_client.is_playing():
129+
ctx.voice_client.pause()
130+
elif ctx.voice_client.is_paused():
131+
ctx.voice_client.resume()
132+
133+
@classmethod
134+
async def _react_flick(cls, reaction):
135+
"""Switch between paused and playing states
136+
137+
:param reaction: Reaction object invoking flick action
138+
:return: None
139+
"""
140+
guild = reaction.message.guild
141+
if guild.voice_client.is_playing():
142+
guild.voice_client.pause()
143+
elif guild.voice_client.is_paused():
144+
guild.voice_client.resume()
145+
119146
# Pause current audio stream
120147
@commands.command()
121148
async def pause(self, ctx):
@@ -162,6 +189,12 @@ async def skip(self, ctx, *queue_id):
162189
await ctx.send("Skipping:\t{}".format(player_title))
163190
ctx.voice_client.stop()
164191

192+
async def _react_skip(self, reaction):
193+
guild_id = reaction.message.guild.id
194+
player_title = self.queues[guild_id][0].title
195+
await reaction.message.channel.send("Skipping:\t{}".format(player_title))
196+
reaction.message.guild.voice_client.stop()
197+
165198
# Display information about current audio being played
166199
@commands.command()
167200
async def player(self, ctx):
@@ -175,8 +208,7 @@ async def player(self, ctx):
175208
if queue:
176209
source = queue[0]
177210
status = "Paused" if ctx.voice_client.is_paused() else "Playing"
178-
embed_playing = create_playing_embed(source, status)
179-
await ctx.send(embed=embed_playing)
211+
await setup_player(ctx, source)
180212

181213
# View the queue of the existing playlist
182214
@commands.command()
@@ -218,7 +250,8 @@ async def queue(self, ctx, first=5):
218250
embed_queue.add_field(name=f"Queue (Displaying first {first})",
219251
value=queue_string,
220252
inline=False)
221-
await ctx.send(embed=embed_queue)
253+
ret_msg: Message = await ctx.send(embed=embed_queue)
254+
await ret_msg.delete(delay=10)
222255

223256
@commands.command()
224257
async def speak(self, ctx, *phrase: str):
@@ -230,6 +263,39 @@ async def speak(self, ctx, *phrase: str):
230263
if not ctx.voice_client.is_playing() or ctx.voice_client.is_paused():
231264
await droid_speak_translate(ctx, phrase)
232265

266+
@staticmethod
267+
def _is_not_bot(user):
268+
return not user.bot
269+
270+
@commands.Cog.listener()
271+
async def on_reaction_add(self, reaction: Reaction, user: User) -> None:
272+
"""Listen for all added reactions
273+
274+
:param reaction: Reaction that was submitted by a User
275+
:param user: The User that submitted the Reaction
276+
:return:
277+
"""
278+
if self._is_not_bot(user):
279+
await self.manage_music(reaction, user)
280+
281+
async def manage_music(self, reaction: Reaction, user: User) -> None:
282+
"""For a given Reaction, determine if music player needs to be managed
283+
284+
:param reaction: a Reaction object
285+
:param user: the User who sent the Reaction
286+
:return:
287+
"""
288+
if reaction.message.author.bot: # This loosely rules out reactions to non-bot messages messages.
289+
if str(reaction) == "⏭️":
290+
await self._react_skip(reaction)
291+
await reaction.message.clear_reactions()
292+
await reaction.message.delete(delay=10)
293+
elif str(reaction) == "⏯️":
294+
await self._react_flick(reaction)
295+
await reaction.remove(user)
296+
else:
297+
pass
298+
233299
@stream.before_invoke
234300
@speak.before_invoke
235301
async def ensure_voice(self, ctx):

cogs/voice/voice_fun.py

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import asyncio
2+
3+
from discord import Message
24
from emoji import demojize
35
import googleapiclient.discovery
46
import random
@@ -41,8 +43,6 @@ async def get_info(cls, url):
4143
"""Stream audio from a supplied url instead of searching
4244
4345
:param url: supplied URL
44-
:param loop: video looping
45-
:param stream: determine if URL is a stream
4646
:return:
4747
"""
4848

@@ -156,13 +156,21 @@ async def add_queue(music, ctx, source: YTDLSource):
156156

157157
if len(source.videos) == 1:
158158
embed = single_queue_embed(source.videos[0], len(music.queues[guild_id]))
159-
await ctx.send(embed=embed)
159+
ret_msg: Message = await ctx.send(embed=embed)
160+
await ret_msg.delete(delay=10)
160161
else:
161162
embed = playlist_queue_embed(source)
162-
await ctx.send(embed=embed)
163+
ret_msg: Message = await ctx.send(embed=embed)
164+
await ret_msg.delete(delay=10)
163165
return
164166

165167

168+
async def setup_player(ctx, source):
169+
msg = await ctx.send(embed=create_playing_embed(source, "Playing"))
170+
await msg.add_reaction("⏯️")
171+
await msg.add_reaction("⏭️")
172+
173+
166174
# Queue player
167175
def play_queue(music, ctx):
168176
"""Setup and manage the music player
@@ -182,8 +190,7 @@ def play_queue(music, ctx):
182190
tmp = source.extract_audio()
183191
audio = discord.PCMVolumeTransformer(tmp, volume=music.cur_volume)
184192
music.players[guild_id] = audio
185-
asyncio.run_coroutine_threadsafe(ctx.send(embed=create_playing_embed(source, "Playing")),
186-
loop=music.bot.loop)
193+
asyncio.run_coroutine_threadsafe(setup_player(ctx, source), loop=music.bot.loop)
187194
play_audio(music, audio, ctx)
188195
else:
189196
pass

mksbot_main.py

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,14 @@
22
import sys
33
import traceback
44
import yaml
5+
import discord
56

67
from cogs.reddit.reddit import Reddit
78
from cogs.amusement.amusement import Amusement
89
from cogs.voice.voice import Music
910
from discord.ext import commands
1011

11-
12-
from base_functions import *
13-
14-
#if not discord.opus.is_loaded():
15-
# discord.opus.load_opus('opus')
12+
import base_functions as mks
1613

1714
# Import config data and extensions (cogs)
1815
config = yaml.safe_load(open("./config.yml"))
@@ -23,7 +20,7 @@
2320
cogs = [Reddit(bot),
2421
Amusement(bot),
2522
Music(bot)]
26-
23+
2724
if __name__ == '__main__':
2825
for extension in cogs:
2926
try:
@@ -68,7 +65,7 @@ async def info(ctx):
6865
async def user(ctx, *, member: discord.Member):
6966
"""Get user information
7067
"""
71-
await ctx.send(embed=user_info_embed(member))
68+
await ctx.send(embed=mks.user_info_embed(member))
7269

7370

7471
@bot.command(pass_context=True)
@@ -89,7 +86,7 @@ async def rm(ctx, *number):
8986
if n > 0:
9087
limit = n
9188

92-
except ValueError as e:
89+
except ValueError:
9390
if number[0] == "purge":
9491
limit = None
9592

@@ -135,8 +132,8 @@ async def help(ctx, *args):
135132
await ctx.send(embed=embed)
136133

137134
else:
138-
c_group = []
139-
just_c = []
135+
c_group = [group for _ in config["help"] for group in config["help"]]
136+
just_c = [c in group for group in config["help"] for c in config["help"][group]]
140137
command = args[0].replace("!", "")
141138
[[c_group.append(group) for c in config["help"][group]] for group in config["help"]]
142139
[[just_c.append(c) for c in config["help"][group]] for group in config["help"]]

requirements.txt

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
google-api-python-client==1.8.0
2-
praw==6.5.1
3-
requests==2.23.0
4-
discord.py==1.3.3
5-
youtube_dl==2020.7.28
6-
emoji==0.5.4
1+
requests==2.24.0
72
discord==1.0.1
3+
emoji==0.6.0
4+
google_api_python_client==1.10.0
5+
praw==7.1.0
86
PyYAML==5.3.1
7+
youtube_dl==2020.7.28

0 commit comments

Comments
 (0)