Skip to content

Commit

Permalink
remove role functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
purarue committed May 15, 2024
1 parent e8edbc2 commit 9bf3117
Show file tree
Hide file tree
Showing 3 changed files with 2 additions and 69 deletions.
39 changes: 0 additions & 39 deletions filmswap/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -301,20 +301,6 @@ async def leave(interaction: discord.Interaction[ClientT]) -> None:
f"User {interaction.user.id} {interaction.user.display_name} used leave"
)

# shared code to remove the role from the user, regardless of whether or not it succeeded
async def _remove_role() -> None:
if not settings.MODIFY_ROLES:
return
assert isinstance(interaction.user, discord.Member)
filmswap_role_id = bot.filmswap_role_id() # type: ignore
assert isinstance(filmswap_role_id, int)
logger.info(
f"Removing role id {filmswap_role_id} from user {interaction.user.id}"
)
await interaction.user.remove_roles(
discord.Object(id=filmswap_role_id),
reason="User tried to leave the film swap",
)

# if user uses this in dm, tell them to use it in the server instead
if interaction.guild is None:
Expand All @@ -332,13 +318,11 @@ async def _remove_role() -> None:
"Sorry, you can't leave the swap right now. Wait till the beginning of the next swap period to leave",
ephemeral=True,
)
await _remove_role()
return

try:
leave_swap(interaction.user.id)
except RuntimeError as e:
await _remove_role()
return await interaction.response.send_message(
f"Error: {e}", ephemeral=True
)
Expand All @@ -348,9 +332,6 @@ async def _remove_role() -> None:
ephemeral=True,
)

# remove film swap role from user
await _remove_role()

@bot.tree.command(name="done-watching", description="Mark your gift as watched") # type: ignore[arg-type]
async def done_watching(interaction: discord.Interaction[ClientT]) -> None:
logger.info(
Expand Down Expand Up @@ -713,18 +694,6 @@ async def on_ready() -> None:
bot.tree.copy_global_to(guild=discord.Object(id=settings.GUILD_ID))
await bot.tree.sync(guild=discord.Object(id=settings.GUILD_ID))

roles = await guild.fetch_roles()
filmswap_role = discord.utils.get(roles, name=settings.ROLE)

if filmswap_role is None:
logger.warning(
f"Could not find filmswap role, please create a role with the name '{settings.ROLE}'"
)
else:
logger.info(f"Found filmswap role: {filmswap_role}")

bot._filmswap_role = filmswap_role # type: ignore

# change bot username on boot up
assert bot.user is not None, "Bot user is None while booting up!"
await bot.user.edit(username=settings.BOT_NAME)
Expand All @@ -734,12 +703,4 @@ async def on_ready() -> None:
logger.info("Starting background tasks...")
bot.loop.create_task(background_tasks(bot))

def filmswap_role_id() -> int:
rid = bot._filmswap_role.id # type: ignore
assert rid is not None
assert isinstance(rid, int)
return rid

bot.filmswap_role_id = filmswap_role_id # type: ignore

return bot
30 changes: 2 additions & 28 deletions filmswap/manage.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,12 +94,6 @@ def get_bot(self) -> commands.Bot:
assert isinstance(self._bot, commands.Bot) # type: ignore
return self._bot # type: ignore

@property
def swap_role_id(self) -> int:
resp = self.get_bot().filmswap_role_id() # type: ignore
assert isinstance(resp, int)
return resp

@discord.ui.button(
label="Join swap",
style=discord.ButtonStyle.primary,
Expand All @@ -112,31 +106,13 @@ async def join_swap(
f"User {interaction.user.id} {interaction.user.display_name} clicked button to join swap"
)

# shared function which is called to give role to user, regardless of whether or not
# they actually joined. this is so that they are notified next time, if they tried to join
async def _add_role() -> None:
if not settings.MODIFY_ROLES:
return
logger.info(
f"Giving user {interaction.user.id} {interaction.user.name} role"
)
# this has to be done in a server
assert interaction.guild is not None
assert isinstance(interaction.user, discord.Member)

# add the film role swap to the user
await interaction.user.add_roles(
discord.Object(self.swap_role_id), reason="User joined film swap"
)

try:
join_swap(interaction.user.id, interaction.user.display_name)
except Exception as e:
logger.exception(e, exc_info=True)
await interaction.response.send_message(str(e), ephemeral=True)
# send as a DM as well, ephemeral messages are easy to miss
await interaction.user.send(str(e))
await _add_role()
self.is_finished()
return

Expand All @@ -149,8 +125,6 @@ async def _add_role() -> None:
ephemeral=True,
)

await _add_role()

self.is_finished()


Expand Down Expand Up @@ -580,7 +554,7 @@ async def send_join_message(
return

@discord.app_commands.command( # type: ignore[arg-type]
name="filmswap-ban", description="Ban a user from the swap"
name="swap-ban", description="Ban a user from the swap"
)
async def filmswap_ban(
self, interaction: discord.Interaction[ClientT], discord_user_id: str
Expand Down Expand Up @@ -621,7 +595,7 @@ async def filmswap_ban(
return

@discord.app_commands.command( # type: ignore[arg-type]
name="filmswap-unban", description="Unban a user from the swap"
name="swap-unban", description="Unban a user from the swap"
)
async def filmswap_unban(
self, interaction: discord.Interaction[ClientT], discord_user_id: str
Expand Down
2 changes: 0 additions & 2 deletions filmswap/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,9 @@ class Settings(BaseSettings):
SQL_ECHO: bool = False
GUILD_ID: int = -1
ALLOWED_ROLES: list[str] = []
ROLE: str = "film-swap"
ENVIRONMENT: str = Environment.DEVELOPMENT
BACKUP_DIR: str = "backups"
BOT_NAME: str = "FilmSwap"
MODIFY_ROLES: bool = False
PERIOD_POST_HOOK: bool = True
FILMSWAP_TOKEN: str
BACKUPS_DIR: str = "backups"
Expand Down

0 comments on commit 9bf3117

Please sign in to comment.