Skip to content

Commit

Permalink
roles, god damn it
Browse files Browse the repository at this point in the history
  • Loading branch information
geirawsm committed Jul 30, 2024
1 parent 5f8e61a commit f86bf6e
Showing 1 changed file with 101 additions and 22 deletions.
123 changes: 101 additions & 22 deletions sausage_bot/cogs/roles.py
Original file line number Diff line number Diff line change
Expand Up @@ -1065,24 +1065,19 @@ async def add_reaction_message(
reac[0], type(reac[0])
)
)
print(reac[0])
print(reac[1])
reactions_in.append(
(reaction_msg.id, reac[0], reac[1])
(
reaction_msg.id, reac[0], reac[1]
)
)
log.debug(f'Adding emoji {reac[1]}')
await reaction_msg.add_reaction(reac[1])
await db_helper.insert_many_all(
envs.roles_db_roles_schema,
inserts=reactions_in
)
# TODO Gå over fra posting til reordering?
for reaction in reactions_in:
log.debug(f'Adding emoji {reaction[2]}')
await reaction_msg.add_reaction(
get(
discord_commands.get_guild().emojis,
id=int(re.search(
r'<:.*:(\d+)>', reaction[2]
)[1])
)
)
# Add to messages DB
await db_helper.insert_many_all(
envs.roles_db_msgs_schema,
Expand Down Expand Up @@ -1231,8 +1226,8 @@ async def sort_reaction_items(
commands.is_owner(),
commands.has_permissions(manage_roles=True)
)
@roles_reaction_group.command(
name='remove_message', description='Remove a reaction message'
@roles_reaction_remove_group.command(
name='message', description='Remove a reaction message'
)
@discord.app_commands.autocomplete(reaction_msg=reaction_msgs_autocomplete)
async def remove_reaction_message(
Expand All @@ -1244,7 +1239,7 @@ async def remove_reaction_message(
Parameters
------------
reaction_msg: int/str
The message ID from Discord or name in the database
w The message ID from Discord or name in the database
'''
await interaction.response.defer(ephemeral=True)
# Get message object
Expand Down Expand Up @@ -1443,12 +1438,25 @@ async def add_settings(
await interaction.response.defer(ephemeral=True)
if setting == 'Unique role':
_setting = 'unique'
# TODO Add a check, unique should only appear once
unique = await db_helper.get_output(
template_info=envs.roles_db_settings_schema,
select=('value'),
where=('setting', 'unique'),
single=True
)
if unique:
await interaction.followup.send(
# TODO i18n
'Unique role already set, remove or edit it'
)
return
elif setting == 'Not include in total':
_setting = 'not_include_in_total'
await db_helper.insert_many_all(
template_info=envs.roles_db_settings_schema,
inserts=[
(_setting, str(role_in.id))
(_setting, role_in)
]
)
# TODO i18n
Expand All @@ -1472,7 +1480,7 @@ async def remove_settings(
Parameters
------------
setting: typing.Literal
setting: str
The setting to remove
'''
await interaction.response.defer(ephemeral=True)
Expand All @@ -1484,6 +1492,72 @@ async def remove_settings(
await interaction.followup.send(f'Removed setting')
return

@commands.check_any(
commands.is_owner(),
commands.has_permissions(manage_roles=True)
)
@roles_settings_group.command(
name='list', description='List settings for roles on the server'
)
async def list_settings(
self, interaction: discord.Interaction
):
'''
List settings for roles on the server
'''
await interaction.response.defer(ephemeral=True)

settings_db = await db_helper.get_output(
template_info=envs.roles_db_settings_schema
)
_settings_db_expanded = []
for setting in settings_db:
_role = get(
discord_commands.get_guild().roles,
id=int(setting[1])
)
_settings_db_expanded.append(
(setting[0], _role, setting[1])
)
_settings = tabulate(_settings_db_expanded, headers=['Setting', 'Role', 'Value'])
# TODO i18n
await interaction.followup.send(f'```{_settings}```')
return

@commands.check_any(
commands.is_owner(),
commands.has_permissions(manage_roles=True)
)
@discord.app_commands.autocomplete(setting=settings_autocomplete)
@roles_settings_group.command(
name='edit', description='Edit a setting for roles on the server'
)
async def edit_settings(
self, interaction: discord.Interaction,
setting: str, role_in: discord.Role
):
'''
Edit a setting for roles on the server
Parameters
------------
setting: str
The setting to edit
role_in: discord.Role
The role to add to the setting
'''
await interaction.response.defer(ephemeral=True)
await db_helper.update_fields(
template_info=envs.roles_db_settings_schema,
where=('setting', setting),
updates=('value', str(role_in.id))
)
# TODO i18n
await interaction.followup.send(
f'Edited setting ({setting} = {role_in.id})'
)
return


async def setup(bot):
# Create necessary databases before starting
Expand Down Expand Up @@ -1668,6 +1742,7 @@ async def on_member_update(before, after):
where=('setting', 'unique'),
single=True
)
log.debug(f'Got `unique_role`: {unique_role}')
if not unique_role or unique_role == '':
# TODO var msg
log.log('No unique role provided or setting is not string')
Expand All @@ -1689,7 +1764,7 @@ async def on_member_update(before, after):
', '.join(role.name for role in after.roles)
))
if len(after.roles) and all(
unique_role[0] == role.id for role in after.roles
unique_role == role.id for role in after.roles
):
log.debug('Only the unique role was added')
return
Expand All @@ -1706,21 +1781,25 @@ async def on_member_update(before, after):
log.debug('Found roles not to include in total')
_before -= len(not_include_in_total)
_after -= len(not_include_in_total)
if any(str(unique_role[0]) == role for role in before.roles):
if any(str(unique_role) == role for role in before.roles):
_before -= 1
elif any(str(unique_role[0]) == role for role in after.roles):
elif any(str(unique_role) == role for role in after.roles):
_after -= 1
log.verbose('before and after, minus unique role:')
log.verbose(f'_before: {_before}')
log.verbose(f'_after: {_after}')
if int(_after) == 0:
# TODO var msg
log.debug('Length of _after is 0, adding unique role')
await after.add_roles(_guild.get_role(int(unique_role[0])))
await after.add_roles(
get(_guild.roles, id=int(unique_role))
)
elif int(_after) > 1:
# TODO var msg
log.debug(
'Length of after.roles is more than 1, removing unique role'
)
await after.remove_roles(_guild.get_role(int(unique_role[0])))
await after.remove_roles(
get(_guild.roles, id=int(unique_role))
)
return

0 comments on commit f86bf6e

Please sign in to comment.