Browse Source

Moved "here" command in appropriate file

Lucas Villeneuve 5 years ago
parent
commit
575bdba965
2 changed files with 34 additions and 20 deletions
  1. 33 0
      myanimebot/commands.py
  2. 1 20
      myanimebot/discord.py

+ 33 - 0
myanimebot/commands.py

@@ -256,3 +256,36 @@ async def help_cmd(channel):
     embed.add_field(name="`ping`", value="Ping the bot.")
     embed.add_field(name="`about`", value="Get some information about this bot")
     await channel.send(embed=embed)
+
+
+async def here_cmd(author, server, channel):
+    ''' Processes the command "here" and registers a channel to send new found feeds '''
+
+    # Verify that the user is allowed
+    if in_allowed_role(author, server) is False:
+        return await channel.send("Only allowed users can use this command!")
+    
+    if utils.is_server_in_db(server.id):
+        # Server in DB, so we need to update the channel
+
+        # Check if channel already registered
+        channels = utils.get_channels(server.id)
+        channels_id = [channel["channel"] for channel in channels]
+        globals.logger.debug("Channels {} and channel id {}".format(channels_id, channel.id))
+        if (str(channel.id) in channels_id):
+            await channel.send("Channel **{}** already in use for this server.".format(channel))
+        else:
+            cursor = globals.conn.cursor(buffered=True)
+            cursor.execute("UPDATE t_servers SET channel = {} WHERE server = {}".format(channel.id, server.id))
+            globals.conn.commit()
+            
+            await channel.send("Channel updated to: **{}**.".format(channel))
+            
+        cursor.close()
+    else:
+        # No server found in DB, so register it
+        cursor = globals.conn.cursor(buffered=True)
+        cursor.execute("INSERT INTO t_servers (server, channel) VALUES ({},{})".format(server.id, channel.id))
+        globals.conn.commit() # TODO Move to corresponding file
+        
+        await channel.send("Channel **{}** configured for **{}**.".format(channel, server))

+ 1 - 20
myanimebot/discord.py

@@ -56,26 +56,7 @@ class MyAnimeBot(discord.Client):
                     await commands.ping_cmd(message, channel)
                 
                 elif words[1] == "here":
-                    if in_allowed_role(message.author, message.guild):
-                        cursor = globals.conn.cursor(buffered=True)
-                        cursor.execute("SELECT server, channel FROM t_servers WHERE server=%s", [str(message.guild.id)])
-                        data = cursor.fetchone()
-                        
-                        if data is None:
-                            cursor.execute("INSERT INTO t_servers (server, channel) VALUES (%s,%s)", [str(message.guild.id), str(message.channel.id)])
-                            globals.conn.commit()
-                            
-                            await message.channel.send("Channel **" + str(message.channel) + "** configured for **" + str(message.guild) + "**.")
-                        else:
-                            if(data[1] == str(message.channel.id)): await message.channel.send("Channel **" + str(message.channel) + "** already in use for this server.")
-                            else:
-                                cursor.execute("UPDATE t_servers SET channel = %s WHERE server = %s", [str(message.channel.id), str(message.guild.id)])
-                                globals.conn.commit()
-                                
-                                await message.channel.send("Channel updated to: **" + str(message.channel) + "**.")
-                                
-                        cursor.close()
-                    else: await message.channel.send("Only allowed users can use this command!")
+                    await commands.here_cmd(message.author, message.guild, channel)
                     
                 elif words[1] == "add":
                     await commands.add_user_cmd(words, message)