Skip to content

Commit

Permalink
Merge pull request Just-Some-Bots#1808 from TheerapakG/playground_aut…
Browse files Browse the repository at this point in the history
…oplaylistmsg

Now playing message for automatic entries
  • Loading branch information
Jayden Bailey authored Apr 23, 2019
2 parents dc4ebbc + 80ba052 commit b1e9e02
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 12 deletions.
14 changes: 14 additions & 0 deletions config/example_options.ini
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,20 @@ AllowUnboundServers = no
# enabled, this option will take priority.
AutojoinChannels =

# Determine which channels the bot is going to output now playing messages to. If this is
# not specified for a server, the bot will output now playing message for manually added
# entry in the channel that users used the command to add that entry. The bot will also
# try to guess where the now playing messages for automatically added entries should go
# if this is not specified for a server, which might not possible in some cases. Specifying
# more than one channel for a server forces the bot to pick only one channel from the list
# to send messages to
NowPlayingChannels =

# The bot try to delete (or edit) previously sent now playing messages by default. If you
# don't want the bot to delete them (for keeping log of what have been played), turn this
# option off.
DeleteNowPlaying = yes

[MusicBot]
# The volume of the bot, between 0.01 and 1.0.
DefaultVolume = 0.25
Expand Down
44 changes: 32 additions & 12 deletions musicbot/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -469,15 +469,6 @@ async def on_player_play(self, player, entry):
author = entry.meta.get('author', None)

if channel and author:
last_np_msg = self.server_specific_data[channel.guild]['last_np_msg']
if last_np_msg and last_np_msg.channel == channel:

async for lmsg in channel.history(limit=1):
if lmsg != last_np_msg and last_np_msg:
await self.safe_delete_message(last_np_msg)
self.server_specific_data[channel.guild]['last_np_msg'] = None
break # This is probably redundant

author_perms = self.permissions.for_user(author)

if author not in player.voice_client.channel.members and author_perms.skip_when_absent:
Expand All @@ -490,11 +481,32 @@ async def on_player_play(self, player, entry):
else:
newmsg = 'Now playing in `%s`: `%s` added by `%s`' % (
player.voice_client.channel.name, entry.title, entry.meta['author'].name)
else:
# no author (and channel), it's an autoplaylist (or autostream from my other PR) entry.
newmsg = 'Now playing automatically added entry `%s` in `%s`' % (
entry.title, player.voice_client.channel.name)

if newmsg:
guild = player.voice_client.guild
last_np_msg = self.server_specific_data[guild]['last_np_msg']

if self.config.nowplaying_channels:
for potential_channel_id in self.config.nowplaying_channels:
potential_channel = self.get_channel(potential_channel_id)
if potential_channel and potential_channel.guild == guild:
channel = potential_channel
break

if self.server_specific_data[channel.guild]['last_np_msg']:
self.server_specific_data[channel.guild]['last_np_msg'] = await self.safe_edit_message(last_np_msg, newmsg, send_if_fail=True)
if channel:
pass
elif not channel and last_np_msg:
channel = last_np_msg.channel
else:
self.server_specific_data[channel.guild]['last_np_msg'] = await self.safe_send_message(channel, newmsg)
log.debug('no channel to put now playing message into')
return

# send it in specified channel
self.server_specific_data[guild]['last_np_msg'] = await self.safe_send_message(channel, newmsg)

# TODO: Check channel voice state?

Expand All @@ -513,6 +525,14 @@ async def on_player_stop(self, player, **_):

async def on_player_finished_playing(self, player, **_):
log.debug('Running on_player_finished_playing')

# delete last_np_msg somewhere if we have cached it
if self.config.delete_nowplaying:
guild = player.voice_client.guild
last_np_msg = self.server_specific_data[guild]['last_np_msg']
if last_np_msg:
await self.safe_delete_message(last_np_msg)

def _autopause(player):
if self._check_if_empty(player.voice_client.channel):
log.info("Player finished playing, autopaused in empty channel")
Expand Down
11 changes: 11 additions & 0 deletions musicbot/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ def __init__(self, config_file):
self.bound_channels = config.get('Chat', 'BindToChannels', fallback=ConfigDefaults.bound_channels)
self.unbound_servers = config.getboolean('Chat', 'AllowUnboundServers', fallback=ConfigDefaults.unbound_servers)
self.autojoin_channels = config.get('Chat', 'AutojoinChannels', fallback=ConfigDefaults.autojoin_channels)
self.nowplaying_channels = config.get('Chat', 'NowPlayingChannels', fallback=ConfigDefaults.nowplaying_channels)
self.delete_nowplaying = config.getboolean('Chat', 'DeleteNowPlaying', fallback=ConfigDefaults.delete_nowplaying)

self.default_volume = config.getfloat('MusicBot', 'DefaultVolume', fallback=ConfigDefaults.default_volume)
self.skips_required = config.getint('MusicBot', 'SkipsRequired', fallback=ConfigDefaults.skips_required)
Expand Down Expand Up @@ -179,6 +181,13 @@ def run_checks(self):
log.warning("AutojoinChannels data is invalid, will not autojoin any channels")
self.autojoin_channels = set()

if self.nowplaying_channels:
try:
self.nowplaying_channels = set(int(x) for x in self.nowplaying_channels.replace(',', ' ').split() if x)
except:
log.warning("NowPlayingChannels data is invalid, will use the default behavior for all servers")
self.autojoin_channels = set()

self._spotify = False
if self.spotify_clientid and self.spotify_clientsecret:
self._spotify = True
Expand Down Expand Up @@ -315,6 +324,8 @@ class ConfigDefaults:
bound_channels = set()
unbound_servers = False
autojoin_channels = set()
nowplaying_channels = set()
delete_nowplaying = True

default_volume = 0.15
skips_required = 4
Expand Down

0 comments on commit b1e9e02

Please sign in to comment.