From c4d6db0d0014cf6e1059a6342e8abe946e2019f8 Mon Sep 17 00:00:00 2001
From: dnns01 <mail@dnns01.de>
Date: Fri, 17 Jul 2020 21:10:30 +0200
Subject: [PATCH] Extracted help as dict to be loaded when !help is invoked.

---
 fernuni-bot.py | 77 +++++++++++++++++++++++---------------------------
 1 file changed, 35 insertions(+), 42 deletions(-)

diff --git a/fernuni-bot.py b/fernuni-bot.py
index 6b92317..d676832 100644
--- a/fernuni-bot.py
+++ b/fernuni-bot.py
@@ -5,21 +5,24 @@ import discord
 from discord.ext import commands
 from dotenv import load_dotenv
 
-# .env file is necessary in the same directory, that contains Token and guild.
+# .env file is necessary in the same directory, that contains several strings.
 load_dotenv()
 TOKEN = os.getenv('DISCORD_TOKEN')
 GUILD = int(os.getenv('DISCORD_GUILD'))
 ACTIVITY = os.getenv('DISCORD_ACTIVITY')
 OWNER = int(os.getenv('DISCORD_OWNER'))
 ROLES_FILE = os.getenv('DISCORD_ROLES_FILE')
+HELP_FILE = os.getenv('DISCORD_HELP_FILE')
+
 PIN_EMOJI = "📌"
 bot = commands.Bot(command_prefix='!', help_command=None, activity=discord.Game(ACTIVITY), owner_id=OWNER)
 assignable_roles = {}
 
 
-# Returns an guild object, that matches the id specified in GUILD.
-# This guild is the FU Hagen Informatik/Mathematik guild.
 def get_guild():
+    """ Returns an guild object, that matches the id specified in GUILD.
+    This guild is the FU Hagen Informatik/Mathematik guild."""
+
     for guild in bot.guilds:
         if guild.id == GUILD:
             return guild
@@ -27,15 +30,17 @@ def get_guild():
     return None
 
 
-# Get the key for a given role. This role is used for adding or removing a role from a user.
 def get_key(role):
+    """ Get the key for a given role. This role is used for adding or removing a role from a user. """
+
     for key, role_name in assignable_roles.items():
         if role_name == role.name:
             return key
 
 
-# Get all roles that are available at the guild.
 def get_guild_roles():
+    """ Get all roles that are available at the guild. """
+
     guild = get_guild()
     if guild is not None:
         roles = {}
@@ -57,25 +62,28 @@ def get_member(user):
     return None
 
 
-# Get all roles assigned to a member.
 def get_members_roles(user):
+    """ Get all roles assigned to a member. """
+
     member = get_member(user)
     if member is not None:
         return member.roles
     return None
 
 
-# Returns the reaction, that is equal to the specified PIN_EMOJI,
-# or if that reaction does not exist in list of reactions, None will be returned
 def get_reaction(reactions):
+    """ Returns the reaction, that is equal to the specified PIN_EMOJI,
+    or if that reaction does not exist in list of reactions, None will be returned"""
+
     for reaction in reactions:
         if reaction.emoji == PIN_EMOJI:
             return reaction
     return None
 
 
-# Send DM to a user/member
 async def send_dm(user, message, embed=None):
+    """ Send DM to a user/member """
+
     if type(user) is discord.User or type(user) is discord.Member:
         if user.dm_channel is None:
             await user.create_dm()
@@ -83,40 +91,20 @@ async def send_dm(user, message, embed=None):
         await user.dm_channel.send(message, embed=embed)
 
 
-# Send help message as DM
 @bot.command(name="help")
 async def fu_help(ctx):
-    embed = discord.Embed(title="Fernuni-Bot Hilfe",
-                          description="Mit mir kannst du auf folgende Weise interagieren:",
-                          color=0x004c97)
-
-    embed.set_thumbnail(
-        url="https://cdn.discordapp.com/avatars/697842294279241749/c7d3063f39d33862e9b950f72ab71165.webp?size=1024")
-    embed.add_field(name="!help", value='Hilfe anzeigen', inline=False)
-    embed.add_field(name="!link", value='Einladungslink für diesen Discord-Server anzeigen.', inline=False)
-    embed.add_field(name="!stats", value='Nutzerstatistik anzeigen', inline=False)
-    embed.add_field(name="!all-roles", value='Alle verfügbaren Rollen anzeigen', inline=False)
-    embed.add_field(name="!my-roles", value='Dir zugewiesene Rollen anzeigen', inline=False)
-    embed.add_field(name="!add-roles ROLE1 ...", value='Eine oder mehrere Rollen hinzufügen', inline=False)
-    embed.add_field(name="!remove-roles ROLE1 ...", value='Eine oder mehrere zugewiesene Rollen entfernen',
-                    inline=False)
-    embed.add_field(name='\u200B', value='\u200B', inline=False)
-    embed.add_field(name="Benutzung Rollenbezogener Kommandos",
-                    value='Der Aufruf von `!all-roles` oder `!my-roles` gibt eine Liste der Rollen aus, die '
-                          'folgendermaßen aufgebaut ist: `[KEY] ROLLENNAME`. \u000ABeispiel: \u000A'
-                          '[BWI] B.Sc. Wirtschaftsinformatik \u000A[BM] B.Sc. Mathematik \u000A'
-                          '[BI] B.Sc. Informatik \u000A\u000ABei der Verwendung von `!add-roles` und `!remove-roles` '
-                          'kann nun eine Liste von Keys übergeben werden, um sich selbst diese Rollen hinzuzufügen, '
-                          'oder zu entfernen. Möchte man sich also nun selbst die Rollen B.Sc. Informatik und '
-                          'B.Sc. Mathematik hinzufügen, gibt man folgendes kommando ein: `!add-roles BI BM`',
-                    inline=False)
+    """ Send help message as DM """
 
+    help_file = open(HELP_FILE, mode='r')
+    help_dict = json.load(help_file)
+    embed = discord.Embed.from_dict(help_dict)
     await send_dm(ctx.author, "", embed=embed)
 
 
-# Send all available roles that can be assigned to a member by this bot as DM
 @bot.command(name="all-roles")
 async def fu_all_roles(message):
+    """ Send all available roles that can be assigned to a member by this bot as DM """
+
     roles = get_guild_roles()
     answer = "Verfügbare Rollen: \n"
     for key, role in roles.items():
@@ -125,9 +113,10 @@ async def fu_all_roles(message):
     await send_dm(message.author, answer)
 
 
-# Send the roles assigned to a member as DM.
 @bot.command(name="my-roles")
 async def fu_my_roles(message):
+    """ Send the roles assigned to a member as DM. """
+
     my_roles = get_members_roles(message.author)
     answer = "Dir zugewiesene Rollen:\n"
 
@@ -171,8 +160,9 @@ def fu_load_roles():
     assignable_roles = json.load(roles_file)
 
 
-# Add or remove roles assigned to a member. Multiple roles can be added with one command, or removed.
 async def modify_roles(ctx, add, args):
+    """ Add or remove roles assigned to a member. Multiple roles can be added with one command, or removed. """
+
     guild = get_guild()
 
     if guild is not None:
@@ -196,16 +186,18 @@ async def modify_roles(ctx, add, args):
                         await send_dm(ctx.author, f'Fehler bei der Entfernung der Rolle {role.name}')
 
 
-# Sends link to invite others to Discord server in Chat.
 @bot.command(name="link")
 async def fu_link(message):
+    """ Sends link to invite others to Discord server in Chat. """
+
     await message.channel.send('Benutze bitte folgenden Link, um andere Studierende auf unseren Discord einzuladen: '
                                'http://fernuni-discord.dnns01.de')
 
 
-# Sends stats in Chat.
 @bot.command(name="stats")
 async def fu_stats(message):
+    """ Sends stats in Chat. """
+
     guild = get_guild()
     members = await guild.fetch_members().flatten()
     roles = get_guild_roles()
@@ -227,15 +219,17 @@ async def fu_stats(message):
     await message.channel.send(answer)
 
 
-# Pin the given message, if it is not already pinned
 async def pin_message(message):
+    """ Pin the given message, if it is not already pinned """
+
     if not message.pinned:
         await message.pin()
         await message.channel.send(f'Folgende Nachricht wurde gerade angepinnt: {message.jump_url}')
 
 
-# Unpin the given message, if it is pinned, and it has no pin reaction remaining.
 async def unpin_message(message):
+    """ Unpin the given message, if it is pinned, and it has no pin reaction remaining. """
+
     if message.pinned:
         reaction = get_reaction(message.reactions)
         if reaction is None:
@@ -247,7 +241,6 @@ async def unpin_message(message):
 async def on_ready():
     print("Client started!")
     fu_load_roles()
-    print(assignable_roles)
 
 
 @bot.event
-- 
GitLab