Skip to content

Commit

Permalink
2
Browse files Browse the repository at this point in the history
  • Loading branch information
paris-ci committed Apr 29, 2024
1 parent 5bfb92b commit a8ceeae
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 61 deletions.
41 changes: 29 additions & 12 deletions src/cogs/ducks_hunting_commands.py
Expand Up @@ -268,9 +268,13 @@ async def bang(self, ctx: MyContext, target: Optional[SmartMemberConverter], *ar
msg = _("💥 Your weapon jammed. Reload it and ponder how unlucky you are.")
await CommandView(
self.bot,
command_to_be_ran="reload",
label="Reload",
style=ButtonStyle.blurple,
commands=[{
"command": "reload",
"button_kwargs": {
"label": "Reload",
"style": ButtonStyle.blurple,
},
}],
).send(
ctx,
content=msg,
Expand Down Expand Up @@ -410,9 +414,9 @@ async def bang(self, ctx: MyContext, target: Optional[SmartMemberConverter], *ar
player_name = db_target.member.user.name

if (
not murder
and target_coat_color == Coats.ORANGE
and random.randint(1, 100) <= 75
not murder
and target_coat_color == Coats.ORANGE
and random.randint(1, 100) <= 75
):
db_hunter.shooting_stats["near_misses"] += 1
db_target.shooting_stats["near_missed"] += 1
Expand All @@ -430,7 +434,7 @@ async def bang(self, ctx: MyContext, target: Optional[SmartMemberConverter], *ar
return

elif (
hunter_coat_color == Coats.PINK and target_coat_color == Coats.PINK
hunter_coat_color == Coats.PINK and target_coat_color == Coats.PINK
):
if murder:
db_hunter.shooting_stats["murders"] -= 1 # Cancel the murder
Expand Down Expand Up @@ -463,7 +467,7 @@ async def bang(self, ctx: MyContext, target: Optional[SmartMemberConverter], *ar
return

has_valid_kill_licence = (
db_hunter.is_powerup_active("kill_licence") and not murder
db_hunter.is_powerup_active("kill_licence") and not murder
)

db_hunter.shooting_stats["killed"] += 1
Expand Down Expand Up @@ -700,9 +704,22 @@ async def reload(self, ctx: MyContext):
await db_hunter.save()
await CommandView(
self.bot,
command_to_be_ran="shop magazine",
label="Buy magazine (13xp)",
style=ButtonStyle.blurple,
commands=[
{
"command": "shop magazine",
"button_kwargs": {
"label": "Buy magazine (13xp)",
"style": ButtonStyle.blurple,
},
},
{
"command": "inv use mags",
"button_kwargs": {
"label": "Use a magazine (from your inventory)",
"style": ButtonStyle.blurple,
},
}
],
).send(
ctx,
content=_(
Expand All @@ -720,7 +737,7 @@ async def reload(self, ctx: MyContext):
@commands.command()
@checks.channel_enabled()
async def hug(
self, ctx: MyContext, target: Optional[Union[discord.Member, discord.Role, str]], *args
self, ctx: MyContext, target: Optional[Union[discord.Member, discord.Role, str]], *args
):
"""
Hug the duck that appeared first on the channel.
Expand Down
117 changes: 68 additions & 49 deletions src/utils/views.py
Expand Up @@ -11,7 +11,7 @@


async def get_context_from_interaction(
bot: MyBot, interaction: Interaction
bot: MyBot, interaction: Interaction
) -> MyContext:
"""
Return a modified, probably invalid but "good enough" context for use with the commands extensions.
Expand All @@ -23,10 +23,10 @@ async def get_context_from_interaction(

if fake_message is None:
fake_message = (
interaction.channel.last_message
or await interaction.channel.fetch_message(
interaction.channel.last_message_id
)
interaction.channel.last_message
or await interaction.channel.fetch_message(
interaction.channel.last_message_id
)
)
bot.logger.debug("Got last message from cache/network for get_context")
fake_message.author = interaction.user
Expand All @@ -48,7 +48,7 @@ class BigButtonMixin(ui.Button):
"""

def __init__(
self, *args, button_pad_to: int = 78, label: Optional[str] = None, **kwargs
self, *args, button_pad_to: int = 78, label: Optional[str] = None, **kwargs
):
"""
Adds a new argument to button creation : the total length of the button (invalid if higher than 80).
Expand Down Expand Up @@ -91,7 +91,7 @@ async def callback(self, interaction: Interaction):

class CommandButton(AutomaticDeferMixin, ui.Button):
def __init__(
self, bot, command, command_args=None, command_kwargs=None, *args, **kwargs
self, bot, command, command_args=None, command_kwargs=None, *args, **kwargs
):
"""
Button to execute a command. A list of args and kwargs can be passed to run the command with those args.
Expand Down Expand Up @@ -257,52 +257,71 @@ class CommandView(AuthorizedUserMixin, View):
"""

def __init__(
self,
bot: MyBot,
command_to_be_ran: Union[Command, str],
command_args: Optional[List[Any]] = None,
command_kwargs: Optional[Dict[str, Any]] = None,
authorized_users: Iterable[int] = "__all__",
persist: Union[bool, str] = True,
**button_kwargs,
self,
bot: MyBot,
command_to_be_ran: Union[Command, str] = None,
command_args: Optional[List[Any]] = None,
command_kwargs: Optional[Dict[str, Any]] = None,
commands: List[Dict] = None,
authorized_users: Iterable[int] = "__all__",
persist: Union[bool, str] = True,
**button_kwargs,
):

super().__init__(bot)

if isinstance(command_to_be_ran, str):
command_to_be_ran = bot.get_command(command_to_be_ran)

if command_to_be_ran is None:
raise RuntimeError("The command passed can't be found.")

if (
isinstance(persist, bool)
and command_args
or command_kwargs
or authorized_users != "__all__"
):
persist = False

if isinstance(persist, str):
persistance_id = persist
elif persist:
persistance_id = f"cmd:cn{command_to_be_ran.qualified_name}"
else:
persistance_id = None

self.authorized_users_ids = authorized_users

self.add_item(
CommandButton(
bot,
command_to_be_ran,
command_args,
command_kwargs,
custom_id=persistance_id,
row=0,
**button_kwargs,
if commands is not None and command_to_be_ran is not None:
raise ValueError("You can't pass both a command and a list of commands.")

if commands is None:
commands = [
{
"command": command_to_be_ran,
"command_args": command_args,
"command_kwargs": command_kwargs,
"button_kwargs": button_kwargs,
}
]

for command_data in commands:
command_to_be_ran = command_data["command"]
command_args = command_data.get("command_args", None)
command_kwargs = command_data.get("command_kwargs", None)
button_kwargs = command_data.get("button_kwargs", {})

if isinstance(command_to_be_ran, str):
command_to_be_ran = bot.get_command(command_to_be_ran)

if command_to_be_ran is None:
raise RuntimeError("The command passed can't be found.")

if (
isinstance(persist, bool)
and command_args
or command_kwargs
or authorized_users != "__all__"
):
persist = False

if isinstance(persist, str):
persistance_id = persist
elif persist:
persistance_id = f"cmd:cn{command_to_be_ran.qualified_name}"
else:
persistance_id = None

self.authorized_users_ids = authorized_users

self.add_item(
CommandButton(
bot,
command_to_be_ran,
command_args,
command_kwargs,
custom_id=persistance_id,
row=0,
**button_kwargs,
)
)
)


class ConfirmView(DisableViewOnTimeoutMixin, AuthorizedUserMixin, View):
Expand Down

0 comments on commit a8ceeae

Please sign in to comment.