Skip to content

Commit

Permalink
Merge branch 'permissions' into autostream_permissions
Browse files Browse the repository at this point in the history
  • Loading branch information
TheerapakG committed Sep 26, 2018
2 parents 8f6b837 + 157d62e commit 6cd34ff
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 35 deletions.
14 changes: 14 additions & 0 deletions config/example_permissions.ini
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,20 @@
;
;;;;;;;;;;;;;;;;;;;

; This group is for owner. Any options not specified will fallback to permissive default value. Don't remove/rename this group.
; You cannot assign users or roles to this group. Those options are ignored.
[Owner (auto)]
; MaxSongLength = 0
; MaxSongs = 0
; MaxPlaylistLength = 0
; AllowPlaylists = yes
; InstaSkip = yes
; Remove = yes
; SkipWhenAbsent = no
; BypassKaraokeMode = yes
; ToggleAutoPlaylists = yes
; Extractors =

; This is the fallback group for any users that don't get assigned to another group. Don't remove/rename this group.
; You cannot assign users or roles to this group. Those options are ignored.
[Default]
Expand Down
13 changes: 6 additions & 7 deletions musicbot/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -1203,7 +1203,7 @@ async def on_ready(self):
self.config.autojoin_channels.difference_update(invalids)

if chlist:
log.info("Autojoining voice chanels:")
log.info("Autojoining voice channels:")
[log.info(' - {}/{}'.format(ch.guild.name.strip(), ch.name.strip())) for ch in chlist if ch]
else:
log.info("Not autojoining any voice channels")
Expand Down Expand Up @@ -1340,7 +1340,7 @@ async def cmd_toggleplaylist(self, author, permissions, player, channel):
await self.serialize_json(player.auto_mode, player.voice_client.channel.guild, dir = 'data/%s/mode.json')

if player.auto_mode['mode'] == 'toggle':
if not permissions.toggle_playlists and not author.id == self.config.owner_id and not author == user:
if not permissions.toggle_playlists and not author == user:
raise exceptions.PermissionsError(
self.str.get('cmd-toggleplaylist-noperm', 'You have no permission to toggle autoplaylist'),
expire_in=30
Expand Down Expand Up @@ -2310,7 +2310,7 @@ async def cmd_remove(self, user_mentions, message, author, permissions, channel,

if user_mentions:
for user in user_mentions:
if author.id == self.config.owner_id or permissions.remove or author == user:
if permissions.remove or author == user:
try:
entry_indexes = [e for e in player.playlist.entries if e.meta.get('author', None) == user]
for entry in entry_indexes:
Expand All @@ -2337,7 +2337,7 @@ async def cmd_remove(self, user_mentions, message, author, permissions, channel,
if index > len(player.playlist.entries):
raise exceptions.CommandError(self.str.get('cmd-remove-invalid', "Invalid number. Use {}queue to find queue positions.").format(self.config.command_prefix), expire_in=20)

if author.id == self.config.owner_id or permissions.remove or author == player.playlist.get_entry_at_index(index - 1).meta.get('author', None):
if permissions.remove or author == player.playlist.get_entry_at_index(index - 1).meta.get('author', None):
entry = player.playlist.delete_entry_at_index((index - 1))
await self._manual_delete_check(message)
if entry.meta.get('channel', False) and entry.meta.get('author', False):
Expand Down Expand Up @@ -2378,9 +2378,8 @@ async def cmd_skip(self, player, channel, author, message, permissions, voice_ch
current_entry = player.current_entry

if (param.lower() in ['force', 'f']) or self.config.legacy_skip:
if author.id == self.config.owner_id \
or permissions.instaskip \
or (self.config.allow_author_skip and author == player.current_entry.meta.get('author', None)):
if permissions.instaskip \
or (self.config.allow_author_skip and author == player.current_entry.meta.get('author', None)):

player.skip() # TODO: check autopause stuff here
await self._manual_delete_check(message)
Expand Down
105 changes: 77 additions & 28 deletions musicbot/permissions.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@

class PermissionsDefaults:
perms_file = 'config/permissions.ini'

#now it's unpermissive by default for most
CommandWhiteList = set()
CommandBlackList = set()
IgnoreNonVoice = set()
GrantToRoles = set()
UserList = set()

MaxSongs = 0
MaxSongLength = 0
MaxSongs = 8
MaxSongLength = 210
MaxPlaylistLength = 0
MaxSearchItems = 10

Expand All @@ -30,11 +30,37 @@ class PermissionsDefaults:

ToggleAutoPlaylists = False

Extractors = set()

Extractors = "youtube youtube:playlist"

class Permissions:
def __init__(self, config_file, grant_all=None):

def gen_permissive():
configPermissive = configparser.ConfigParser(interpolation=None)
configPermissive['PermissionsPermissive'] = {}
sectionPermissive = configPermissive['PermissionsPermissive']
sectionPermissive['CommandWhiteList'] = ''
sectionPermissive['CommandBlackList'] = ''
sectionPermissive['IgnoreNonVoice'] = ''
sectionPermissive['GrantToRoles'] = ''
sectionPermissive['UserList'] = ''

sectionPermissive['MaxSongs'] = '0'
sectionPermissive['MaxSongLength'] = '0'
sectionPermissive['MaxPlaylistLength'] = '0'
sectionPermissive['MaxSearchItems'] = '20'

sectionPermissive['AllowPlaylists'] = 'yes'
sectionPermissive['InstaSkip'] = 'yes'
sectionPermissive['Remove'] = 'yes'
sectionPermissive['SkipWhenAbsent'] = 'no'
sectionPermissive['BypassKaraokeMode'] = 'yes'

sectionPermissive['ToggleAutoPlaylists'] = 'yes'

sectionPermissive['Extractors'] = ''
return sectionPermissive

def __init__(self, config_file, grant_all=None):
self.config_file = config_file
self.config = configparser.ConfigParser(interpolation=None)

Expand All @@ -57,7 +83,7 @@ def __init__(self, config_file, grant_all=None):

# Create a fake section to fallback onto the permissive default values to grant to the owner
# noinspection PyTypeChecker
owner_group = PermissionGroup("Owner (auto)", configparser.SectionProxy(self.config, None))
owner_group = PermissionGroup('Owner (auto)', self.config['Owner (auto)'], fallback=Permissions.gen_permissive())
if hasattr(grant_all, '__iter__'):
owner_group.user_list = set(grant_all)

Expand Down Expand Up @@ -104,29 +130,52 @@ def create_group(self, name, **kwargs):


class PermissionGroup:
def __init__(self, name, section_data):
def __init__(self, name, section_data, fallback=None):
self.name = name

self.command_whitelist = section_data.get('CommandWhiteList', fallback=PermissionsDefaults.CommandWhiteList)
self.command_blacklist = section_data.get('CommandBlackList', fallback=PermissionsDefaults.CommandBlackList)
self.ignore_non_voice = section_data.get('IgnoreNonVoice', fallback=PermissionsDefaults.IgnoreNonVoice)
self.granted_to_roles = section_data.get('GrantToRoles', fallback=PermissionsDefaults.GrantToRoles)
self.user_list = section_data.get('UserList', fallback=PermissionsDefaults.UserList)

self.max_songs = section_data.get('MaxSongs', fallback=PermissionsDefaults.MaxSongs)
self.max_song_length = section_data.get('MaxSongLength', fallback=PermissionsDefaults.MaxSongLength)
self.max_playlist_length = section_data.get('MaxPlaylistLength', fallback=PermissionsDefaults.MaxPlaylistLength)
self.max_search_items = section_data.get('MaxSearchItems', fallback=PermissionsDefaults.MaxSearchItems)

self.allow_playlists = section_data.get('AllowPlaylists', fallback=PermissionsDefaults.AllowPlaylists)
self.instaskip = section_data.get('InstaSkip', fallback=PermissionsDefaults.InstaSkip)
self.remove = section_data.get('Remove', fallback=PermissionsDefaults.Remove)
self.skip_when_absent = section_data.get('SkipWhenAbsent', fallback=PermissionsDefaults.SkipWhenAbsent)
self.bypass_karaoke_mode = section_data.get('BypassKaraokeMode', fallback=PermissionsDefaults.BypassKaraokeMode)

self.toggle_playlists = section_data.get('ToggleAutoPlaylists', fallback=PermissionsDefaults.ToggleAutoPlaylists)

self.extractors = section_data.get('Extractors', fallback=PermissionsDefaults.Extractors)
if fallback == None:
self.command_whitelist = section_data.get('CommandWhiteList', fallback=PermissionsDefaults.CommandWhiteList)
self.command_blacklist = section_data.get('CommandBlackList', fallback=PermissionsDefaults.CommandBlackList)
self.ignore_non_voice = section_data.get('IgnoreNonVoice', fallback=PermissionsDefaults.IgnoreNonVoice)
self.granted_to_roles = section_data.get('GrantToRoles', fallback=PermissionsDefaults.GrantToRoles)
self.user_list = section_data.get('UserList', fallback=PermissionsDefaults.UserList)

self.max_songs = section_data.get('MaxSongs', fallback=PermissionsDefaults.MaxSongs)
self.max_song_length = section_data.get('MaxSongLength', fallback=PermissionsDefaults.MaxSongLength)
self.max_playlist_length = section_data.get('MaxPlaylistLength', fallback=PermissionsDefaults.MaxPlaylistLength)
self.max_search_items = section_data.get('MaxSearchItems', fallback=PermissionsDefaults.MaxSearchItems)

self.allow_playlists = section_data.get('AllowPlaylists', fallback=PermissionsDefaults.AllowPlaylists)
self.instaskip = section_data.get('InstaSkip', fallback=PermissionsDefaults.InstaSkip)
self.remove = section_data.get('Remove', fallback=PermissionsDefaults.Remove)
self.skip_when_absent = section_data.get('SkipWhenAbsent', fallback=PermissionsDefaults.SkipWhenAbsent)
self.bypass_karaoke_mode = section_data.get('BypassKaraokeMode', fallback=PermissionsDefaults.BypassKaraokeMode)

self.toggle_playlists = section_data.get('ToggleAutoPlaylists', fallback=PermissionsDefaults.ToggleAutoPlaylists)

self.extractors = section_data.get('Extractors', fallback=PermissionsDefaults.Extractors)

else:
self.command_whitelist = section_data.get('CommandWhiteList', fallback=fallback['CommandWhiteList'])
self.command_blacklist = section_data.get('CommandBlackList', fallback=fallback['CommandBlackList'])
self.ignore_non_voice = section_data.get('IgnoreNonVoice', fallback=fallback['IgnoreNonVoice'])
self.granted_to_roles = section_data.get('GrantToRoles', fallback=fallback['GrantToRoles'])
self.user_list = section_data.get('UserList', fallback=fallback['UserList'])

self.max_songs = section_data.get('MaxSongs', fallback=fallback['MaxSongs'])
self.max_song_length = section_data.get('MaxSongLength', fallback=fallback['MaxSongLength'])
self.max_playlist_length = section_data.get('MaxPlaylistLength', fallback=fallback['MaxPlaylistLength'])
self.max_search_items = section_data.get('MaxSearchItems', fallback=fallback['MaxSearchItems'])

self.allow_playlists = section_data.get('AllowPlaylists', fallback=fallback['AllowPlaylists'])
self.instaskip = section_data.get('InstaSkip', fallback=fallback['InstaSkip'])
self.remove = section_data.get('Remove', fallback=fallback['Remove'])
self.skip_when_absent = section_data.get('SkipWhenAbsent', fallback=fallback['SkipWhenAbsent'])
self.bypass_karaoke_mode = section_data.get('BypassKaraokeMode', fallback=fallback['BypassKaraokeMode'])

self.toggle_playlists = section_data.get('ToggleAutoPlaylists', fallback=fallback['ToggleAutoPlaylists'])

self.extractors = section_data.get('Extractors', fallback=fallback['Extractors'])

self.validate()

Expand Down

0 comments on commit 6cd34ff

Please sign in to comment.