2017-02-13 15:25:56 +13:00
|
|
|
from .voice_utilities import *
|
2017-04-20 15:18:05 +12:00
|
|
|
from discord import FFmpegPCMAudio, PCMVolumeTransformer
|
2016-10-30 09:23:12 +13:00
|
|
|
|
2016-07-08 01:05:42 +12:00
|
|
|
import discord
|
|
|
|
from discord.ext import commands
|
2016-10-30 09:23:12 +13:00
|
|
|
|
2017-02-13 12:45:19 +13:00
|
|
|
from . import utils
|
|
|
|
|
2016-08-20 16:00:23 +12:00
|
|
|
import math
|
2016-10-30 09:23:12 +13:00
|
|
|
import asyncio
|
2017-02-28 14:02:18 +13:00
|
|
|
import inspect
|
2017-04-20 15:18:05 +12:00
|
|
|
import time
|
2017-04-22 07:45:20 +12:00
|
|
|
import re
|
2016-07-08 01:05:42 +12:00
|
|
|
|
|
|
|
if not discord.opus.is_loaded():
|
|
|
|
discord.opus.load_opus('/usr/lib64/libopus.so.0')
|
2016-07-31 12:20:55 +12:00
|
|
|
|
2016-12-22 19:56:25 +13:00
|
|
|
|
2016-07-08 01:05:42 +12:00
|
|
|
class VoiceState:
|
2017-04-20 15:18:05 +12:00
|
|
|
def __init__(self, guild, bot):
|
|
|
|
self.guild = guild
|
2016-12-22 12:06:32 +13:00
|
|
|
self.songs = Playlist(bot)
|
2017-04-20 15:18:05 +12:00
|
|
|
self.current = None
|
2016-08-20 16:00:23 +12:00
|
|
|
self.required_skips = 0
|
2016-08-17 03:22:32 +12:00
|
|
|
self.skip_votes = set()
|
2017-04-20 15:18:05 +12:00
|
|
|
self.audio_player = bot.loop.create_task(self.audio_player_task())
|
2016-08-21 17:36:38 +12:00
|
|
|
|
2017-04-20 15:18:05 +12:00
|
|
|
@property
|
|
|
|
def voice(self):
|
|
|
|
return self.guild.voice_client
|
2016-07-08 01:05:42 +12:00
|
|
|
|
|
|
|
@property
|
2017-04-20 15:18:05 +12:00
|
|
|
def playing(self):
|
2017-04-24 10:28:13 +12:00
|
|
|
if self.voice is None:
|
|
|
|
return False
|
|
|
|
else:
|
|
|
|
return self.voice.is_playing() or self.voice.is_paused()
|
2016-07-08 01:05:42 +12:00
|
|
|
|
|
|
|
def skip(self):
|
|
|
|
self.skip_votes.clear()
|
2017-04-20 15:18:05 +12:00
|
|
|
if self.playing:
|
|
|
|
self.voice.stop()
|
2016-07-08 01:05:42 +12:00
|
|
|
|
2017-04-20 15:18:05 +12:00
|
|
|
def after(self):
|
|
|
|
self.current = None
|
2016-07-08 01:05:42 +12:00
|
|
|
|
|
|
|
async def audio_player_task(self):
|
|
|
|
while True:
|
2017-04-20 15:18:05 +12:00
|
|
|
if self.playing or self.songs.peek() is None:
|
2016-12-22 12:06:32 +13:00
|
|
|
await asyncio.sleep(1)
|
2017-04-20 15:18:05 +12:00
|
|
|
continue
|
2016-12-22 12:06:32 +13:00
|
|
|
|
2017-04-20 15:18:05 +12:00
|
|
|
self.current = await self.songs.get_next_entry()
|
2017-02-07 11:34:06 +13:00
|
|
|
|
2017-04-20 15:18:05 +12:00
|
|
|
source = FFmpegPCMAudio(
|
2016-12-22 19:56:25 +13:00
|
|
|
self.current.filename,
|
2017-04-20 15:18:05 +12:00
|
|
|
before_options='-nostdin',
|
|
|
|
options='-vn -b:a 128k'
|
2016-12-22 19:56:25 +13:00
|
|
|
)
|
2017-04-20 15:18:05 +12:00
|
|
|
source = PCMVolumeTransformer(source)
|
|
|
|
self.voice.play(source, after=self.after)
|
2016-10-30 09:23:12 +13:00
|
|
|
self.current.start_time = time.time()
|
|
|
|
|
2016-07-09 13:27:19 +12:00
|
|
|
|
2016-07-08 01:05:42 +12:00
|
|
|
class Music:
|
|
|
|
"""Voice related commands.
|
|
|
|
Works in multiple servers at once.
|
|
|
|
"""
|
2016-07-31 12:20:55 +12:00
|
|
|
|
2016-07-08 01:05:42 +12:00
|
|
|
def __init__(self, bot):
|
|
|
|
self.bot = bot
|
|
|
|
self.voice_states = {}
|
2016-12-22 12:06:32 +13:00
|
|
|
down = Downloader(download_folder='audio_tmp')
|
|
|
|
self.downloader = down
|
|
|
|
self.bot.downloader = down
|
2016-07-08 01:05:42 +12:00
|
|
|
|
|
|
|
def __unload(self):
|
2016-08-16 15:30:52 +12:00
|
|
|
# If this is unloaded, cancel all players and disconnect from all channels
|
2016-07-08 01:05:42 +12:00
|
|
|
for state in self.voice_states.values():
|
|
|
|
try:
|
|
|
|
state.audio_player.cancel()
|
|
|
|
if state.voice:
|
|
|
|
self.bot.loop.create_task(state.voice.disconnect())
|
|
|
|
except:
|
|
|
|
pass
|
|
|
|
|
2017-02-07 15:56:04 +13:00
|
|
|
async def queue_embed_task(self, state, channel, author):
|
|
|
|
index = 0
|
|
|
|
message = None
|
|
|
|
fmt = None
|
|
|
|
possible_reactions = ['\u27A1', '\u2B05', '\u2b06', '\u2b07', '\u274c']
|
2017-04-20 15:18:05 +12:00
|
|
|
|
|
|
|
# Our check to ensure the only one who reacts is the bot
|
|
|
|
def check(react, u):
|
|
|
|
if message is None:
|
|
|
|
return False
|
|
|
|
elif react.message.id != message.id:
|
|
|
|
return False
|
|
|
|
elif react.emoji not in possible_reactions:
|
|
|
|
return False
|
|
|
|
else:
|
|
|
|
return u.id == author.id
|
|
|
|
|
2017-02-07 15:56:04 +13:00
|
|
|
while True:
|
|
|
|
# Get the current queue (It might change while we're doing this)
|
|
|
|
# So do this in the while loop
|
|
|
|
queue = state.songs.entries
|
|
|
|
count = len(queue)
|
|
|
|
# This means the last song was removed
|
|
|
|
if count == 0:
|
2017-04-20 15:18:05 +12:00
|
|
|
await channel.send("Nothing currently in the queue")
|
2017-02-07 15:56:04 +13:00
|
|
|
break
|
|
|
|
# Get the current entry
|
|
|
|
entry = queue[index]
|
|
|
|
# Get the entry's embed
|
|
|
|
embed = entry.to_embed()
|
|
|
|
# Set the embed's title to indicate the amount of things in the queue
|
|
|
|
count = len(queue)
|
2017-04-20 15:18:05 +12:00
|
|
|
embed.title = "Current Queue [{}/{}]".format(index + 1, count)
|
2017-02-07 15:56:04 +13:00
|
|
|
# Now we need to send the embed, so check if the message is already set
|
|
|
|
# If not, then we need to send a new one (i.e. this is the first time called)
|
|
|
|
if message:
|
2017-04-20 15:18:05 +12:00
|
|
|
await message.edit(content=fmt, embed=embed)
|
2017-02-07 15:56:04 +13:00
|
|
|
# There's only one reaction we want to make sure we remove in the circumstances
|
|
|
|
# If the member doesn't have kick_members permissions, and isn't the requester
|
|
|
|
# Then they can't remove the song, otherwise they can
|
2017-04-20 15:18:05 +12:00
|
|
|
if not author.guild_permissions.kick_members and author.id != entry.requester.id:
|
2017-02-07 15:56:04 +13:00
|
|
|
try:
|
2017-04-20 15:18:05 +12:00
|
|
|
await message.remove_reaction('\u274c', channel.server.me)
|
2017-02-07 15:56:04 +13:00
|
|
|
except:
|
|
|
|
pass
|
2017-04-20 15:18:05 +12:00
|
|
|
elif not author.guild_permissions.kick_members and author.id == entry.requester.id:
|
2017-02-07 15:56:04 +13:00
|
|
|
try:
|
2017-04-20 15:18:05 +12:00
|
|
|
await message.add_reaction('\u274c')
|
2017-02-07 15:56:04 +13:00
|
|
|
except:
|
|
|
|
pass
|
|
|
|
else:
|
2017-04-20 15:18:05 +12:00
|
|
|
message = await channel.send(embed=embed)
|
|
|
|
await message.add_reaction('\N{LEFTWARDS BLACK ARROW}')
|
|
|
|
await message.add_reaction('\N{BLACK RIGHTWARDS ARROW}')
|
2017-02-07 15:56:04 +13:00
|
|
|
# The moderation tools that can be used
|
2017-03-08 12:56:24 +13:00
|
|
|
if author.guild_permissions.kick_members:
|
2017-04-20 15:18:05 +12:00
|
|
|
await message.add_reaction('\N{DOWNWARDS BLACK ARROW}')
|
|
|
|
await message.add_reaction('\N{UPWARDS BLACK ARROW}')
|
|
|
|
await message.add_reaction('\N{CROSS MARK}')
|
2017-02-07 15:56:04 +13:00
|
|
|
elif author == entry.requester:
|
2017-04-20 15:18:05 +12:00
|
|
|
await message.add_reaction('\N{CROSS MARK}')
|
2017-02-07 15:56:04 +13:00
|
|
|
# Reset the fmt message
|
2017-04-20 15:18:05 +12:00
|
|
|
fmt = "\u200B"
|
2017-02-07 15:56:04 +13:00
|
|
|
# Now we wait for the next reaction
|
2017-04-20 15:18:05 +12:00
|
|
|
try:
|
|
|
|
reaction, user = await self.bot.wait_for('reaction_add', check=check, timeout=180)
|
|
|
|
except asyncio.TimeoutError:
|
2017-02-07 15:56:04 +13:00
|
|
|
break
|
|
|
|
# Now we can prepare for the next embed to be sent
|
|
|
|
# If right is clicked
|
|
|
|
if '\u27A1' in reaction.emoji:
|
|
|
|
index += 1
|
|
|
|
if index >= count:
|
|
|
|
index = 0
|
|
|
|
# If left is clicked
|
|
|
|
elif '\u2B05' in reaction.emoji:
|
|
|
|
index -= 1
|
|
|
|
if index < 0:
|
|
|
|
index = count - 1
|
|
|
|
# If up is clicked
|
|
|
|
elif '\u2b06' in reaction.emoji:
|
|
|
|
# A second check just to make sure, as well as ensuring index is higher than 0
|
2017-03-08 12:56:24 +13:00
|
|
|
if author.guild_permissions.kick_members and index > 0:
|
2017-02-07 15:56:04 +13:00
|
|
|
if entry != queue[index]:
|
|
|
|
fmt = "`Error: Position of this entry has changed, cannot complete your action`"
|
|
|
|
else:
|
|
|
|
# Remove the current entry
|
|
|
|
del queue[index]
|
|
|
|
# Add it one position higher
|
|
|
|
queue.insert(index - 1, entry)
|
|
|
|
# Lets move the index to look at the new place of the entry
|
|
|
|
index -= 1
|
|
|
|
# If down is clicked
|
|
|
|
elif '\u2b07' in reaction.emoji:
|
|
|
|
# A second check just to make sure, as well as ensuring index is lower than last
|
2017-03-08 12:56:24 +13:00
|
|
|
if author.guild_permissions.kick_members and index < (count - 1):
|
2017-02-07 15:56:04 +13:00
|
|
|
if entry != queue[index]:
|
|
|
|
fmt = "`Error: Position of this entry has changed, cannot complete your action`"
|
|
|
|
else:
|
|
|
|
# Remove the current entry
|
|
|
|
del queue[index]
|
|
|
|
# Add it one position lower
|
|
|
|
queue.insert(index + 1, entry)
|
|
|
|
# Lets move the index to look at the new place of the entry
|
|
|
|
index += 1
|
|
|
|
# If x is clicked
|
|
|
|
elif '\u274c' in reaction.emoji:
|
|
|
|
# A second check just to make sure
|
2017-03-08 12:56:24 +13:00
|
|
|
if author.guild_permissions.kick_members or author == entry.requester:
|
2017-02-07 15:56:04 +13:00
|
|
|
if entry != queue[index]:
|
|
|
|
fmt = "`Error: Position of this entry has changed, cannot complete your action`"
|
|
|
|
else:
|
|
|
|
# Simply remove the entry in place
|
|
|
|
del queue[index]
|
|
|
|
# This is the only check we need to make, to ensure index is now not more than last
|
|
|
|
new_count = count - 1
|
|
|
|
if index >= new_count:
|
|
|
|
index = new_count - 1
|
|
|
|
try:
|
2017-04-20 15:18:05 +12:00
|
|
|
await message.remove_reaction(reaction.emoji, user)
|
2017-02-07 15:56:04 +13:00
|
|
|
except discord.Forbidden:
|
|
|
|
pass
|
2017-04-20 15:18:05 +12:00
|
|
|
await message.delete()
|
|
|
|
|
|
|
|
async def on_voice_state_update(self, member, before, after):
|
|
|
|
if after is None or after.channel is None:
|
|
|
|
return
|
|
|
|
state = self.voice_states.get(after.channel.guild.id)
|
2017-04-24 10:28:13 +12:00
|
|
|
if state is None or state.voice is None or state.voice.channel is None:
|
2017-04-20 15:18:05 +12:00
|
|
|
return
|
|
|
|
voice_channel = state.voice.channel
|
|
|
|
num_members = len(voice_channel.members)
|
|
|
|
state.required_skips = math.ceil((num_members + 1) / 3)
|
|
|
|
|
|
|
|
async def add_entry(self, song, ctx):
|
|
|
|
requester = ctx.message.author
|
|
|
|
state = self.voice_states[ctx.message.guild.id]
|
2017-04-23 15:06:45 +12:00
|
|
|
entry, _ = await state.songs.add_entry(song, requester)
|
2017-04-20 15:18:05 +12:00
|
|
|
return entry
|
2017-02-07 15:56:04 +13:00
|
|
|
|
2017-02-18 17:14:38 +13:00
|
|
|
@commands.command(pass_context=True)
|
|
|
|
@commands.check(utils.is_owner)
|
2017-04-20 15:18:05 +12:00
|
|
|
async def vdebug(self, ctx, *, code: str):
|
2017-02-18 17:14:38 +13:00
|
|
|
"""Evaluates code."""
|
|
|
|
code = code.strip('` ')
|
|
|
|
python = '```py\n{}\n```'
|
|
|
|
|
|
|
|
env = {
|
|
|
|
'bot': self.bot,
|
|
|
|
'ctx': ctx,
|
|
|
|
'message': ctx.message,
|
2017-03-08 12:56:24 +13:00
|
|
|
'server': ctx.message.guild,
|
2017-04-20 15:18:05 +12:00
|
|
|
'guild': ctx.message.guild,
|
2017-02-18 17:14:38 +13:00
|
|
|
'channel': ctx.message.channel,
|
|
|
|
'author': ctx.message.author
|
|
|
|
}
|
|
|
|
|
|
|
|
env.update(globals())
|
|
|
|
|
|
|
|
try:
|
|
|
|
result = eval(code, env)
|
|
|
|
if inspect.isawaitable(result):
|
|
|
|
result = await result
|
|
|
|
except Exception as e:
|
2017-03-08 12:56:24 +13:00
|
|
|
await ctx.send(python.format(type(e).__name__ + ': ' + str(e)))
|
2017-02-18 17:14:38 +13:00
|
|
|
return
|
|
|
|
|
2017-03-08 12:56:24 +13:00
|
|
|
await ctx.send(python.format(result))
|
2017-02-07 15:56:04 +13:00
|
|
|
|
2017-04-09 15:04:46 +12:00
|
|
|
@commands.command(pass_context=True)
|
|
|
|
@commands.guild_only()
|
2017-02-13 12:45:19 +13:00
|
|
|
@utils.custom_perms(send_messages=True)
|
2016-10-30 09:23:12 +13:00
|
|
|
async def progress(self, ctx):
|
|
|
|
"""Provides the progress of the current song"""
|
|
|
|
|
|
|
|
# Make sure we're playing first
|
2017-04-20 15:18:05 +12:00
|
|
|
state = self.voice_states.get(ctx.message.guild.id)
|
|
|
|
if state is None or not state.playing:
|
2017-03-08 12:56:24 +13:00
|
|
|
await ctx.send('Not playing anything.')
|
2016-10-30 09:23:12 +13:00
|
|
|
else:
|
|
|
|
progress = state.current.progress
|
|
|
|
length = state.current.length
|
|
|
|
# Another check, just to make sure; this may happen for a very brief amount of time
|
|
|
|
# Between when the song was requested, and still downloading to play
|
|
|
|
if not progress or not length:
|
2017-03-08 12:56:24 +13:00
|
|
|
await ctx.send('Not playing anything.')
|
2016-10-30 09:23:12 +13:00
|
|
|
return
|
|
|
|
|
|
|
|
# Otherwise just format this nicely
|
|
|
|
progress = divmod(round(progress, 0), 60)
|
|
|
|
length = divmod(round(length, 0), 60)
|
2016-10-30 10:00:53 +13:00
|
|
|
fmt = "Current song progress: {0[0]}m {0[1]}s/{1[0]}m {1[1]}s".format(progress, length)
|
2017-03-08 12:56:24 +13:00
|
|
|
await ctx.send(fmt)
|
2016-10-30 09:23:12 +13:00
|
|
|
|
2017-04-20 15:18:05 +12:00
|
|
|
@commands.command(aliases=['summon'])
|
2017-04-09 15:04:46 +12:00
|
|
|
@commands.guild_only()
|
2017-02-13 12:45:19 +13:00
|
|
|
@utils.custom_perms(send_messages=True)
|
2017-04-20 15:18:05 +12:00
|
|
|
async def join(self, ctx, *, channel: discord.VoiceChannel = None):
|
|
|
|
"""Joins a voice channel. Provide the name of a voice channel after the command, and
|
|
|
|
I will attempt to join this channel. Otherwise, I will join the channel you are in.
|
|
|
|
|
|
|
|
EXAMPLE: !join Music
|
|
|
|
RESULT: I have joined the music channel"""
|
|
|
|
if channel is None:
|
|
|
|
if ctx.message.author.voice is None or ctx.message.author.voice.channel is None:
|
|
|
|
await ctx.send("You need to either be in a voice channel, or provide the name of a voice channel!")
|
|
|
|
return False
|
|
|
|
channel = ctx.message.author.voice.channel
|
|
|
|
|
2017-04-20 15:32:18 +12:00
|
|
|
perms = channel.permissions_for(ctx.message.guild.me)
|
|
|
|
|
|
|
|
if not perms.connect or not perms.speak or not perms.use_voice_activation:
|
2017-04-20 15:43:24 +12:00
|
|
|
await ctx.send("I do not have correct permissions in {}! Please turn on `connect`, `speak`, and `use "
|
|
|
|
"voice activation`".format(channel.name))
|
2017-04-20 15:32:18 +12:00
|
|
|
return
|
|
|
|
|
2017-04-20 15:43:24 +12:00
|
|
|
state = self.voice_states.get(ctx.message.guild.id)
|
2017-04-25 09:21:17 +12:00
|
|
|
if state and state.voice and state.voice.channel:
|
2017-04-20 15:43:24 +12:00
|
|
|
await state.voice.move_to(channel)
|
2017-05-01 11:56:02 +12:00
|
|
|
await ctx.send("Joined {} and ready to play".format(channel.name))
|
2017-04-20 15:43:24 +12:00
|
|
|
return True
|
|
|
|
else:
|
|
|
|
self.voice_states[ctx.message.guild.id] = VoiceState(ctx.message.guild, self.bot)
|
|
|
|
await channel.connect()
|
|
|
|
await ctx.send("Joined {} and ready to play".format(channel.name))
|
|
|
|
return True
|
2016-07-08 01:05:42 +12:00
|
|
|
|
2017-04-20 15:18:05 +12:00
|
|
|
@commands.command()
|
2017-04-09 15:04:46 +12:00
|
|
|
@commands.guild_only()
|
2017-02-13 12:45:19 +13:00
|
|
|
@utils.custom_perms(send_messages=True)
|
2016-07-09 13:27:19 +12:00
|
|
|
async def play(self, ctx, *, song: str):
|
2016-07-08 01:05:42 +12:00
|
|
|
"""Plays a song.
|
|
|
|
If there is a song currently in the queue, then it is
|
|
|
|
queued until the next song is done playing.
|
|
|
|
This command automatically searches as well from YouTube.
|
|
|
|
The list of supported sites can be found here:
|
|
|
|
https://rg3.github.io/youtube-dl/supportedsites.html
|
|
|
|
"""
|
2017-04-20 15:18:05 +12:00
|
|
|
if ctx.message.guild.id not in self.voice_states:
|
|
|
|
if not await ctx.invoke(self.join):
|
2016-07-08 01:05:42 +12:00
|
|
|
return
|
2016-08-21 17:36:38 +12:00
|
|
|
|
2017-04-21 12:13:57 +12:00
|
|
|
song = re.sub('[<>\[\]]', '', song)
|
|
|
|
|
2016-08-28 15:32:11 +12:00
|
|
|
try:
|
2017-04-20 15:18:05 +12:00
|
|
|
entry = await self.add_entry(song, ctx)
|
|
|
|
except asyncio.TimeoutError:
|
|
|
|
await ctx.send("You took too long!")
|
2017-04-23 14:45:36 +12:00
|
|
|
except LiveStreamError as e:
|
2017-04-23 10:54:23 +12:00
|
|
|
await ctx.send(str(e))
|
2017-04-20 15:18:05 +12:00
|
|
|
else:
|
|
|
|
if entry is None:
|
|
|
|
await ctx.send("Sorry but I couldn't download/find {}".format(song))
|
|
|
|
else:
|
|
|
|
await ctx.send("Enqueued {}".format(entry))
|
2016-07-08 01:05:42 +12:00
|
|
|
|
2017-04-09 15:04:46 +12:00
|
|
|
@commands.command(pass_context=True)
|
|
|
|
@commands.guild_only()
|
2017-02-13 12:45:19 +13:00
|
|
|
@utils.custom_perms(kick_members=True)
|
2016-10-03 07:08:20 +13:00
|
|
|
async def volume(self, ctx, value: int = None):
|
2016-07-08 01:05:42 +12:00
|
|
|
"""Sets the volume of the currently playing song."""
|
|
|
|
|
2017-04-20 15:18:05 +12:00
|
|
|
state = self.voice_states.get(ctx.message.guild.id)
|
2017-04-26 22:00:43 +12:00
|
|
|
if state is None or state.voice is None or state.voice.source is None:
|
2017-04-21 10:28:14 +12:00
|
|
|
await ctx.send("Not playing anything right now, please set volume after playing something")
|
2017-04-24 12:09:49 +12:00
|
|
|
elif value is None:
|
2017-04-23 15:12:48 +12:00
|
|
|
volume = state.voice.source.volume
|
2017-03-08 12:56:24 +13:00
|
|
|
await ctx.send("Current volume is {}".format(volume))
|
2017-04-20 15:45:11 +12:00
|
|
|
elif value > 200:
|
2017-03-08 12:56:24 +13:00
|
|
|
await ctx.send("Sorry but the max volume is 200")
|
2017-04-20 15:45:11 +12:00
|
|
|
elif state.playing:
|
2017-04-23 15:12:48 +12:00
|
|
|
state.voice.source.volume = value / 100
|
2017-04-26 22:00:43 +12:00
|
|
|
await ctx.send('Set the volume to {:.0%}'.format(state.voice.source.volume))
|
2016-07-08 01:05:42 +12:00
|
|
|
|
2017-04-09 15:04:46 +12:00
|
|
|
@commands.command(pass_context=True)
|
|
|
|
@commands.guild_only()
|
2017-02-13 12:45:19 +13:00
|
|
|
@utils.custom_perms(kick_members=True)
|
2016-07-08 01:05:42 +12:00
|
|
|
async def pause(self, ctx):
|
|
|
|
"""Pauses the currently played song."""
|
2017-04-20 15:18:05 +12:00
|
|
|
state = self.voice_states.get(ctx.message.guild.id)
|
|
|
|
if state and state.playing:
|
|
|
|
state.voice.pause()
|
2016-07-08 01:05:42 +12:00
|
|
|
|
2017-04-09 15:04:46 +12:00
|
|
|
@commands.command(pass_context=True)
|
|
|
|
@commands.guild_only()
|
2017-02-13 12:45:19 +13:00
|
|
|
@utils.custom_perms(kick_members=True)
|
2016-07-08 01:05:42 +12:00
|
|
|
async def resume(self, ctx):
|
|
|
|
"""Resumes the currently played song."""
|
2017-04-20 15:18:05 +12:00
|
|
|
state = self.voice_states.get(ctx.message.guild.id)
|
2017-04-20 15:32:18 +12:00
|
|
|
if state and state.voice.is_connected():
|
2017-04-20 15:18:05 +12:00
|
|
|
state.voice.resume()
|
2016-07-08 01:05:42 +12:00
|
|
|
|
2017-04-09 15:04:46 +12:00
|
|
|
@commands.command(pass_context=True)
|
|
|
|
@commands.guild_only()
|
2017-02-13 12:45:19 +13:00
|
|
|
@utils.custom_perms(kick_members=True)
|
2016-07-08 01:05:42 +12:00
|
|
|
async def stop(self, ctx):
|
|
|
|
"""Stops playing audio and leaves the voice channel.
|
|
|
|
This also clears the queue.
|
|
|
|
"""
|
2017-04-20 15:18:05 +12:00
|
|
|
state = self.voice_states.get(ctx.message.guild.id)
|
2016-08-21 17:36:38 +12:00
|
|
|
|
2016-09-06 14:52:14 +12:00
|
|
|
# Stop playing whatever song is playing.
|
2017-04-22 17:29:58 +12:00
|
|
|
if state and state.voice and state.voice.is_connected():
|
2017-04-20 15:18:05 +12:00
|
|
|
state.voice.stop()
|
2017-02-18 17:11:19 +13:00
|
|
|
|
2017-04-20 15:35:06 +12:00
|
|
|
state.songs.clear()
|
2016-08-21 17:36:38 +12:00
|
|
|
|
2017-04-20 15:35:06 +12:00
|
|
|
# This will cancel the audio event we're using to loop through the queue
|
|
|
|
# Then erase the voice_state entirely, and disconnect from the channel
|
|
|
|
state.audio_player.cancel()
|
|
|
|
await state.voice.disconnect()
|
|
|
|
del self.voice_states[ctx.message.guild.id]
|
2016-08-10 14:13:53 +12:00
|
|
|
|
2017-04-09 15:04:46 +12:00
|
|
|
@commands.command(pass_context=True)
|
|
|
|
@commands.guild_only()
|
2017-02-13 12:45:19 +13:00
|
|
|
@utils.custom_perms(send_messages=True)
|
2016-08-07 01:09:24 +12:00
|
|
|
async def eta(self, ctx):
|
|
|
|
"""Provides an ETA on when your next song will play"""
|
2016-08-16 15:30:52 +12:00
|
|
|
# Note: There is no way to tell how long a song has been playing, or how long there is left on a song
|
|
|
|
# That is why this is called an "ETA"
|
2017-04-20 15:18:05 +12:00
|
|
|
state = self.voice_states.get(ctx.message.guild.id)
|
2016-08-07 01:09:24 +12:00
|
|
|
author = ctx.message.author
|
2016-08-10 14:13:53 +12:00
|
|
|
|
2017-04-20 15:18:05 +12:00
|
|
|
if state is None or not state.playing:
|
2017-03-08 12:56:24 +13:00
|
|
|
await ctx.send('Not playing any music right now...')
|
2016-08-07 01:09:24 +12:00
|
|
|
return
|
2016-12-22 12:06:32 +13:00
|
|
|
|
|
|
|
queue = state.songs.entries
|
2016-08-16 15:30:52 +12:00
|
|
|
if len(queue) == 0:
|
2017-03-08 12:56:24 +13:00
|
|
|
await ctx.send("Nothing currently in the queue")
|
2016-08-07 01:09:24 +12:00
|
|
|
return
|
2016-08-21 17:36:38 +12:00
|
|
|
|
2016-12-22 12:06:32 +13:00
|
|
|
# Start off by adding the remaining length of the current song
|
|
|
|
count = state.current.remaining
|
2016-08-07 01:09:24 +12:00
|
|
|
found = False
|
2016-08-16 15:30:52 +12:00
|
|
|
# Loop through the songs in the queue, until the author is found as the requester
|
|
|
|
# The found bool is used to see if we actually found the author, or we just looped through the whole queue
|
|
|
|
for song in queue:
|
2016-08-07 01:09:24 +12:00
|
|
|
if song.requester == author:
|
|
|
|
found = True
|
|
|
|
break
|
2016-12-22 12:06:32 +13:00
|
|
|
count += song.duration
|
2016-08-21 17:36:38 +12:00
|
|
|
|
2016-08-07 01:09:24 +12:00
|
|
|
if not found:
|
2017-03-08 12:56:24 +13:00
|
|
|
await ctx.send("You are not in the queue!")
|
2016-08-07 01:09:24 +12:00
|
|
|
return
|
2017-03-08 12:56:24 +13:00
|
|
|
await ctx.send("ETA till your next play is: {0[0]}m {0[1]}s".format(divmod(round(count, 0), 60)))
|
2016-08-10 14:13:53 +12:00
|
|
|
|
2017-04-09 15:04:46 +12:00
|
|
|
@commands.command(pass_context=True)
|
|
|
|
@commands.guild_only()
|
2017-02-13 12:45:19 +13:00
|
|
|
@utils.custom_perms(send_messages=True)
|
2016-08-06 01:45:20 +12:00
|
|
|
async def queue(self, ctx):
|
|
|
|
"""Provides a printout of the songs that are in the queue"""
|
2017-04-20 15:18:05 +12:00
|
|
|
state = self.voice_states.get(ctx.message.guild.id)
|
|
|
|
if state is None:
|
|
|
|
await ctx.send("Nothing currently in the queue")
|
2016-08-06 01:45:20 +12:00
|
|
|
return
|
2016-08-16 15:30:52 +12:00
|
|
|
# Asyncio provides no non-private way to access the queue, so we have to use _queue
|
2017-04-20 15:18:05 +12:00
|
|
|
_queue = state.songs.entries
|
|
|
|
if len(_queue) == 0:
|
2017-03-08 12:56:24 +13:00
|
|
|
await ctx.send("Nothing currently in the queue")
|
2016-08-07 00:27:08 +12:00
|
|
|
else:
|
2017-02-07 15:56:04 +13:00
|
|
|
self.bot.loop.create_task(self.queue_embed_task(state, ctx.message.channel, ctx.message.author))
|
2016-07-09 13:27:19 +12:00
|
|
|
|
2017-04-09 15:04:46 +12:00
|
|
|
@commands.command(pass_context=True)
|
|
|
|
@commands.guild_only()
|
2017-02-13 12:45:19 +13:00
|
|
|
@utils.custom_perms(send_messages=True)
|
2016-07-09 13:27:19 +12:00
|
|
|
async def queuelength(self, ctx):
|
2016-07-08 01:05:42 +12:00
|
|
|
"""Prints the length of the queue"""
|
2017-03-08 12:56:24 +13:00
|
|
|
await ctx.send("There are a total of {} songs in the queue"
|
2017-04-20 15:18:05 +12:00
|
|
|
.format(len(self.voice_states.get(ctx.message.guild.id).songs.entries)))
|
2016-07-31 12:20:55 +12:00
|
|
|
|
2017-04-09 15:04:46 +12:00
|
|
|
@commands.command(pass_context=True)
|
|
|
|
@commands.guild_only()
|
2017-02-13 12:45:19 +13:00
|
|
|
@utils.custom_perms(send_messages=True)
|
2016-07-08 01:05:42 +12:00
|
|
|
async def skip(self, ctx):
|
|
|
|
"""Vote to skip a song. The song requester can automatically skip.
|
2016-08-20 16:00:23 +12:00
|
|
|
approximately 1/3 of the members in the voice channel
|
|
|
|
are required to vote to skip for the song to be skipped.
|
2016-07-08 01:05:42 +12:00
|
|
|
"""
|
|
|
|
|
2017-04-20 15:18:05 +12:00
|
|
|
state = self.voice_states.get(ctx.message.guild.id)
|
|
|
|
if state is None or not state.playing:
|
2017-03-08 12:56:24 +13:00
|
|
|
await ctx.send('Not playing any music right now...')
|
2016-07-08 01:05:42 +12:00
|
|
|
return
|
2016-08-21 17:36:38 +12:00
|
|
|
|
2016-08-16 15:30:52 +12:00
|
|
|
# Check if the person requesting a skip is the requester of the song, if so automatically skip
|
2016-07-08 01:05:42 +12:00
|
|
|
voter = ctx.message.author
|
|
|
|
if voter == state.current.requester:
|
2017-03-08 12:56:24 +13:00
|
|
|
await ctx.send('Requester requested skipping song...')
|
2016-07-08 01:05:42 +12:00
|
|
|
state.skip()
|
2016-08-16 15:30:52 +12:00
|
|
|
# Otherwise check if the voter has already voted
|
2016-07-08 01:05:42 +12:00
|
|
|
elif voter.id not in state.skip_votes:
|
|
|
|
state.skip_votes.add(voter.id)
|
|
|
|
total_votes = len(state.skip_votes)
|
2016-08-21 17:36:38 +12:00
|
|
|
|
2016-08-16 15:30:52 +12:00
|
|
|
# Now check how many votes have been made, if 3 then go ahead and skip, otherwise add to the list of votes
|
2016-08-20 16:00:23 +12:00
|
|
|
if total_votes >= state.required_skips:
|
2017-03-08 12:56:24 +13:00
|
|
|
await ctx.send('Skip vote passed, skipping song...')
|
2016-07-08 01:05:42 +12:00
|
|
|
state.skip()
|
|
|
|
else:
|
2017-03-08 12:56:24 +13:00
|
|
|
await ctx.send('Skip vote added, currently at [{}/{}]'.format(total_votes, state.required_skips))
|
2016-07-08 01:05:42 +12:00
|
|
|
else:
|
2017-03-08 12:56:24 +13:00
|
|
|
await ctx.send('You have already voted to skip this song.')
|
2016-07-31 12:20:55 +12:00
|
|
|
|
2017-04-09 15:04:46 +12:00
|
|
|
@commands.command(pass_context=True)
|
|
|
|
@commands.guild_only()
|
2017-02-13 12:45:19 +13:00
|
|
|
@utils.custom_perms(kick_members=True)
|
2016-07-08 01:05:42 +12:00
|
|
|
async def modskip(self, ctx):
|
|
|
|
"""Forces a song skip, can only be used by a moderator"""
|
2017-04-20 15:18:05 +12:00
|
|
|
state = self.voice_states.get(ctx.message.guild.id)
|
|
|
|
if state is None or not state.playing:
|
2017-03-08 12:56:24 +13:00
|
|
|
await ctx.send('Not playing any music right now...')
|
2016-07-08 01:05:42 +12:00
|
|
|
return
|
2016-07-31 12:20:55 +12:00
|
|
|
|
2016-07-08 01:05:42 +12:00
|
|
|
state.skip()
|
2017-03-08 12:56:24 +13:00
|
|
|
await ctx.send('Song has just been skipped.')
|
2016-07-08 01:05:42 +12:00
|
|
|
|
2017-04-09 15:04:46 +12:00
|
|
|
@commands.command(pass_context=True)
|
|
|
|
@commands.guild_only()
|
2017-02-13 12:45:19 +13:00
|
|
|
@utils.custom_perms(send_messages=True)
|
2016-07-08 01:05:42 +12:00
|
|
|
async def playing(self, ctx):
|
|
|
|
"""Shows info about the currently played song."""
|
|
|
|
|
2017-04-20 15:18:05 +12:00
|
|
|
state = self.voice_states.get(ctx.message.guild.id)
|
|
|
|
if state is None or not state.playing:
|
2017-03-08 12:56:24 +13:00
|
|
|
await ctx.send('Not playing anything.')
|
2016-07-08 01:05:42 +12:00
|
|
|
else:
|
2017-02-07 15:56:04 +13:00
|
|
|
# Create the embed object we'll use
|
|
|
|
embed = discord.Embed()
|
|
|
|
# Fill in the simple things
|
|
|
|
embed.add_field(name='Title', value=state.current.title, inline=False)
|
|
|
|
embed.add_field(name='Requester', value=state.current.requester.display_name, inline=False)
|
|
|
|
# Get the amount of current skips, and display how many have been skipped/how many required
|
2016-07-08 01:05:42 +12:00
|
|
|
skip_count = len(state.skip_votes)
|
2017-02-07 15:56:04 +13:00
|
|
|
embed.add_field(name='Skip Count', value='{}/{}'.format(skip_count, state.required_skips), inline=False)
|
|
|
|
# Get the current progress and display this
|
|
|
|
progress = state.current.progress
|
|
|
|
length = state.current.length
|
|
|
|
progress = divmod(round(progress, 0), 60)
|
|
|
|
length = divmod(round(length, 0), 60)
|
|
|
|
fmt = "{0[0]}m {0[1]}s/{1[0]}m {1[1]}s".format(progress, length)
|
2017-04-20 15:18:05 +12:00
|
|
|
embed.add_field(name='Progress', value=fmt, inline=False)
|
2017-02-07 15:56:04 +13:00
|
|
|
# And send the embed
|
2017-03-08 12:56:24 +13:00
|
|
|
await ctx.send(embed=embed)
|
2016-07-09 13:27:19 +12:00
|
|
|
|
|
|
|
|
|
|
|
def setup(bot):
|
|
|
|
bot.add_cog(Music(bot))
|