diff --git a/.idea/misc.xml b/.idea/misc.xml index d1e22ec..e557d17 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -1,4 +1,7 @@ + + \ No newline at end of file diff --git a/code/__pycache__/fun.cpython-38.pyc b/code/__pycache__/fun.cpython-38.pyc index 9c29ed5..f427ba1 100644 Binary files a/code/__pycache__/fun.cpython-38.pyc and b/code/__pycache__/fun.cpython-38.pyc differ diff --git a/code/__pycache__/mod.cpython-38.pyc b/code/__pycache__/mod.cpython-38.pyc index fa56d09..4b541fb 100644 Binary files a/code/__pycache__/mod.cpython-38.pyc and b/code/__pycache__/mod.cpython-38.pyc differ diff --git a/code/__pycache__/utils.cpython-38.pyc b/code/__pycache__/utils.cpython-38.pyc index f20159d..14bf848 100644 Binary files a/code/__pycache__/utils.cpython-38.pyc and b/code/__pycache__/utils.cpython-38.pyc differ diff --git a/code/data/auto_roles b/code/data/auto_roles new file mode 100644 index 0000000..f6a25c4 Binary files /dev/null and b/code/data/auto_roles differ diff --git a/code/data/welcome_channel b/code/data/welcome_channel new file mode 100644 index 0000000..8fe4fc8 Binary files /dev/null and b/code/data/welcome_channel differ diff --git a/code/fun.py b/code/fun.py index 82772be..97956b1 100644 --- a/code/fun.py +++ b/code/fun.py @@ -5,7 +5,8 @@ @app_commands.describe(roll_number="Le nombre de dés à lancer (Max. 100)", - roll_max="La valeur maximale des faces du dé (Max. 50)") + roll_max="Le nombre des faces des dés (Max. 50)") +@app_commands.rename(roll_number="dés-lancés", roll_max="faces") async def dice_command(interaction: discord.Interaction, roll_number: int = 1, roll_max: int = 6): """ Cette commande tire un certain nombre de dés et en resort des statistiques @@ -33,3 +34,7 @@ async def dice_command(interaction: discord.Interaction, roll_number: int = 1, r # # Ex. fun_commands = [{'name':'deuxième-test', 'description':'Hello World !', 'func':first_test}, # {'name':'salut', 'description':'Te dis salut !', 'func':hi_command}] + +if __name__ == '__main__': + import os + os.system("python main.py") diff --git a/code/main.py b/code/main.py index ce2e635..dbf05ea 100644 --- a/code/main.py +++ b/code/main.py @@ -1,5 +1,6 @@ import discord from discord import app_commands +import pickle # Ici j'importe les fichiers où l'on a défini les commandes import fun @@ -8,6 +9,7 @@ # Je définis les variables essentiels du Bot ici : intents = discord.Intents.default() +intents.members = True client = discord.Client(intents=intents) tree = app_commands.CommandTree(client) @@ -37,6 +39,48 @@ async def on_ready(): print(f"We have logged in as {client.user}") +@client.event +async def on_member_join(member: discord.Member): + """ + Cette commande s'éxecute lorsqu'un membre rejoin le serveur : + - Paramètre 'member' : Les informations du membre en question; + """ + + # Ici je récupère les données du fichier data/welcome_channel pour savoir où envoyer le message de bienvenue + try: + with open("data/welcome_channel", "rb") as file: + welcome_channels = pickle.Unpickler(file).load() + + try: + if welcome_channels[member.guild.id] is not None: + welcome_embed = discord.Embed(title="**:tada: | Bienvenue !**", + description=f"Bienvenue à {member.mention} sur" + f" **{member.guild.name}** ! Tu es le" + f" **{member.guild.member_count}eme** " + f"membre !", + color=discord.Color.red()) + + welcome_embed.set_thumbnail(url='https://cdn3.emoji.gg/emojis/6286_tada_animated.gif') + await client.get_channel(welcome_channels[member.guild.id]).send(embed=welcome_embed) + + except KeyError: + pass + + except EOFError: + pass + + try: + with open("data/auto_roles", "rb") as file: + auto_roles = pickle.Unpickler(file).load() + + for role_id in auto_roles[member.guild.id]: + role = member.guild.get_role(role_id) + await member.add_roles(role) + + except EOFError: + pass + + @tree.error async def on_command_error(interaction, error): """ @@ -50,6 +94,6 @@ async def on_command_error(interaction, error): raise error -TOKEN = "OTIxMTQ1MzU1NTYxNzQyMzk2.GHPvKg.Oc1GGhhRCaIrgduhV1oct6uTYZ3WpALrjf-pHM " +TOKEN = "" print("Launch of the Client...") client.run(TOKEN) diff --git a/code/mod.py b/code/mod.py index 007a718..b4750d3 100644 --- a/code/mod.py +++ b/code/mod.py @@ -1,15 +1,118 @@ import discord from discord import app_commands +import pickle + + +@app_commands.checks.has_permissions(administrator=True) +@app_commands.rename(channel="salon") +@app_commands.describe(channel="Le salon dans lequel tu veux que les messages de bienvenue s'affichent." + " (Laisser vide pour désactiver)") +async def welcome_command(interaction: discord.Interaction, channel: discord.TextChannel = None): + """ + Cette commande permet de définir un salon pour envoyer un mesage de bienvenue dans un salon donné : + - Paramètre 'channel' : Le salon où l'on veut envoyer les messages de bienvenues; + """ + + # Ici je récupère les informations contenues dans le fichier data/welcome_channel + try: + with open("data/welcome_channel", "rb") as file: + welcome_channels = pickle.Unpickler(file).load() + + except EOFError: + welcome_channels = {} + + if channel is not None: + welcome_channels[interaction.guild_id] = channel.id + + else: + welcome_channels[interaction.guild_id] = None + + # Ici j'enregistre les modifications + with open("data/welcome_channel", "wb") as file: + pickle.Pickler(file).dump(welcome_channels) + + if channel is not None: + embed = discord.Embed(title="**:wave: | Bienvenue**", + description=f"Le salon de bienvenue est désormais {channel.mention} !", + color=discord.Color.green()) + + else: + embed = discord.Embed(title=":wave: | Bienvenue", description="Il n'y a désormais plus de salon de bienvenue !", + color=discord.Color.green()) + + embed.set_thumbnail(url='https://cdn3.emoji.gg/emojis/6286_tada_animated.gif') + await interaction.response.send_message(embed=embed, ephemeral=True) + + +@app_commands.checks.has_permissions(administrator=True) +@app_commands.choices(mode=[ + app_commands.Choice(name="Ajouter", value=1), + app_commands.Choice(name="Supprimer", value=0) +] +) +@app_commands.describe(mode="Voulez-vous ajouter ou supprimer un rôle de l'auto-role ?") +@app_commands.describe(role="Le role en question.") +async def auto_role_setup(interaction: discord.Interaction, mode: app_commands.Choice[int], role: discord.Role): + """ + Cette commande permet de setup un system d'auto-role pour donner un role aux nouveaux membres : + - Paramètre 'mode' : Sert à décider si l'on veux enlever un role où l'ajouter à l'auto-role; + - Paramètre 'role' : Le role en question; + """ + try: + with open("data/auto_roles", "rb") as file: + auto_roles = pickle.Unpickler(file).load() + + except EOFError: + auto_roles = {} + + if mode.value == 1: + try: + roles_list = auto_roles[interaction.guild_id] + + except KeyError: + roles_list = [] + + roles_list.append(role.id) + auto_roles[interaction.guild_id] = roles_list + + embed = discord.Embed(title="**:white_check_mark: | Rôle ajouté dans l'auto-role !**", description=f"Le rôle {role.mention} a bien été ajouté à l'auto-role !", + color=discord.Color.green()) + else: + try: + roles_list = auto_roles[interaction.guild_id] + try: + roles_list.remove(role.id) + auto_roles[interaction.guild_id] = roles_list + + embed = discord.Embed(title="**:white_check_mark: | Rôle supprimé de l'auto-role !**", description=f"Le rôle {role.mention} a bien été retiré de l'auto-role !", + color=discord.Color.green()) + + except ValueError: + embed = discord.Embed(title="**:x: | Ce rôle n'était pas présent dans l'auto-role !**", color=discord.Color.red()) + + except KeyError: + embed = discord.Embed(title="**:x: | Il n'y a aucon rôle dans l'auto-role !**", color=discord.Color.red()) + + with open("data/auto_roles", "wb") as file: + pickle.Pickler(file).dump(auto_roles) + + await interaction.response.send_message(embed=embed, ephemeral=True) + # Cette ligne doit se trouver à la fin du fichier ! -mod_commands = [] +mod_commands = [{'name': 'salon-bienvenue', 'description': 'Cette commande définit un salon pour envoyer' + ' des messages de bienvenue !', 'func': welcome_command}, + {'name': 'auto-role', 'description': 'Cette commande permet de donné un rôle aux nouveaux membres !', 'func': auto_role_setup}] + # Elle sert à ajouter à "l'arbre" des commandes du Bot la commande crée : -# # Pour cela il faut y mettre un nouveau dictionnaire et y mettre les clés suivantes avec ce qui leur correspond en paramètre : # - 'name' : Le nom de ta commande (Ex. say-goodbye) # - 'description' : La description de ta commande (Ex. Never Gonna Give You Up !) # - 'func' : la fonction qui contient ta commande (Ex. say_goodbye_command) PS. Ne pas mettre les parenthèses # -# Ex. fun_commands = [{'name':'deuxième-test', 'description':'Hello World !', 'func':first_test}, +# Ex. mod_commands = [{'name':'deuxième-test', 'description':'Hello World !', 'func':first_test}, # {'name':'salut', 'description':'Te dis salut !', 'func':hi_command}] +if __name__ == '__main__': + import os + os.system("python main.py") diff --git a/code/tools/__pycache__/maths.cpython-38.pyc b/code/tools/__pycache__/maths.cpython-38.pyc index 87e5acd..8866e23 100644 Binary files a/code/tools/__pycache__/maths.cpython-38.pyc and b/code/tools/__pycache__/maths.cpython-38.pyc differ diff --git a/code/tools/maths.py b/code/tools/maths.py index 75d5b96..57249fd 100644 --- a/code/tools/maths.py +++ b/code/tools/maths.py @@ -23,16 +23,23 @@ def dice(roll_number: int, roll_max: int): for _ in range(roll_number): rolls.append(random.randint(1, roll_max)) # Simule tout les lancés de dés. - result = discord.Embed(title=":tada: | Alea jacta est !", + result = discord.Embed(title="**:tada: | Alea jacta est !**", description=f"{roll_number} dés ont été lancés avec des faces entre 1 et {roll_max}.", color=discord.Color.pink()) result.set_thumbnail(url='https://media.tenor.com/xrrd8RNhd0MAAAAj/dice-sticker.gif') - result.add_field(name=":game_die: Les résultats sont respectivement :", value=f"{'; '.join(str(item) for item in rolls)}") - result.add_field(name=":bar_chart: Statistiques :", value=f"Moyenne : {sum(rolls) / len(rolls)}\nTotal : {sum(rolls)}\n" - f"Plus grand chiffre obtenu : {sorted(rolls)[len(rolls) - 1]}\n" - f"Plus petit chiffre obtenu : {sorted(rolls)[0]}") - result.set_image(url="https://cdn.discordapp.com/attachments/717821702180044862/729449197480181810/color_seperater_thingy.gif") + if roll_number == 1: + result.add_field(name="**:game_die: Le résultat est :**", value=f"{rolls[0]};") + + else: + result.add_field(name="**:game_die: Les résultats sont respectivement :**", + value=f"{'; '.join(str(item) for item in rolls)}") + result.add_field(name="**:bar_chart: Statistiques :**", + value=f"__Moyenne__ : **{sum(rolls) / len(rolls)}**\n__Total__ : **{sum(rolls)}**\n" + f"__Plus grand chiffre obtenu__ : **{sorted(rolls)[len(rolls) - 1]}**\n" + f"__Plus petit chiffre obtenu__ : **{sorted(rolls)[0]}**") + result.set_image( + url="https://cdn.discordapp.com/attachments/717821702180044862/729449197480181810/color_seperater_thingy.gif") result.set_footer(text="Command by Futuray") - return result \ No newline at end of file + return result diff --git a/code/utils.py b/code/utils.py index f8876a7..bce4c80 100644 --- a/code/utils.py +++ b/code/utils.py @@ -1,32 +1,26 @@ +import pickle + import discord from discord import app_commands @app_commands.checks.has_permissions(administrator=True) - -@app_commands.rename(channel="salon") -@app_commands.describe(channel="Le salon dans lequel tu veux faire ton annonce.") - -@app_commands.rename(title="titre") -@app_commands.describe(title="Le titre que tu veux donner à ton annonce.") - -@app_commands.rename(announcement="annonce") -@app_commands.describe(announcement="Ton annonce.") - -@app_commands.rename(thumbnail="icone") -@app_commands.describe(thumbnail="L'icône que tu veux donner à ton annonce.") - -@app_commands.describe(image="L'image que tu veux donner à ton annonce.") - +@app_commands.rename(channel="salon", title="titre", announcement="annonce", thumbnail="icone", color="couleur") +@app_commands.describe(channel="Le salon dans lequel tu veux faire ton annonce.", + title="Le titre que tu veux donner à ton annonce.", + announcement="Ton annonce.", + thumbnail="L'icône que tu veux donner à ton annonce.", + image="L'image que tu veux donner à ton annonce.", + color="La couleur que tu veux donner à ton annonce.", + ping="Rôle que tu veux mentionner pour ton annonce.") @app_commands.choices(color=[ app_commands.Choice(name="bleu", value=1), app_commands.Choice(name="vert", value=2), app_commands.Choice(name="rouge", value=3) ]) -@app_commands.rename(color="couleur") -@app_commands.describe(color="La couleur que tu veux donner à ton message.") async def announcement_command(interaction: discord.Interaction, channel: discord.TextChannel, title: str, - announcement: str, thumbnail: discord.Attachment = None, image: discord.Attachment = None, color: app_commands.Choice[int] = 1): + announcement: str, color: app_commands.Choice[int], thumbnail: discord.Attachment = None, + image: discord.Attachment = None, ping: discord.Role = None): """ Cette commande permet à un administrateur de faire une annonce dans un salon spécifié : - Paramètre 'channel' : Le salon où l'on veut poster l'annonce; @@ -34,12 +28,16 @@ async def announcement_command(interaction: discord.Interaction, channel: discor - Paramètre 'thumbnail' (facultatif) : Icône affichée en haut à droite du message de type Embed; - Paramètre 'image' (facultatif) : Image ajoutée à l'annonce; - Paramètre 'title' : Titre de l'annonce; + - Paramètre 'ping' : (facultatif) Rôle à notifier; """ - if color.value == 1: color = discord.Color.blue() - elif color.value == 2: color = discord.Color.green() - elif color.value == 3: color = discord.Color.red() + if color.value == 1: + color = discord.Color.blue() + elif color.value == 2: + color = discord.Color.green() + elif color.value == 3: + color = discord.Color.red() - embed = discord.Embed(title=title, description=announcement, color=color) + embed = discord.Embed(title=f"**{title}**", description=announcement, color=color) if thumbnail is not None: embed.set_thumbnail(url=thumbnail.url) @@ -49,12 +47,53 @@ async def announcement_command(interaction: discord.Interaction, channel: discor embed.set_footer(icon_url=interaction.user.avatar.url, text=f"Annonce faite par {interaction.user.name} !") await channel.send(embed=embed) - validation_embed = discord.Embed(title=":white_check_mark: | Ton annonce à bien été envoyée !", color=discord.Color.green()) + if ping is not None: + if ping == interaction.guild.default_role: + msg = await channel.send(interaction.guild.default_role) + + else: + msg = await channel.send(ping.mention) + + await msg.delete() + + validation_embed = discord.Embed(title=":white_check_mark: | Ton annonce à bien été envoyée !", + description=f"L'annonce à été envoyée dans le salon {channel.mention} !", + color=discord.Color.green()) validation_embed.set_thumbnail(url="https://cdn3.emoji.gg/emojis/2121-announcement-badge.png") await interaction.response.send_message(embed=validation_embed, ephemeral=True) + +async def list_auto_roles_command(interaction: discord.Interaction): + """ + Cette commande permet d'afficher les rôles présents d'auto-role d'un serveur. + """ + try: + with open("data/auto_roles", "rb") as file: + auto_roles = pickle.Unpickler(file).load() + + try: + str_auto_roles = "" + for roleID in auto_roles[interaction.guild_id]: + role = interaction.guild.get_role(roleID) + str_auto_roles += f"- {role.mention}\n" + + embed = discord.Embed(title="**:page_with_curl: | Voici la liste des roles de l'auto-role :**", + description=str_auto_roles, color=discord.Color.green()) + embed.set_thumbnail(url="https://cdn3.emoji.gg/emojis/4854-staff-blue.png") + + except KeyError: + embed = discord.Embed(title=":x: | Il n'y a aucun auto-role sur ce serveur !", color=discord.Color.red()) + + except EOFError: + embed = discord.Embed(title=":x: | Il n'y a aucun auto-role sur ce serveur !", color=discord.Color.red()) + + await interaction.response.send_message(embed=embed, ephemeral=True) + # Cette ligne doit se trouver à la fin du fichier ! -utils_commands = [{'name':'annonce', 'description':'Cette commande permet de créer un message d\'annonce ! (Permission réquise : Administrateur)', 'func':announcement_command}] +utils_commands = [{'name': 'annonce', + 'description': 'Cette commande permet de créer un message d\'annonce ! (Permission réquise : Administrateur)', + 'func': announcement_command}, + {'name':'liste-auto-role', 'description':'Donne la liste des rôles présents dans l\'auto-role du serveur.', 'func':list_auto_roles_command}] # Elle sert à ajouter à "l'arbre" des commandes du Bot la commande crée : # # Pour cela il faut y mettre un nouveau dictionnaire et y mettre les clés suivantes avec ce qui leur correspond en paramètre : @@ -62,6 +101,9 @@ async def announcement_command(interaction: discord.Interaction, channel: discor # - 'description' : La description de ta commande (Ex. Never Gonna Give You Up !) # - 'func' : la fonction qui contient ta commande (Ex. say_goodbye_command) PS. Ne pas mettre les parenthèses # -# Ex. fun_commands = [{'name':'deuxième-test', 'description':'Hello World !', 'func':first_test}, +# Ex. utils_commands = [{'name':'deuxième-test', 'description':'Hello World !', 'func':first_test}, # {'name':'salut', 'description':'Te dis salut !', 'func':hi_command}] +if __name__ == '__main__': + import os + os.system("python main.py")