Skip to content

Commit

Permalink
Merge branch 'master' into review
Browse files Browse the repository at this point in the history
  • Loading branch information
vonDassendorf authored Aug 24, 2021
2 parents 20aac7f + 9420dce commit 4e84d05
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 16 deletions.
4 changes: 4 additions & 0 deletions config/example_options.ini
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ CommandPrefix = !
# a space.
BindToChannels =

# Restricts the bot to only join certain voice channels. To use this, add the IDs
# of the voice channels you would like the bot to join, separated by a space
RestrictToChannels =

# Changes the behavior of BindToChannels. Normally any messages sent to a channel not in
# BindToChannels will be ignored. This option allows servers that do not have any bound
# channels while other server have some defined to still use commands in any channel with
Expand Down
62 changes: 50 additions & 12 deletions musicbot/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,11 @@ def _autopause(player):
)
continue

elif self.config.restrict_to_channels and channel not in self.restricted_to_channels:
log.info("Will not join channel \"{}\", restricted to only join specific channels:".format(channel.name))
[log.info(' - {}/{}'.format(ch.guild.name.strip(), ch.name.strip())) for ch in self.restricted_to_channels if ch]
continue

try:
player = await self.get_player(
channel, create=True, deserialize=self.config.persistent_queue
Expand Down Expand Up @@ -1230,14 +1235,41 @@ async def on_ready(self):
else:
log.info("Not bound to any text channels")

if self.config.restrict_to_channels:
chlist = set(self.get_channel(i) for i in self.config.restrict_to_channels if i)
chlist.discard(None)

invalids = set()
invalids.update(c for c in chlist if isinstance(c, discord.TextChannel))

chlist.difference_update(invalids)

if chlist:
log.info("Restricted to voice channels:")
[log.info(' - {}/{}'.format(ch.guild.name.strip(), ch.name.strip())) for ch in chlist if ch]

else:
log.info("Not restricted to any voice channels")

if invalids and self.config.debug_mode:
print(flush=True)
log.info("RestrictedToChannels is meant to restrict the bot to specific voice channels."
"Use BindToChannel option to restrict the bot to text channels:")
[log.info(' - {}/{}'.format(ch.guild.name.strip(), ch.name.strip())) for ch in invalids if ch]

self.restricted_to_channels = chlist
else:
log.info("Not restricted to any voice channels")
self.restricted_to_channels = set()

if self.config.autojoin_channels:
chlist = set(
self.get_channel(i) for i in self.config.autojoin_channels if i
)
chlist.discard(None)

invalids = set()
invalids.update(c for c in chlist if isinstance(c, discord.TextChannel))
invalids.update(c for c in chlist if isinstance(c, discord.TextChannel) or c not in self.restricted_to_channels)

chlist.difference_update(invalids)
self.config.autojoin_channels.difference_update(invalids)
Expand All @@ -1254,12 +1286,10 @@ async def on_ready(self):

if invalids and self.config.debug_mode:
print(flush=True)
log.info("Cannot autojoin text channels:")
[
log.info(" - {}/{}".format(ch.guild.name.strip(), ch.name.strip()))
for ch in invalids
if ch
]
log.info("Cannot autojoin following channels because they are either "
"text channels or bot is restricted:")
[log.info(' - {}/{}'.format(ch.guild.name.strip(), ch.name.strip())) for ch in invalids if ch]


self.autojoin_channels = chlist

Expand Down Expand Up @@ -2809,11 +2839,19 @@ async def cmd_summon(self, channel, guild, author, voice_channel):
expire_in=25,
)

player = await self.get_player(
author.voice.channel,
create=True,
deserialize=self.config.persistent_queue,
)
elif author.voice.channel not in self.restricted_to_channels:
log.info("Will not join channel '{0}/{1}', restricted to only join specific channels".format(
author.voice.channel.guild, author.voice.channel.name))
raise exceptions.CommandError(
self.str.get('cmd-summon-channel-not-permitted',
"Will not join channel `{0}/{1}`, restricted to channels:\n").format(
author.voice.channel.guild, author.voice.channel.name) +
"\n".join(['`'+c.guild.name+'/'+c.name+'`' for c in self.restricted_to_channels
if author.voice.channel.guild == c.guild]),
expire_in=25
)

player = await self.get_player(author.voice.channel, create=True, deserialize=self.config.persistent_queue)

if player.is_stopped:
player.play()
Expand Down
20 changes: 16 additions & 4 deletions musicbot/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,8 @@ def __init__(self, config_file):
)

self.auth = ()

self.spotify_clientid = config.get(
"Credentials", "Spotify_ClientID", fallback=ConfigDefaults.spotify_clientid
)
self.restrict_to_channels = config.get('Chat', 'RestrictToChannels', fallback=ConfigDefaults.restrict_to_channels)
self.spotify_clientid = config.get("Credentials", "Spotify_ClientID", fallback=ConfigDefaults.spotify_clientid)
self.spotify_clientsecret = config.get(
"Credentials",
"Spotify_ClientSecret",
Expand Down Expand Up @@ -307,6 +305,13 @@ def run_checks(self):
)
self.bound_channels = set()

if self.restrict_to_channels:
try:
self.restrict_to_channels = set(x for x in self.restrict_to_channels.replace(',', ' ').split() if x)
except:
log.warning("RestrictToChannels data is invalid, will not restrict to any channels")
self.restrict_to_channels = set()

if self.autojoin_channels:
try:
self.autojoin_channels = set(
Expand Down Expand Up @@ -339,6 +344,12 @@ def run_checks(self):

self.delete_invoking = self.delete_invoking and self.delete_messages

self.bound_channels = set(int(item) for item in self.bound_channels)

self.restrict_to_channels = set(int(item) for item in self.restrict_to_channels)

self.autojoin_channels = set(int(item) for item in self.autojoin_channels)

ap_path, ap_name = os.path.split(self.auto_playlist_file)
apn_name, apn_ext = os.path.splitext(ap_name)
self.auto_playlist_removed_file = os.path.join(
Expand Down Expand Up @@ -478,6 +489,7 @@ class ConfigDefaults:

command_prefix = "!"
bound_channels = set()
restrict_to_channels = set()
unbound_servers = False
autojoin_channels = set()
dm_nowplaying = False
Expand Down

0 comments on commit 4e84d05

Please sign in to comment.