-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add reaction role functionality (#105)
* Add reaction role functionality Fixes #11 Add functionality for role assignment based on reactions. - Add `addreactionrole` and `removereactionrole` commands in `bot/botcommands/member.py` to link and unlink reactions to roles. - Update `bot/management/logging.py` to handle role assignment and removal based on reactions. - Add utility functions `add_reaction_role` and `remove_reaction_role` in `bot/util.py` for managing reaction roles. - Add `ReactionRole` model in `bot/db/models/reaction_role.py` to define the structure for reaction roles. - Add `ReactionRoleService` class in `bot/db/services/reaction_role_service.py` to handle database operations for reaction roles. --- For more details, open the [Copilot Workspace session](https://copilot-workspace.githubnext.com/ADEPT-Informatique/adeptbot/issues/11?shareId=XXXX-XXXX-XXXX-XXXX). * Fixed workspace-copilot PR + cleaned up some code and project settings * pylint workflow fix * Fixed pylint issue * workaround last pylint error, made parsed_time keyword-only
- Loading branch information
1 parent
53f07e3
commit ac28cd8
Showing
22 changed files
with
281 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,12 @@ | ||
{ | ||
"python.analysis.importFormat": "absolute", | ||
"editor.defaultFormatter": "ms-python.black-formatter", | ||
"files.trimTrailingWhitespace": true, | ||
"editor.formatOnSave": true, | ||
"black-formatter.args": ["--line-length", "120", "--target-version", "py311"] | ||
|
||
"isort.args": ["--profile", "black"], | ||
"python.analysis.importFormat": "absolute", | ||
"editor.codeActionsOnSave": { | ||
"source.organizeImports": "always", | ||
"source.fixAll": "always" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
"""This module contains the commands related to the members of the server.""" | ||
|
||
import discord | ||
from discord.ext import commands | ||
from discord.ext.commands.context import Context | ||
|
||
import configs | ||
from bot.botcommands.utils.validators import has_at_least_role | ||
from bot.db.services.reaction_role_service import ReactionRoleService | ||
from bot.util import AdeptBotException | ||
|
||
|
||
class ReactionRoleCog(commands.Cog): | ||
"""This class contains the commands related to the members of the server.""" | ||
|
||
def __init__(self, bot: discord.Client) -> None: | ||
self.bot = bot | ||
self.reaction_role_service = ReactionRoleService() | ||
|
||
@commands.command() | ||
@has_at_least_role(configs.ADMIN_ROLE) | ||
async def addreactionrole(self, ctx: Context, message_id: int, emoji: str, role: discord.Role): | ||
""" | ||
Cette commande permet d'ajouter une réaction à un message et de la lier à un rôle. | ||
Utilisation: | ||
!addreactionrole <message_id> <emoji> <role_id> | ||
""" | ||
message = await ctx.fetch_message(message_id) | ||
|
||
if not message or not role: | ||
raise AdeptBotException("Message ou rôle invalide!") | ||
|
||
await message.add_reaction(emoji) | ||
await self.reaction_role_service.add_reaction_role(message_id, emoji, role.id) | ||
await ctx.send(f"Réaction {emoji} ajoutée au message {message.jump_url} et liée au rôle {role.name}.") | ||
|
||
@commands.command() | ||
@has_at_least_role(configs.ADMIN_ROLE) | ||
async def removereactionrole(self, ctx: Context, message_id: int, emoji: str): | ||
""" | ||
Cette commande permet de retirer une réaction d'un message et de supprimer le lien avec un rôle. | ||
Utilisation: | ||
!removereactionrole <message_id> <emoji> | ||
""" | ||
message = await ctx.fetch_message(message_id) | ||
|
||
if not message: | ||
raise AdeptBotException("Message invalide!") | ||
|
||
await message.clear_reaction(emoji) | ||
await self.reaction_role_service.remove_reaction_role(message_id, emoji) | ||
await ctx.send(f"Réaction {emoji} retirée du message {message.jump_url}.") | ||
|
||
@commands.Cog.listener() | ||
async def on_raw_reaction_add(self, payload: discord.RawReactionActionEvent): | ||
"""This event is called when a reaction is added to a message.""" | ||
if payload.member.bot: | ||
return | ||
|
||
reaction_role = await self.reaction_role_service.get_reaction_role(payload.message_id, str(payload.emoji)) | ||
if reaction_role: | ||
guild = self.bot.get_guild(payload.guild_id) | ||
role = guild.get_role(reaction_role.role_id) | ||
await payload.member.add_roles(role) | ||
|
||
@commands.Cog.listener() | ||
async def on_raw_reaction_remove(self, payload: discord.RawReactionActionEvent): | ||
"""This event is called when a reaction is removed from a message.""" | ||
guild = self.bot.get_guild(payload.guild_id) | ||
member = guild.get_member(payload.user_id) | ||
if member.bot: | ||
return | ||
|
||
reaction_role = await self.reaction_role_service.get_reaction_role(payload.message_id, str(payload.emoji)) | ||
if reaction_role: | ||
role = guild.get_role(reaction_role.role_id) | ||
await member.remove_roles(role) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
"""ReactionRole model for linking reactions to roles.""" | ||
|
||
from bot.db.models.entity import Entity | ||
|
||
|
||
class ReactionRole(Entity): | ||
""" | ||
ReactionRole model for linking reactions to roles. | ||
Attributes | ||
---------- | ||
`message_id` : int | ||
The ID of the message. | ||
`emoji` : str | ||
The emoji used for the reaction. | ||
`role_id` : int | ||
The ID of the role to assign. | ||
""" | ||
|
||
__slots__ = ("message_id", "emoji", "role_id") | ||
|
||
def __init__(self, _id: int, message_id: int, emoji: str, role_id: int): | ||
super().__init__(_id) | ||
self.message_id = message_id | ||
self.emoji = emoji | ||
self.role_id = role_id | ||
|
||
def __getstate__(self): | ||
state = super().__getstate__() | ||
state.update( | ||
{ | ||
"message_id": self.message_id, | ||
"emoji": self.emoji, | ||
"role_id": self.role_id, | ||
} | ||
) | ||
return state |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
"""Service class for ReactionRole model.""" | ||
|
||
from bot.db.models.reaction_role import ReactionRole | ||
from bot.db.services.base_service import BaseService | ||
|
||
|
||
class ReactionRoleService(BaseService): | ||
""" | ||
Service class for ReactionRole model. | ||
Methods | ||
------- | ||
`add_reaction_role` : None | ||
Add a reaction role to the database. | ||
`remove_reaction_role` : None | ||
Remove a reaction role from the database. | ||
`get_reaction_role` : ReactionRole | ||
Get a reaction role from the database. | ||
""" | ||
|
||
@property | ||
def collection_name(self): | ||
return "reaction_roles" | ||
|
||
async def add_reaction_role(self, message_id: int, emoji: str, role_id: int): | ||
""" | ||
Add a reaction role to the database. | ||
Parameters | ||
---------- | ||
`message_id` : int | ||
The ID of the message. | ||
`emoji` : str | ||
The emoji used for the reaction. | ||
`role_id` : int | ||
The ID of the role to assign. | ||
""" | ||
reaction_role = ReactionRole(None, message_id, emoji, role_id) | ||
self.insert_one(reaction_role.__getstate__()) | ||
|
||
async def remove_reaction_role(self, message_id: int, emoji: str): | ||
""" | ||
Remove a reaction role from the database. | ||
Parameters | ||
---------- | ||
`message_id` : int | ||
The ID of the message. | ||
`emoji` : str | ||
The emoji used for the reaction. | ||
""" | ||
self.delete_one({"message_id": message_id, "emoji": emoji}) | ||
|
||
async def get_reaction_role(self, message_id: int, emoji: str) -> ReactionRole | None: | ||
""" | ||
Get a reaction role from the database. | ||
Parameters | ||
---------- | ||
`message_id` : int | ||
The ID of the message. | ||
`emoji` : str | ||
The emoji used for the reaction. | ||
Returns | ||
------- | ||
ReactionRole | ||
The reaction role. | ||
""" | ||
result = self.find_one({"message_id": message_id, "emoji": emoji}) | ||
if result: | ||
return ReactionRole(result["_id"], result["message_id"], result["emoji"], result["role_id"]) | ||
return None |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.