Skip to content

Commit

Permalink
Team Cog Fixes (#10)
Browse files Browse the repository at this point in the history
* fix: replaced BOT_ID with main bot instead of test bot

* fix: deferred all interaction responses so discord knows the interaction has been acknowledged.

* fix: changed max team size to 6
  • Loading branch information
eluric authored Mar 2, 2024
1 parent e029d2b commit 6bd7e34
Showing 1 changed file with 37 additions and 21 deletions.
58 changes: 37 additions & 21 deletions cogs/team.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
import src.queries.team as team_query
import src.queries.player as player_query

BOT_ID = 1209630493801320558
MAX_TEAM_SIZE = 5
BOT_ID = 1208986388226121849
MAX_TEAM_SIZE = 6


class Team(commands.GroupCog):
Expand All @@ -20,16 +20,18 @@ async def create_team(self, interaction: discord.Interaction, team_name: str):
user = interaction.user
guild = interaction.guild

await interaction.response.defer(ephemeral=True)

if await player_query.get_player(str(user.id)):
await interaction.response.send_message(
await interaction.followup.send(
"You are already in a team. Please leave the team before creating a new one.",
ephemeral=True,
)
return

# check team name is not already taken
if await team_query.get_team(team_name):
return await interaction.response.send_message(
return await interaction.followup.send(
f'Team "{team_name}" is already taken. Please choose a different team name.',
ephemeral=True,
)
Expand Down Expand Up @@ -60,8 +62,9 @@ async def create_team(self, interaction: discord.Interaction, team_name: str):

# give role to user
await user.add_roles(team_role)
await interaction.response.send_message(
f'Team "{team_name}" created successfully!', ephemeral=True

await interaction.followup.send(
content=f'Team "{team_name}" created successfully!', ephemeral=True
)

@app_commands.command(name="leave")
Expand All @@ -70,12 +73,14 @@ async def leave_team(self, interaction: discord.Interaction):
user = interaction.user
guild = interaction.guild

await interaction.response.defer(ephemeral=True)

# get the team name from the user
discord_id = user.id
player = await player_query.get_player(str(discord_id))

if not player:
await interaction.response.send_message(
await interaction.followup.send(
"You are not part of a team.", ephemeral=True
)
return
Expand All @@ -96,12 +101,10 @@ async def leave_team(self, interaction: discord.Interaction):
# also delete the role
team_members = await team_query.get_team_members(team_name)
if team_members:
await interaction.response.send_message(
"You have left the team.", ephemeral=True
)
await interaction.followup.send("You have left the team.", ephemeral=True)
return

await interaction.response.send_message(
await interaction.followup.send(
"You have left the team. Since there are no members left in the team, the channels will be deleted.",
ephemeral=True,
)
Expand All @@ -126,10 +129,12 @@ async def invite(
user = interaction.user
guild = interaction.guild

await interaction.response.defer(ephemeral=True)

# check user is already in a team
player = await player_query.get_player(str(user.id))
if not player:
await interaction.response.send_message(
await interaction.followup.send(
"You must be in a team to use this command.", ephemeral=True
)
return
Expand All @@ -138,16 +143,14 @@ async def invite(

# check invited user is not already in a team
if await player_query.get_player(str(invited_user.id)):
await interaction.response.send_message(
await interaction.followup.send(
"The user you're trying to invite is already in a team.", ephemeral=True
)
return

# check that team is not full
if len(await team_query.get_team_members(team_name)) == MAX_TEAM_SIZE:
await interaction.response.send_message(
"Your team is full.", ephemeral=True
)
await interaction.followup.send("Your team is full.", ephemeral=True)
return

# otherwise send an invite
Expand All @@ -173,14 +176,18 @@ async def accept_callback(interaction: discord.Interaction):
new_player = interaction.user
team = await team_query.get_team(team_name)

await interaction.response.defer()

# check team still exists
if not team:
cancel_embed = discord.Embed(
colour=discord.Color.orange(),
title=f"{team_name} Does Not Exist",
description=f"Sorry, looks like that team has been deleted :(",
)
await interaction.response.edit_message(embed=cancel_embed, view=None)
await interaction.followup.edit_message(
message_id=interaction.message.id, embed=cancel_embed, view=None
)
return

# check team is not full
Expand All @@ -191,7 +198,9 @@ async def accept_callback(interaction: discord.Interaction):
title=f"{team_name} Is Full",
description=f"You can no longer join the team :(",
)
await interaction.response.edit_message(embed=full_embed, view=None)
await interaction.followup.edit_message(
message_id=interaction.message.id, embed=full_embed, view=None
)
return

# add new user to team
Expand All @@ -204,20 +213,27 @@ async def accept_callback(interaction: discord.Interaction):
title=f"{team_name} Invitation Accepted",
description=f"You are now part of {team_name}! Join your teammates at <#{team.text_channel_id}>.",
)
await interaction.response.edit_message(embed=accept_embed, view=None)

await interaction.followup.edit_message(
message_id=interaction.message.id, embed=accept_embed, view=None
)

async def reject_callback(interaction: discord.Interaction):
await interaction.response.defer()

reject_embed = discord.Embed(
color=discord.Color.red(),
title=f"{team_name} Invitation Rejected",
)
await interaction.response.edit_message(embed=reject_embed, view=None)
await interaction.followup.edit_message(
message_id=interaction.message.id, embed=reject_embed, view=None
)

accept_btn.callback = accept_callback
reject_btn.callback = reject_callback

await invited_user.send(embed=embed, view=view)
await interaction.response.send_message(
await interaction.followup.send(
f"{invited_user} has been invited to your team.", ephemeral=True
)

Expand Down

0 comments on commit 6bd7e34

Please sign in to comment.