Skip to content

Commit

Permalink
Merge pull request #132 from Edwinexd/no-dm-errors
Browse files Browse the repository at this point in the history
Send error when a guild-only command is being ran in DMs
  • Loading branch information
eibex authored Dec 17, 2023
2 parents 9936b25 + 054774d commit 5ba2d4a
Show file tree
Hide file tree
Showing 8 changed files with 41 additions and 14 deletions.
1 change: 1 addition & 0 deletions cogs/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ def __init__(self, bot):

@commands.slash_command(name="admin", description=static_response.get("brief-admin"))
@commands.has_permissions(administrator=True)
@commands.guild_only()
async def admin(
self,
inter,
Expand Down
1 change: 1 addition & 0 deletions cogs/control.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ async def controlbot_group(self, inter):
pass

@controlbot_group.sub_command(name="version", description=static_response.get("brief-version"))
@commands.guild_only()
async def print_version(self, inter):
await inter.response.defer()
if not self.bot.isadmin(inter.author, inter.guild.id):
Expand Down
2 changes: 2 additions & 0 deletions cogs/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ def __init__(self, bot):
async def on_command_error(self, inter, error):
if isinstance(error, commands.NotOwner):
await inter.send(self.bot.response.get("not-owner", guild_id=inter.guild.id))
elif isinstance(error, commands.NoPrivateMessage):
await inter.send(self.bot.response.get("no-dm"))
else:
traceback.print_tb(error.__traceback__)
print(error)
Expand Down
1 change: 1 addition & 0 deletions cogs/help.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ def __init__(self, bot):
self.bot = bot

@commands.slash_command(name="help", description=static_response.get("brief-help"))
@commands.guild_only()
async def hlp(self, inter):
if self.bot.isadmin(inter.author, inter.guild.id):
await inter.response.defer()
Expand Down
3 changes: 3 additions & 0 deletions cogs/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ async def message_group(self, inter):
pass

@message_group.sub_command(name="new", description=static_response.get("brief-message-new"))
@commands.guild_only()
async def new(self, inter):
if not self.bot.isadmin(inter.author, inter.guild.id):
await inter.send(self.bot.response.get("new-reactionrole-noadmin", guild_id=inter.guild.id))
Expand Down Expand Up @@ -388,6 +389,7 @@ def reaction_check3(payload):
await inter.channel.send(self.bot.response.get("new-reactionrole-cancelled", guild_id=inter.guild.id))

@message_group.sub_command(name="edit", description=static_response.get("brief-message-edit"))
@commands.guild_only()
async def edit_selector(
self,
inter: disnake.ApplicationCommandInteraction,
Expand Down Expand Up @@ -578,6 +580,7 @@ async def edit_selector(
await inter.send(content=self.bot.response.get("edit-permission-error", guild_id=inter.guild.id))

@message_group.sub_command(name="reaction", description=static_response.get("brief-message-reaction"))
@commands.guild_only()
async def edit_reaction(
self,
inter,
Expand Down
33 changes: 20 additions & 13 deletions cogs/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ async def settings_group(self, inter):
pass

@settings_group.sub_command(name="systemchannel", description=static_response.get("brief-settings-systemchannel"))
@commands.guild_only()
async def set_systemchannel(
self,
inter,
Expand Down Expand Up @@ -107,6 +108,7 @@ async def set_systemchannel(
await inter.edit_original_message(content=self.bot.response.get("systemchannels-success", guild_id=inter.guild.id))

@settings_group.sub_command(name="notify", description=static_response.get("brief-settings-notify"))
@commands.guild_only()
async def toggle_notify(self, inter):
if not self.bot.isadmin(inter.author, inter.guild.id):
return
Expand Down Expand Up @@ -137,6 +139,9 @@ async def set_language(
):
await inter.response.defer()
if _range == "server":
if not inter.guild:
await inter.send(content=self.bot.response.get("no-dm-parameters").format(parameters="server"))
return
# Check admin
if not self.bot.isadmin(inter.author, inter.guild.id):
await inter.send(content=self.bot.response.get("not-admin", guild_id=inter.guild.id))
Expand All @@ -145,34 +150,35 @@ async def set_language(
else:
# Check if bot owner
if not await self.bot.is_owner(inter.author):
await inter.send(content=self.bot.response.get("not-owner", guild_id=inter.guild.id))
await inter.send(content=self.bot.response.get("not-owner", guild_id=inter.guild.id if inter.guild else None))
return

self.bot.config.update("server", "language", language)
self.bot.response.global_language = language

await inter.edit_original_message(content=self.bot.response.get("language-success", guild_id=inter.guild.id))
await inter.edit_original_message(content=self.bot.response.get("language-success", guild_id=inter.guild.id if inter.guild else None))

@commands.is_owner()
@settings_group.sub_command(name="colour", description=static_response.get("brief-settings-colour"))
async def set_colour(
self, inter, colour: str = commands.Param(description=static_response.get("settings-colour-option-colour"))
):
await inter.response.defer()
guild_id_or_none = inter.guild.id if inter.guild else None
try:
self.bot.config.botcolour = disnake.Colour(int(colour, 16))
self.bot.config.update("server", "colour", colour)
example = disnake.Embed(
title=self.bot.response.get("example-embed", guild_id=inter.guild.id),
description=self.bot.response.get("example-embed-new-colour", guild_id=inter.guild.id),
title=self.bot.response.get("example-embed", guild_id=guild_id_or_none),
description=self.bot.response.get("example-embed-new-colour", guild_id=guild_id_or_none),
colour=self.bot.config.botcolour,
)
await inter.edit_original_message(
content=self.bot.response.get("colour-changed", guild_id=inter.guild.id), embed=example
content=self.bot.response.get("colour-changed", guild_id=guild_id_or_none), embed=example
)

except ValueError:
await inter.edit_original_message(content=self.bot.response.get("colour-hex-error", guild_id=inter.guild.id))
await inter.edit_original_message(content=self.bot.response.get("colour-hex-error", guild_id=guild_id_or_none))

@commands.is_owner()
@settings_group.sub_command(name="activity", description=static_response.get("brief-settings-activity"))
Expand All @@ -185,37 +191,38 @@ async def change_activity(
activity: str = commands.Param(description=static_response.get("settings-activity-option-activity"), default=None),
):
await inter.response.defer()
guild_id_or_none = inter.guild.id if inter.guild else None
if action == "add" and activity:
if "," in activity:
await inter.send(self.bot.response.get("activity-no-commas", guild_id=inter.guild.id))
await inter.send(self.bot.response.get("activity-no-commas", guild_id=guild_id_or_none))

else:
self.bot.activities.add(activity)
await inter.send(self.bot.response.get("activity-success", guild_id=inter.guild.id).format(new_activity=activity))
await inter.send(self.bot.response.get("activity-success", guild_id=guild_id_or_none).format(new_activity=activity))
elif action == "list":
if self.bot.activities.activity_list:
formatted_list = []
for item in self.bot.activities.activity_list:
formatted_list.append(f"`{item}`")

await inter.send(
self.bot.response.get("current-activities", guild_id=inter.guild.id).format(
self.bot.response.get("current-activities", guild_id=guild_id_or_none).format(
activity_list="\n- ".join(formatted_list)
)
)

else:
await inter.send(self.bot.response.get("no-current-activities", guild_id=inter.guild.id))
await inter.send(self.bot.response.get("no-current-activities", guild_id=guild_id_or_none))
elif action == "remove" and activity:
removed = self.bot.activities.remove(activity)
if removed:
await inter.send(
self.bot.response.get("rm-activity-success", guild_id=inter.guild.id).format(activity_to_delete=activity)
self.bot.response.get("rm-activity-success", guild_id=guild_id_or_none).format(activity_to_delete=activity)
)
else:
await inter.send(self.bot.response.get("rm-activity-not-exists", guild_id=inter.guild.id))
await inter.send(self.bot.response.get("rm-activity-not-exists", guild_id=guild_id_or_none))
else:
await inter.send(self.bot.response.get("activity-add-list-remove", guild_id=inter.guild.id))
await inter.send(self.bot.response.get("activity-add-list-remove", guild_id=guild_id_or_none))


def setup(bot):
Expand Down
4 changes: 3 additions & 1 deletion i18n/en-gb.json
Original file line number Diff line number Diff line change
Expand Up @@ -156,5 +156,7 @@
"settings-activity-option-activity": "The text that appears after 'Playing' in the bot's status message (case sensitive)",
"activity-add-list-remove": "You need to provide an activity if using `add` or `remove`.",
"admin-option-action": "Use 'add', 'remove', 'list' to add/remove/view bot admins",
"admin-option-role": "The role you want to add/remove as a bot admin"
"admin-option-role": "The role you want to add/remove as a bot admin",
"no-dm": "This command cannot be used in DMs.",
"no-dm-parameters": "This command cannot be used in DMs with the parameter(s) {parameters}."
}
10 changes: 10 additions & 0 deletions tests/language_structure.json
Original file line number Diff line number Diff line change
Expand Up @@ -729,5 +729,15 @@
"admin-option-role": {
"max_length": 100,
"parameters": []
},
"no-dm": {
"max_length": 1024,
"parameters": []
},
"no-dm-parameters": {
"max_length": 1024,
"parameters": [
"parameters"
]
}
}

0 comments on commit 5ba2d4a

Please sign in to comment.