Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adding interaction check to config embed, making channel optional, and some logic changes #350

Merged
merged 22 commits into from
Aug 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
4319405
adding interaction check to config embed and small logic changes
imtherealF1 Aug 22, 2024
bf89c5f
making channel optional, if no channel the bot uses interaction channel
imtherealF1 Aug 22, 2024
329eb2b
3 lines into 1 for black checks to pass
imtherealF1 Aug 22, 2024
dbeda9b
changing the code for pyright
imtherealF1 Aug 22, 2024
eed31c9
changes for pyright checks to pass
imtherealF1 Aug 22, 2024
e3f937f
removing extra newline at the end of file
imtherealF1 Aug 22, 2024
eb84ee5
checking if bot has the correct permissions with a decorator
imtherealF1 Aug 22, 2024
1b23b07
passing discord.Member instead of Player
imtherealF1 Aug 22, 2024
1d25125
more changes for pyright
imtherealF1 Aug 22, 2024
4e2bcd1
even more changes for pyright
imtherealF1 Aug 22, 2024
ad3a00e
ensuring channel is a Text.Channel and not interaction.channel
imtherealF1 Aug 22, 2024
46adfca
changing the code to access the disabled attribute
imtherealF1 Aug 22, 2024
6780fa4
making sure the channel is a text channel
imtherealF1 Aug 22, 2024
8697418
changing cmd description and making code easier and simpler
imtherealF1 Aug 22, 2024
cadafe2
small change in the bot's response
imtherealF1 Aug 22, 2024
6a8b5d9
re-adding a description that i accidentally removed
imtherealF1 Aug 23, 2024
b1ae108
leaving whitespace between description and init
imtherealF1 Aug 24, 2024
164bd4d
adding user permission checks in a decorator
imtherealF1 Aug 29, 2024
d7396de
removing an unnecessary "self.stop()"
imtherealF1 Aug 29, 2024
bf41499
adding permission checks in decorators in /config disable
imtherealF1 Aug 30, 2024
436c047
removing unnecessary permission checks
imtherealF1 Aug 30, 2024
0264074
Merge branch 'master' into pr/config/interaction-check
flaree Aug 30, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 41 additions & 35 deletions ballsdex/packages/config/cog.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import TYPE_CHECKING, cast
from typing import TYPE_CHECKING, Optional, cast

import discord
from discord import app_commands
Expand Down Expand Up @@ -36,53 +36,52 @@ def __init__(self, bot: "BallsDexBot"):
self.bot = bot

@app_commands.command()
@app_commands.describe(channel="The new text channel to set.")
@app_commands.checks.has_permissions(manage_guild=True)
@app_commands.checks.bot_has_permissions(
read_messages=True,
send_messages=True,
embed_links=True,
)
async def channel(
self,
interaction: discord.Interaction,
channel: discord.TextChannel,
channel: Optional[discord.TextChannel] = None,
):
"""
Set or change the channel where countryballs will spawn.

Parameters
----------
channel: discord.TextChannel
The channel you want to set, current one if not specified.
"""
guild = cast(discord.Guild, interaction.guild) # guild-only command
user = cast(discord.Member, interaction.user)
if not user.guild_permissions.manage_guild:
await interaction.response.send_message(
"You need the permission to manage the server to use this."
)
return
if not channel.permissions_for(guild.me).read_messages:
await interaction.response.send_message(
f"I need the permission to read messages in {channel.mention}."
)
return
if not channel.permissions_for(guild.me).send_messages:
await interaction.response.send_message(
f"I need the permission to send messages in {channel.mention}."
)
return
if not channel.permissions_for(guild.me).embed_links:
await interaction.response.send_message(
f"I need the permission to send embed links in {channel.mention}."
)
return

if channel is None:
if isinstance(interaction.channel, discord.TextChannel):
channel = interaction.channel
else:
await interaction.response.send_message(
"The current channel is not a valid text channel.", ephemeral=True
)
return

view = AcceptTOSView(interaction, channel, user)
message = await channel.send(embed=activation_embed, view=view)
view.message = message

await interaction.response.send_message(
embed=activation_embed, view=AcceptTOSView(interaction, channel)
f"The activation embed has been sent in {channel.mention}.", ephemeral=True
)

@app_commands.command()
@app_commands.checks.has_permissions(manage_guild=True)
@app_commands.checks.bot_has_permissions(send_messages=True)
async def disable(self, interaction: discord.Interaction):
"""
Disable or enable countryballs spawning.
"""
guild = cast(discord.Guild, interaction.guild) # guild-only command
user = cast(discord.Member, interaction.user)
if not user.guild_permissions.manage_guild:
await interaction.response.send_message(
"You need the permission to manage the server to use this."
)
return
config, created = await GuildConfig.get_or_create(guild_id=interaction.guild_id)
if config.enabled:
config.enabled = False # type: ignore
Expand All @@ -98,10 +97,17 @@ async def disable(self, interaction: discord.Interaction):
await config.save()
self.bot.dispatch("ballsdex_settings_change", guild, enabled=True)
if config.spawn_channel and (channel := guild.get_channel(config.spawn_channel)):
await interaction.response.send_message(
f"{settings.bot_name} is now enabled in this server, "
f"{settings.collectible_name}s will start spawning soon in {channel.mention}."
)
if channel:
await interaction.response.send_message(
f"{settings.bot_name} is now enabled in this server, "
f"{settings.collectible_name}s will start spawning "
f"soon in {channel.mention}."
)
else:
await interaction.response.send_message(
"The spawning channel specified in the configuration is not available.",
ephemeral=True,
)
else:
await interaction.response.send_message(
f"{settings.bot_name} is now enabled in this server, however there is no "
Expand Down
53 changes: 34 additions & 19 deletions ballsdex/packages/config/components.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from typing import Optional

import discord
from discord.ui import Button, View, button

Expand All @@ -10,10 +12,18 @@ class AcceptTOSView(View):
Button prompting the admin setting up the bot to accept the terms of service.
"""

def __init__(self, interaction: discord.Interaction, channel: discord.TextChannel):
def __init__(
self,
interaction: discord.Interaction,
channel: discord.TextChannel,
imtherealF1 marked this conversation as resolved.
Show resolved Hide resolved
new_player: discord.Member,
):
super().__init__()
self.original_interaction = interaction
self.channel = channel
self.new_player = new_player
self.message: Optional[discord.Message] = None

self.add_item(
Button(
style=discord.ButtonStyle.link,
Expand All @@ -29,40 +39,45 @@ def __init__(self, interaction: discord.Interaction, channel: discord.TextChanne
)
)

async def interaction_check(self, interaction: discord.Interaction) -> bool:
if interaction.user.id != self.new_player.id:
await interaction.response.send_message(
"You are not allowed to interact with this menu.", ephemeral=True
)
return False
return True

@button(
label="Accept",
style=discord.ButtonStyle.success,
emoji="\N{HEAVY CHECK MARK}\N{VARIATION SELECTOR-16}",
)
async def accept_button(self, interaction: discord.Interaction, item: discord.ui.Button):
async def accept_button(self, interaction: discord.Interaction, button: discord.ui.Button):
config, created = await GuildConfig.get_or_create(guild_id=interaction.guild_id)
config.spawn_channel = self.channel.id # type: ignore
await config.save()
interaction.client.dispatch(
"ballsdex_settings_change", interaction.guild, channel=self.channel
)
self.stop()
if self.message:
button.disabled = True
try:
await self.message.edit(view=self)
except discord.HTTPException:
pass
await interaction.response.send_message(
f"The new spawn channel was successfully set to {self.channel.mention}.\n"
f"{settings.collectible_name.title()}s will start spawning as"
" users talk unless the bot is disabled."
)

self.accept_button.disabled = True
try:
await self.original_interaction.followup.edit_message(
"@original", view=self # type: ignore
)
except discord.HTTPException:
pass

async def on_timeout(self) -> None:
self.stop()
for item in self.children:
item.disabled = True # type: ignore
try:
await self.original_interaction.followup.edit_message(
"@original", view=self # type: ignore
)
except discord.HTTPException:
pass
if self.message:
for item in self.children:
if isinstance(item, discord.ui.Button):
item.disabled = True
try:
await self.message.edit(view=self)
except discord.HTTPException:
pass
Loading