Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Queue command format and Auto-Announce feature #71

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
6 changes: 3 additions & 3 deletions config/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
VC_TIMOUT_DEFAULT = True #default template setting for VC timeout true= yes, timeout false= no timeout
ALLOW_VC_TIMEOUT_EDIT = True #allow or disallow editing the vc_timeout guild setting


STARTUP_MESSAGE = "Starting Bot..."
STARTUP_COMPLETE_MESSAGE = "Startup Complete"

Expand All @@ -44,7 +43,8 @@
SONGINFO_DISLIKES = "Dislikes: "
SONGINFO_NOW_PLAYING = "Now Playing"
SONGINFO_QUEUE_ADDED = "Added to queue"
SONGINFO_SONGINFO = "Song info"
SONGINFO_SONGINFO = "Now playing"
SONGINFO_UNKNOWN_SITE = "Unknown site :question:"
SONGINFO_ERROR = "Error: Unsupported site or age restricted content. To enable age restricted content check the documentation/wiki."
SONGINFO_PLAYLIST_QUEUED = "Queued playlist :page_with_curl:"
SONGINFO_UNKNOWN_DURATION = "Unknown"
Expand Down Expand Up @@ -90,4 +90,4 @@
HELP_CHANGECHANNEL_SHORT = "Change the bot channel"
HELP_CHANGECHANNEL_LONG = "Change the bot channel to the VC you are in"

ABSOLUTE_PATH = '' #do not modify
ABSOLUTE_PATH = '' #do not modify
8 changes: 8 additions & 0 deletions musicbot/audiocontroller.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ def __init__(self, bot, guild):

self.timer = utils.Timer(self.timeout_handler)

self.ctx = None

@property
def volume(self):
return self._volume
Expand Down Expand Up @@ -96,6 +98,11 @@ async def play_song(self, song):

self.guild.voice_client.play(discord.FFmpegPCMAudio(
song.base_url, before_options='-reconnect 1 -reconnect_streamed 1 -reconnect_delay_max 5'), after=lambda e: self.next_song(e))

sett = utils.guild_to_settings[self.guild]

if self.ctx is not None and sett.get("announce_tracks"):
await self.ctx.invoke(self.bot.get_command('songinfo'))

self.guild.voice_client.source = discord.PCMVolumeTransformer(
self.guild.voice_client.source)
Expand Down Expand Up @@ -327,6 +334,7 @@ async def timeout_handler(self):
await self.udisconnect()

async def uconnect(self, ctx):
self.ctx = ctx
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When the setting start_voice_channel is set it bypasses uconnect() which will have ctx = none. See line 71 and 79 in run.py. For this feature to work when using these settings uconnect() would need to be reworked to accept an optional voice_channel argument for use in run.py and for line 71 & 79 to use uconnect().


if not ctx.author.voice:
await ctx.send(config.NO_GUILD_MESSAGE)
Expand Down
31 changes: 21 additions & 10 deletions musicbot/commands/music.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,12 @@ async def _play_song(self, ctx, *, track: str):

if song.origin == linkutils.Origins.Default:

if audiocontroller.current_song != None and len(audiocontroller.playlist.playque) == 0:
await ctx.send(embed=song.info.format_output(config.SONGINFO_NOW_PLAYING))
else:
sett = utils.guild_to_settings[ctx.guild]

if audiocontroller.current_song == None or len(audiocontroller.playlist.playque) != 0:
await ctx.send(embed=song.info.format_output(config.SONGINFO_QUEUE_ADDED))
elif not sett.get("announce_tracks"):
await ctx.send(embed=song.info.format_output(config.SONGINFO_NOW_PLAYING))

elif song.origin == linkutils.Origins.Playlist:
await ctx.send(config.SONGINFO_PLAYLIST_QUEUED)
Expand Down Expand Up @@ -128,20 +130,29 @@ async def _queue(self, ctx):

playlist = utils.guild_to_audiocontroller[current_guild].playlist

# Embeds are limited to 25 fields
# Embeds are limited to 4096 characters, this is well under that
if config.MAX_SONG_PRELOAD > 25:
config.MAX_SONG_PRELOAD = 25

embed = discord.Embed(title=":scroll: Queue [{}]".format(
len(playlist.playque)), color=config.EMBED_COLOR, inline=False)
total_runtime = utils.format_time(
sum([int(song.info.duration if song.info.duration else 0) for song in list(playlist.playque)]))

embed = discord.Embed(title=":scroll: {} songs in queue | {} total length".format(
len(playlist.playque), total_runtime), color=config.EMBED_COLOR, inline=False)

in_queue_formats = []
for counter, song in enumerate(list(playlist.playque)[:config.MAX_SONG_PRELOAD], start=1):
if song.info.title is None:
embed.add_field(name="{}.".format(str(counter)), value="[{}]({})".format(
song.info.webpage_url, song.info.webpage_url), inline=False)
in_queue_formats.append("`{}.` [{}]({}) `{}`".format(
str(counter), song.info.webpage_url, song.info.webpage_url,
utils.format_time(song.info.duration)))
else:
embed.add_field(name="{}.".format(str(counter)), value="[{}]({})".format(
song.info.title, song.info.webpage_url), inline=False)
in_queue_formats.append("`{}.` [{}]({}) `{}`".format(
str(counter), song.info.title, song.info.webpage_url,
utils.format_time(song.info.duration)))

if len(in_queue_formats):
embed.description = '\n'.join(in_queue_formats)

await ctx.send(embed=embed)

Expand Down
12 changes: 11 additions & 1 deletion musicbot/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ def __init__(self, guild):
"user_must_be_in_vc": True,
"button_emote": "",
"default_volume": 100,
"vc_timeout": config.VC_TIMOUT_DEFAULT
"vc_timeout": config.VC_TIMOUT_DEFAULT,
"announce_tracks": False
}

self.reload()
Expand Down Expand Up @@ -136,6 +137,7 @@ async def process_setting(self, setting, value, ctx):
'button_emote': lambda: self.button_emote(setting, value, ctx),
'default_volume': lambda: self.default_volume(setting, value, ctx),
'vc_timeout': lambda: self.vc_timeout(setting, value, ctx),
'announce_tracks': lambda: self.announce_tracks(setting, value, ctx),
}
func = switcher.get(setting)

Expand Down Expand Up @@ -246,3 +248,11 @@ async def vc_timeout(self, setting, value, ctx):
else:
await ctx.send("`Error: Value must be True/False`\nUsage: {}set {} True/False".format(config.BOT_PREFIX, setting))
return False

async def announce_tracks(self, setting, value, ctx):
if value.lower() == "true":
self.config[setting] = True
elif value.lower() == "false":
self.config[setting] = False
else:
await ctx.send("`Error: Value must be True/False`\nUsage: {}set {} True/False".format(config.BOT_PREFIX, setting))
16 changes: 16 additions & 0 deletions musicbot/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,22 @@ async def play_check(ctx):
return False


def format_time(duration):
if not duration:
return "00:00"

hours = duration // 60 // 60
minutes = duration // 60 % 60
seconds = duration % 60

# Looks like `h:mm:ss`
return "{}{}{:02d}:{:02d}".format(
hours if hours else "",
":" if hours else "",
minutes,
seconds
)

class Timer:
def __init__(self, callback):
self._callback = callback
Expand Down