Skip to content

Commit

Permalink
adds new command to show server configs and also file cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
SonOfLope committed Jul 19, 2024
1 parent 0166e13 commit d3289a0
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 11 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ jobs:
uses: actions/checkout@v2

- name: Start containers
run: docker-compose -f "docker-compose-tests.yml" up -d
run: docker-compose -f "tests/docker-compose.yml" up -d

- name: Setup Python
uses: actions/setup-python@v2
Expand Down
File renamed without changes.
6 changes: 2 additions & 4 deletions events.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,11 @@ async def on_member_remove(member):
guild_id = guild.id
leave_msg = trema_db.get_server_leave_msg(guild_id)
if leave_msg is not None:
mention_dict = generate_mention_dict(guild, member)
leave_msg = make_mention(leave_msg, mention_dict)
welcome_chan = _get_welcome_chan(guild, trema_db)
leave_msg = make_mention(leave_msg, member)
await welcome_chan.send(leave_msg)




@trema_bot.event
async def on_ready():
logger.info('{0.user} fonctionne.'.format(trema_bot))
21 changes: 18 additions & 3 deletions slash_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ def post_to_calidum(sender_username, request_service, request_details):
def create_slash_cmds(trema_bot, trema_db, start_time, github_token):
config = _create_config_cmds(trema_db)
_create_config_reminder_cmds(trema_db, config)
_create_information_cmds(trema_bot, start_time)
_create_information_cmds(trema_bot, start_time, trema_db)
_create_management_cmds(trema_bot, trema_db)
webhook = _create_webhooks_cmds(trema_db)
request = _create_requests_cmds(trema_db, github_token)
Expand Down Expand Up @@ -198,7 +198,7 @@ async def aide(ctx):
+ "d'un paramètre.\n\n"\
+ "Certains paramètres sont des messages affichés après un évènement concernant "\
+ "un membre particulier. Pour mentionner ce membre, écrivez **@-** dans ces messages. "\
+ "Le signe **[@-]** au début d'une description indique que cette action est possible."
+ "Le signe **[@-]** au début d'une description indique que cette action est possible."
help_embed = Embed(
title=embed_title,
description=instructions,
Expand Down Expand Up @@ -406,7 +406,7 @@ async def config_reminder_delay(ctx,

await ctx.respond(embed=response_embed, ephemeral=True)

def _create_information_cmds(trema_bot, start_time):
def _create_information_cmds(trema_bot, start_time, trema_db):
@trema_bot.command(name="ping", description="Répond avec pong")
async def ping(ctx):
latency = round(trema_bot.latency * 1000)
Expand Down Expand Up @@ -440,6 +440,21 @@ async def info(ctx):

await ctx.respond(embed=help_embed)


@trema_bot.command(name="show_server_config", description="Affiche toutes les configurations du serveur")
@is_authorized(trema_db)
async def show_server_config(ctx):
guild_id = ctx.guild_id
config_values = trema_db.get_all_server_configs(guild_id)

response_embed = Embed(
title="Configurations du Serveur",
description=config_values,
color=Color.green()
)

await ctx.respond(embed=response_embed, ephemeral=True)

def _create_management_cmds(trema_bot, trema_db):

@trema_bot.command(name="annonce", description="Informations sur Trëma")
Expand Down
File renamed without changes.
6 changes: 4 additions & 2 deletions text_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,12 @@ def make_mention(text, mention_dict):
Returns:
str: the given text with mentions replaced.
"""
for placeholder, mention in mention_dict.items():
text = text.replace(placeholder, mention)
for placeholder in list(mention_dict.keys()):
if placeholder in text:
text = text.replace(placeholder, mention_dict.get(placeholder, "{mention non trouvé}"))
return text


def generate_mention_dict(guild, newMember = None):
mention_dict = {
'{server}': guild.name,
Expand Down
38 changes: 37 additions & 1 deletion trema_database.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,12 @@ def _get_collection(self, collection_name):
def _get_document_attr(self, collection, doc_id, attr_key):
collection = self._ensure_col_is_obj(collection)
document = collection.find_one({"_id": doc_id})
if document is None:
return None
attr_value = document.get(attr_key)
return attr_value


def get_leave_msg(self, welcome_id):
leave_msg = self._get_welcome_attr(welcome_id, "leave_msg")
return leave_msg
Expand Down Expand Up @@ -267,8 +270,10 @@ def create_webhook(self, webhookName, channelID, unique_url, guild_id):
def get_all_webhooks(self, guild_id):
server_collection = self._get_collection("server")
server_doc = server_collection.find_one({"_id": guild_id})
if server_doc is None:
return []
return server_doc.get("webhooks", [])

def delete_webhook(self, webhookName, guild_id):
server_collection = self._get_collection("server")
server_collection.update_one(
Expand Down Expand Up @@ -345,6 +350,37 @@ def delete_request(self, guild_id, request_id):
return False
result = requests_collection.delete_one({"guild_id": guild_id, "_id": oid})
return result.deleted_count > 0

def get_all_server_configs(self, server_id):
def safe_get(attr_value, default="Non configuré"):
return default if attr_value is None else attr_value

configs = {
"Nom du serveur": safe_get(self._get_server_attr(server_id, "name")),
"Rôle d'administrateur": safe_get(self._get_server_attr(server_id, "admin_role")),
"Date d'adhésion": safe_get(self._get_server_attr(server_id, "joined_at")),
"ID du canal d'annonces": safe_get(self._get_server_attr(server_id, "announce_chan_id")),
"ID du canal de bienvenue": safe_get(self.get_server_welcome_chan_id(server_id)),
"Message de bienvenue": safe_get(self.get_server_welcome_msg(server_id)),
"Message de départ": safe_get(self.get_server_leave_msg(server_id)),
"Message de rappel": safe_get(self.get_server_reminder_msg(server_id)),
"Délai de rappel (minutes)": safe_get(self.get_server_reminder_delay(server_id), default=0) // 60,
"Webhooks": safe_get(self.get_all_webhooks(server_id), default=[]),
}

formatted_configs = []
for key, value in configs.items():
if isinstance(value, list):
if value:
formatted_value = "\n".join([f"- {item}" for item in value])
else:
formatted_value = "Aucun"
else:
formatted_value = value

formatted_configs.append(f"**{key}**: {formatted_value}")

return "\n\n".join(formatted_configs)

mongo_user = os.getenv('MONGO_USER')
mongo_password = os.getenv('MONGO_PASSWORD')
Expand Down

0 comments on commit d3289a0

Please sign in to comment.