Ver Fonte

kicks statistics

tBKwtWS há 7 anos atrás
pai
commit
28aec864e0

BIN
commands/__pycache__/admin.cpython-36.pyc


BIN
commands/__pycache__/common.cpython-36.pyc


BIN
commands/__pycache__/public.cpython-36.pyc


BIN
commands/__pycache__/statistics.cpython-36.pyc


+ 104 - 107
commands/admin.py

@@ -12,14 +12,14 @@ grey = "\x0314"
 
 def do_command(self, connection, event):
     cmdtype, trigger, command, replyto = CH.disect_command(self, event)
-    if not command: # Do nothing if there is no command.
-        return
-        
+    if not command:
+        return # Do nothing if there is no command.
+    
     # Ignore channel commands from users that do not have at least voice in homechannel or operator status in target channel.
     if event.type == "pubmsg":   # It's a channel message.
-        if not self.channels[self.homechannel].is_owner(event.source.nick) and not self.channels[self.homechannel].is_admin(event.source.nick) and not self.channels[self.homechannel].is_oper(event.source.nick) and not self.channels[event.target].is_owner(event.source.nick) and not self.channels[event.target].is_admin(event.source.nick) and not self.channels[event.target].is_oper(event.source.nick): # Does not have at least voiced status in homechannel or operator status in target channel.
+        if not userstatus.atleast_oper(self, event.source.nick, self.homechannel) and not userstatus.atleast_oper(self, event.source.nick, event.target): # Does not have at least voiced status in homechannel or operator status in target channel.
             return
-            
+    
     if command == "cmd" or command == "commands":
         if cmdtype == "cmd":
             message = grey + "Admin: "
@@ -43,59 +43,50 @@ def do_command(self, connection, event):
                 return
             connection.privmsg(replyto, message[:-2] + ".")
     
-    elif command.split()[0] == "channelfunctions":
-        if cmdtype == "help":    #Display help text. # Help code block first, as it is impossible to predict for what channel a later command is going to be issued. Rights filtering after help test.
+    elif command.split()[0] == "die":
+        if not userstatus.atleast_admin(self, event.source.nick,  self.homechannel):
+            connection.privmsg(replyto, "Denied, you need to have admin (super operator) status or higher in " + red + self.homechannel  + reset + ".")
+            return
+        if cmdtype == "help":    # Display help text.
             if len(command.split()) is not 1:
                 return
-            connection.privmsg(replyto, "Display or toggle the status channel functions. Channel, function and value optional.")
-            connection.privmsg(replyto, grey + "Usage: " + blue + "!channelfunctions " + red + italic + "channel " + reset + italic + "function value")
+            connection.privmsg(replyto, "Kill " + connection.get_nickname() + ". Optionally with reason.")
+            connection.privmsg(replyto,  grey + "Usage: " + blue + "!die " + reset + italic + "reason")
         elif cmdtype == "cmd":
-            if len(command.split()) == 1:   # No arguments.
-                if event.target == connection.get_nickname():   # Command issued via PM.
-                    connection.privmsg(replyto, "Nothing to display, Specify a channel.")
-                else:   # Command issued as channel message.
-                    message = CH.get_channelfunctions(self, event.target)
-                    connection.privmsg(replyto, message)
-            elif len(command.split()) == 2:   # One argument.
-                if command.split()[1] in self.channels: # Info requested on specific channel.
-                    message = CH.get_channelfunctions(self, command.split()[1])
-                    connection.privmsg(replyto, message)
-                else:   # First argument is not a channel the bot inhabits.
-                    connection.privmsg(replyto, command.split()[1] + " is not a channel I inhabit. For help type " + blue + self.helpchar + "channelfunctions" + reset + ".")
-            elif len(command.split()) == 3: # Two arguments.
-                if event.target == connection.get_nickname():   # Command issued via PM.
-                    connection.privmsg(replyto, "One or three arguments required. For help type " + blue + self.helpchar + "channelfunctions" + reset + ".")
-                else:   # Command issued via channel.
-                    if not CH.is_channelfunction(self, command.split()[1]):   # First argument is not a channelfunction.
-                        connection.privmsg(replyto, command.split()[1] + " is not a channel function. For help type " + blue + self.helpchar + "channelfunctions" + reset + ".")
-                        return
-                    if not command.split()[2].lower() in ["on",  "off"]:    # Second argument is not "on" or "off".
-                        connection.privmsg(replyto, "The value of this channel function can only be \"on\" or \"off\". For help type " + blue + self.helpchar + "channelfunctions" + reset + ".")
-                        return
-                    if not self.channels[self.homechannel].is_owner(event.source.nick) and not self.channels[self.homechannel].is_admin(event.source.nick) and not self.channels[self.homechannel].is_oper(event.source.nick) and not self.channels[event.target].is_owner(event.source.nick) and not self.channels[event.target].is_admin(event.source.nick) and not self.channels[event.target].is_oper(event.source.nick):   # Does not have operator status or higher in target or home channel.
-                        connection.privmsg(replyto, "Denied. You need to have at least operator status in " + red + event.target + reset + " or " + red + self.homechannel + reset + ".")
-                        return
-                    self.db.run("UPDATE channels SET " + command.split()[1] + "='" + command.split()[2].lower() + "' WHERE name='" + event.target + "' AND network='" + self.network + "'")
-            elif len(command.split()) == 4: # Three arguments.
-                if not command.split()[1] in self.channels: # Bot does not inhabit channel to be altered.
-                    connection.privmsg(replyto, command.split()[1] + " is not a channel I inhabit. For help type " + blue + self.helpchar + "channelfunctions" + reset + ".")
-                    return
-                if not CH.is_channelfunction(self, command.split()[2]):  # Function does not exist.
-                    connection.privmsg(replyto, command.split()[2] + " is not a valid channel function. For a list help type: " + blue + self.cmdchar + "channelfunctions" + red + italic + "channel")
-                    return
-                if not command.split()[3] in ["on", "off"]: # Third argument is not "on" or "off".
-                    connection.privmsg(replyto, "The value of this channel function can only be \"on\" or \"off\". For help type " + blue + self.helpchar + "channelfunctions" + reset + ".")
-                    return
-                if not self.channels[self.homechannel].is_owner(event.source.nick) and not self.channels[self.homechannel].is_admin(event.source.nick) and not self.channels[self.homechannel].is_oper(event.source.nick) and not self.channels[command.split()[1]].is_owner(event.source.nick) and not self.channels[command.split()[1]].is_admin(event.source.nick) and not self.channels[command.split()[1]].is_oper(event.source.nick):   # Does not have operator status or higher in target or home channel.
-                    connection.privmsg(replyto, "Denied. You need to have at least operator status in " + red + event.target + reset + " or " + red + self.homechannel + reset + ".")
-                    return
-                try:
-                    self.db.run("UPDATE channels SET " + command.split()[2] + "='" + command.split()[3] + "' WHERE name='" + command.split()[1] + "' AND network='" + self.network + "'")
-                except:
-                    connection.privmsg(replyto, "Error, database record not updated.")
-                    return
+            if len(command.split()) == 1:
+                self.die(msg = "Killed by " + event.source.nick)
             else:
-                connection.privmsg(replyto, "Too many arguments. For help type " + blue + self.helpchar + "channelfunctions" + reset + ".")
+                self.die(msg = "[" + event.source.nick + "] " + command.split(maxsplit=1)[1])
+    
+    elif command.split()[0] == "reconnect":
+        if not self.channels[self.homechannel].is_owner(event.source.nick) and not self.channels[self.homechannel].is_admin(event.source.nick) and not self.channels[self.homechannel].is_oper(event.source.nick): 
+            connection.privmsg(replyto, "Denied, you need to have operator status or higher in " + red + self.homechannel  + reset + ".")
+            return
+        if cmdtype == "help":    # Display help text.
+            if len(command.split()) is not 1:
+                return
+            connection.privmsg(replyto, "Reconnect " + connection.get_nickname() + ". Reason optional.")
+            connection.privmsg(replyto,  grey + "Usage: " + blue + "!reconnect " + reset + italic + "reason")
+        elif cmdtype == "cmd":
+            if len(command.split()) == 1:
+                self.disconnect(msg = "Reconnect requested by " + event.source.nick)
+            else:
+                self.disconnect(msg = "[" + event.source.nick + "] " + command.split(maxsplit=1)[1])
+    
+    elif command.split()[0] == "recovernick":
+        if not userstatus.atleast_voiced(self, event.source.nick, self.homechannel):
+            connection.privmsg(replyto, "Denied, you need to have voiced status or higher in " + red + self.homechannel  + reset + ".")
+            return
+        if cmdtype == "help":    # Display help text.
+            if len(command.split()) is not 1:
+                return
+            connection.privmsg(replyto, "Let " + connection.get_nickname() + " try to recover " + connection.nickname + " as nickname.")
+        elif cmdtype == "cmd":
+            if connection.get_nickname() == connection.nickname:
+                connection.action(replyto,  "is already named " + red + connection.nickname + reset + ".")
+                return
+            from common.networkservices import NickServ
+            NickServ.recover_nick(connection, self.password)
     
     elif command.split()[0] == "join":
         if not userstatus.atleast_oper(self, event.source.nick, self.homechannel):
@@ -128,14 +119,11 @@ def do_command(self, connection, event):
             connection.privmsg(replyto, grey + "Usage: " + blue + self.cmdchar + "join " + red + italic + "channel " + reset + italic + "password")
         elif cmdtype == "cmd":
             
-            if self.channels[self.homechannel].is_owner(event.source.nick) or self.channels[self.homechannel].is_admin(event.source.nick) or self.channels[self.homechannel].is_oper(event.source.nick):    # Is at least operator in home channel.
+            if userstatus.atleast_oper(self, event.source.nick, self.homechannel):    # Is at least operator in home channel.
                 homeadmin = True
             
-            try:
-                if self.channels[event.target].is_owner(event.source.nick) or self.channels[event.target].is_admin(event.source.nick) or self.channels[event.target].is_oper(event.source.nick):
-                    targetadmin = True
-            except:
-                pass
+            if userstatus.atleast_oper(self, event.source.nick, event.target):
+                targetadmin = True
             
             if len(command.split()) == 1:   # No arguments.
                 if event.target in self.channels:   # It's a channel message.
@@ -152,7 +140,7 @@ def do_command(self, connection, event):
                 if command.split()[1] not in self.channels: # First argument is not a channel the bot inhabits.
                     connection.action(replyto, "does not inhabit " + red + command.split()[1] + reset + ". For help type " + blue + self.helpchar + "part" + reset + ".")
                     return
-                if not homeadmin and not self.channels[command.split()[1]].is_owner(event.source.nick) and not self.channels[command.split()[1]].is_admin(event.source.nick) and not self.channels[command.split()[1]].is_oper(event.source.nick):  # Insufficient rights.
+                if not homeadmin and not userstatus.atleast_oper(self, event.source.nick, command.split()[1]):  # Insufficient rights.
                     connection.privmsg(replyto, "Denied. You need to have at least operator status in " + red + self.homechan + reset + " or " + red + command.split()[1] + reset + ".")
                     return
                 if command.split()[1] == self.homechannel:
@@ -163,51 +151,6 @@ def do_command(self, connection, event):
                 except:
                     connection.part(command.split()[1], event.source.nick)
     
-    elif command.split()[0] == "die":
-        if not self.channels[self.homechannel].is_owner(event.source.nick) and not self.channels[self.homechannel].is_admin(event.source.nick):
-            connection.privmsg(replyto, "Denied, you need to have admin (super operator) status or higher in " + red + self.homechannel  + reset + ".")
-            return
-        if cmdtype == "help":    # Display help text.
-            if len(command.split()) is not 1:
-                return
-            connection.privmsg(replyto, "Kill " + connection.get_nickname() + ". Optionally with reason.")
-            connection.privmsg(replyto,  grey + "Usage: " + blue + "!die " + reset + italic + "reason")
-        elif cmdtype == "cmd":
-            if len(command.split()) == 1:
-                self.die(msg = "Killed by " + event.source.nick)
-            else:
-                self.die(msg = "[" + event.source.nick + "] " + command.split(maxsplit=1)[1])
-    
-    elif command.split()[0] == "reconnect":
-        if not self.channels[self.homechannel].is_owner(event.source.nick) and not self.channels[self.homechannel].is_admin(event.source.nick) and not self.channels[self.homechannel].is_oper(event.source.nick): 
-            connection.privmsg(replyto, "Denied, you need to have operator status or higher in " + red + self.homechannel  + reset + ".")
-            return
-        if cmdtype == "help":    # Display help text.
-            if len(command.split()) is not 1:
-                return
-            connection.privmsg(replyto, "Reconnect " + connection.get_nickname() + ". Reason optional.")
-            connection.privmsg(replyto,  grey + "Usage: " + blue + "!reconnect " + reset + italic + "reason")
-        elif cmdtype == "cmd":
-            if len(command.split()) == 1:
-                self.disconnect(msg = "Reconnect requested by " + event.source.nick)
-            else:
-                self.disconnect(msg = "[" + event.source.nick + "] " + command.split(maxsplit=1)[1])
-    
-    elif command.split()[0] == "recovernick":
-        if not self.channels[self.homechannel].is_owner(event.source.nick) and not self.channels[self.homechannel].is_admin(event.source.nick) and not self.channels[self.homechannel].is_oper(event.source.nick) and not self.channels[self.homechannel].is_halfop(event.source.nick) and not self.channels[self.homechannel].is_voiced(event.source.nick):
-            connection.privmsg(replyto, "Denied, you need to have voiced status or higher in " + red + self.homechannel  + reset + ".")
-            return
-        if cmdtype == "help":    # Display help text.
-            if len(command.split()) is not 1:
-                return
-            connection.privmsg(replyto, "Let " + connection.get_nickname() + " try to recover " + connection.nickname + " as nickname.")
-        elif cmdtype == "cmd":
-            if connection.get_nickname() == connection.nickname:
-                connection.action(replyto,  "is already named " + red + connection.nickname + reset + ".")
-                return
-            from common.networkservices import NickServ
-            NickServ.recover_nick(connection, self.password)
-    
     elif command.split()[0] == "msg" or  command.split(maxsplit=1)[0] == "act":
         if cmdtype == "help":    #Display help text.
             if len(command.split()) is not 1:
@@ -249,7 +192,7 @@ def do_command(self, connection, event):
                 connection.action(replyto, "does not inhabit " + red + destination + reset + ".")
             
             # Send message if user has at least operator status the home channel or the channel the message is intended for.
-            elif self.channels[self.homechannel].is_owner(event.source.nick) or self.channels[self.homechannel].is_admin(event.source.nick) or self.channels[self.homechannel].is_oper(event.source.nick) or self.channels[destination].is_owner(event.source.nick) or self.channels[destination].is_admin(event.source.nick) or self.channels[destination].is_oper(event.source.nick):
+            elif userstatus.atleast_oper(self, event.source.nick, self.homechannel) or userstatus.atleast_oper(self, event.source.nick, destination):
                 if command.split(maxsplit=1)[0] == "act":
                     connection.action(destination, message)
                 else:
@@ -258,3 +201,57 @@ def do_command(self, connection, event):
             # Reply error if user is not operator of destination channel.
             else:
                 connection.privmsg(replyto, "Denied, you need to be an operator of " + red + destination + reset +".")
+    
+    elif command.split()[0] == "channelfunctions":
+        if cmdtype == "help":    #Display help text. # Help code block first, as it is impossible to predict for what channel a later command is going to be issued. Rights filtering after help test.
+            if len(command.split()) is not 1:
+                return
+            connection.privmsg(replyto, "Display or toggle the status channel functions. Channel, function and value optional.")
+            connection.privmsg(replyto, grey + "Usage: " + blue + "!channelfunctions " + red + italic + "channel " + reset + italic + "function value")
+        elif cmdtype == "cmd":
+            if len(command.split()) == 1:   # No arguments.
+                if event.target == connection.get_nickname():   # Command issued via PM.
+                    connection.privmsg(replyto, "Nothing to display, Specify a channel.")
+                else:   # Command issued as channel message.
+                    message = CH.get_channelfunctions(self, event.target)
+                    connection.privmsg(replyto, message)
+            elif len(command.split()) == 2:   # One argument.
+                if command.split()[1] in self.channels: # Info requested on specific channel.
+                    message = CH.get_channelfunctions(self, command.split()[1])
+                    connection.privmsg(replyto, message)
+                else:   # First argument is not a channel the bot inhabits.
+                    connection.privmsg(replyto, command.split()[1] + " is not a channel I inhabit. For help type " + blue + self.helpchar + "channelfunctions" + reset + ".")
+            elif len(command.split()) == 3: # Two arguments.
+                if event.target == connection.get_nickname():   # Command issued via PM.
+                    connection.privmsg(replyto, "One or three arguments required. For help type " + blue + self.helpchar + "channelfunctions" + reset + ".")
+                else:   # Command issued via channel.
+                    if not CH.is_channelfunction(self, command.split()[1]):   # First argument is not a channelfunction.
+                        connection.privmsg(replyto, command.split()[1] + " is not a channel function. For help type " + blue + self.helpchar + "channelfunctions" + reset + ".")
+                        return
+                    if not command.split()[2].lower() in ["on",  "off"] and not command.split()[1].lower() == "aggressiveness":    # Second argument unsupported.
+                        connection.privmsg(replyto, "The value of this channel function can only be \"on\" or \"off\". For help type " + blue + self.helpchar + "channelfunctions" + reset + ".")
+                        return
+                    if not userstatus.atleast_oper(self, event.source.nick, self.homechannel) and not userstatus.atleast_oper(self, event.source.nick, event.target):   # Does not have operator status or higher in target or home channel.
+                        connection.privmsg(replyto, "Denied. You need to have at least operator status in " + red + event.target + reset + " or " + red + self.homechannel + reset + ".")
+                        return
+                    self.db.run("UPDATE channels SET " + command.split()[1].lower() + "='" + command.split()[2].lower() + "' WHERE name='" + event.target + "' AND network='" + self.network + "'")
+            elif len(command.split()) == 4: # Three arguments.
+                if not command.split()[1] in self.channels: # Bot does not inhabit channel to be altered.
+                    connection.privmsg(replyto, command.split()[1] + " is not a channel I inhabit. For help type " + blue + self.helpchar + "channelfunctions" + reset + ".")
+                    return
+                if not CH.is_channelfunction(self, command.split()[2]):  # Function does not exist.
+                    connection.privmsg(replyto, command.split()[2] + " is not a valid channel function. For a list help type: " + blue + self.cmdchar + "channelfunctions" + red + italic + "channel")
+                    return
+                if not command.split()[3].lower() in ["on", "off"] and not command.split()[2].lower() == "aggressiveness": # Third argument unsupported.
+                    connection.privmsg(replyto, "The value of this channel function can only be \"on\" or \"off\". For help type " + blue + self.helpchar + "channelfunctions" + reset + ".")
+                    return
+                if not userstatus.atleast_oper(self, event.source.nick, self.homechannel) and not userstatus.atleast_oper(self, event.source.nick, command.split()[1]):   # Does not have operator status or higher in target or home channel.
+                    connection.privmsg(replyto, "Denied. You need to have at least operator status in " + red + event.target + reset + " or " + red + self.homechannel + reset + ".")
+                    return
+                try:
+                    self.db.run("UPDATE channels SET " + command.split()[2].lower() + "='" + command.split()[3].lower() + "' WHERE LOWER(name)=LOWER('" + command.split()[1] + "') AND network='" + self.network + "'")
+                except:
+                    connection.privmsg(replyto, "Error, database record not updated.")
+                    return
+            else:
+                connection.privmsg(replyto, "Too many arguments. For help type " + blue + self.helpchar + "channelfunctions" + reset + ".")

+ 7 - 8
commands/common.py

@@ -59,27 +59,27 @@ class CommandHelpers():
             return blue + self.cmdchar + command + reset + ", "
     
     def get_channelfunctions(self, channel):
-        channelfunctions = self.db.one("SELECT autojoin, join_greeting, statistics_commands, games FROM channels WHERE name='" + channel + "' AND network='" + self.network + "'")
+        channelfunctions = self.db.one("SELECT autojoin, aggressiveness, join_greeting, statistics_commands, games FROM channels WHERE LOWER(name)=LOWER('" + channel + "') AND network='" + self.network + "'")
         if channelfunctions[0]:
             autojoin = "on"
         else:
             autojoin = "off"
-        if channelfunctions[1]:
+        if channelfunctions[2]:
             joingreet = "on"
         else:
             joingreet = "off"
-        if channelfunctions[2]:
+        if channelfunctions[3]:
             statscmds = "on"
         else:
             statscmds = "off"
-        if channelfunctions[3]:
+        if channelfunctions[4]:
             games = "on"
         else:
-            games = "off"
-        return ("autojoin " + autojoin + ", join_greeting " + joingreet + ", statistics_commands " + statscmds + ", games " + games + ".")
+            games = "off"""
+        return ("autojoin " + green + autojoin + reset + ", aggressiveness " + green + channelfunctions[1] + reset +  ", join_greeting " + green + joingreet + reset + ", statistics_commands " + green + statscmds + reset + ", games " + green + games + reset + ".")
         
     def is_channelfunction(self, value):
-        if value not in ["autojoin", "join_greeting",  "statistics_commands",  "games"]:
+        if value not in ["autojoin", "join_greeting",  "statistics_commands",  "games", "aggressiveness"]:
             return False
         else:
             return True
@@ -90,4 +90,3 @@ class GameHelpers():
         for iterations in range(amount):
             rolls.append(random.randint(1,  type))
         return rolls
-    

+ 6 - 65
commands/public.py

@@ -31,7 +31,7 @@ def do_command(self, connection, event):
                 return
             connection.privmsg(replyto, "Displays a list of commands.")
         elif cmdtype == "cmd":
-            connection.privmsg(replyto, grey + "Commands: " + CH.ccc(self, "cmd") + CH.ccc(self, "help") + CH.ccc(self, "joins") + CH.ccc(self, "stopgreet")[:-2] + ".")
+            connection.privmsg(replyto, grey + "Commands: " + CH.ccc(self, "cmd") + CH.ccc(self, "help") + CH.ccc(self, "stopgreet")[:-2] + ".")
     
     elif command == "help":
         if cmdtype == "help":    #Display help text.
@@ -43,64 +43,6 @@ def do_command(self, connection, event):
             connection.privmsg(replyto, grey + "Example: " + reset + blue + self.helpchar + "help")
     
     
-    elif command.split()[0] == "joins":
-        if cmdtype == "help":    #Display help text.
-            if len(command.split()) is not 1:
-                return
-            connection.privmsg(replyto, "Display amount of joins of user and or channel. Channel and user optional.")
-            connection.privmsg(replyto, grey + "Usage: " + blue + "!joins " + reset + italic + "channel user")
-        elif cmdtype == "cmd":
-            
-            # Parse user input
-            if len(command.split()) == 1:    # Command contains only !joins.
-                user = event.source.nick
-                if connection.get_nickname() is not event.target:   # Channel message.
-                    channel = event.target
-            elif len(command.split()) == 2:  # Command has one argument.
-                if command.split()[1] in self.channels:
-                    channel = command.split()[1]
-                else:
-                    user = command.split()[1]
-            elif len(command.split()) == 3: # Command has two arguments.
-                if not command.split()[1] in self.channels: # Bot does not inhabit requested channel.
-                    if not command.split()[2] in self.channels: # User did not revert channel and user in command syntax.
-                        connection.action(replyto, "does not inhabit " + red + command.split()[1] + reset + ".")
-                        return
-                    else:   # User reverted user and channel in command syntax.
-                        user = command.split()[1]
-                        channel = command.split()[2]
-                else: # Bot does inhabit requested channel.
-                    user = command.split()[2]
-                    channel = command.split()[1]
-            elif len(command.split()) < 5:  # To many arguments
-                connection.privmsg(replyto, "To many arguments. For help type " + blue + self.helpchar + "joins" + reset + ".")
-                return
-            
-            try:
-                if user and channel:
-                    userjoins = str(self.db.all("SELECT joins FROM joins WHERE channel_network='" + self.network + "' AND \"user\"='" + user + "' AND user_network='" + self.network + "'"))
-                    userchanneljoins = str(self.db.one("SELECT joins FROM joins WHERE channel='" + channel + "' AND channel_network='" + self.network + "' AND \"user\"='" + user + "' AND user_network='" + self.network + "'"))
-                    channeljoins = str(sum(self.db.all("SELECT joins FROM joins WHERE channel='" + channel + "' AND channel_network='" + self.network + "' AND user_network='" + self.network + "'")))
-                    connection.privmsg(replyto, red + user + reset + " has " + green + str(sum(list(map(int, userjoins[1:-1].split(", "))))) + reset + " joins. Of which " + green + str(userchanneljoins) + reset + " have been in " + red + channel + reset + ", that has " + green + str(channeljoins) + reset + " joins" + reset + " in total.")
-                    return
-            except:
-                pass
-            try:
-                if user:
-                    userjoins = str(self.db.all("SELECT joins FROM joins WHERE channel_network='" + self.network + "' AND \"user\"='" + user + "' AND user_network='" + self.network + "' AND user_network='" + self.network + "'"))
-                    connection.privmsg(replyto,  red + user + reset + " has " + green + str(sum(list(map(int, userjoins[1:-1].split(", "))))) + reset + " joins in channels I monitor.")
-                    return
-            except:
-                pass
-            try:
-                if channel:
-                    channeljoins = str(sum(self.db.all("SELECT joins FROM joins WHERE channel='" + channel + "' AND channel_network='" + self.network + "' AND user_network='" + self.network + "'")))
-                    connection.privmsg(replyto,  red + channel + reset + " has been joined " + green + channeljoins + reset + " times.")
-                    return
-            except:
-                    pass
-            connection.privmsg(replyto, "tBKwtWS Was wondering if this programming was sloppy. When you see this message, it was.")
-    
     elif command.split()[0] == "stopgreet":
         if cmdtype == "help":    #Display help text.
             if len(command.split()) is not 1:
@@ -154,7 +96,7 @@ def do_command(self, connection, event):
                 return
             
             # Check for database record.
-            result = self.db.one("SELECT id, stopgreet FROM joins WHERE \"user\"='" + user + "' AND user_network='" + self.network + "' AND channel='" + channel + "' AND channel_network='" + self.network + "'")
+            result = self.db.one("SELECT id, stopgreet FROM joins WHERE LOWER(\"user\")=LOWER('" + user + "') AND user_network='" + self.network + "' AND LOWER(channel)=LOWER('" + channel + "') AND channel_network='" + self.network + "'")
             if not result:  # No record in database.
                 connection.action(replyto, "has not yet had the pleasure of greeting " + red + user + reset + " in " + red + channel + reset + ".")
                 return
@@ -169,8 +111,7 @@ def do_command(self, connection, event):
                 connection.action(replyto, message + red + user + reset + " in " + red + channel + reset + ".")
                 return
             print(str(stopgreet) + str(user) + str(channel))
-            #try:
-            print("UPDATE joins SET stopgreet='" + str(stopgreet) + "' WHERE \"user\"='" + user + "' AND user_network='" + self.network + "' AND channel='" + channel + "' AND channel_network='" + self.network + "'")
-            self.db.run("UPDATE joins SET stopgreet='" + str(stopgreet) + "' WHERE \"user\"='" + user + "' AND user_network='" + self.network + "' AND channel='" + channel + "' AND channel_network='" + self.network + "'")
-            #except:
-                #connection.privmsg(replyto, "Failed to update database.")
+            try:
+                self.db.run("UPDATE joins SET stopgreet='" + str(stopgreet) + "' WHERE LOWER(\"user\")=LOWER('" + user + "') AND user_network='" + self.network + "' AND lower(channel)=LOWER('" + channel + "') AND channel_network='" + self.network + "'")
+            except:
+                connection.privmsg(replyto, "Failed to update database.")

+ 111 - 0
commands/statistics.py

@@ -0,0 +1,111 @@
+from common import log
+from commands.common import CommandHelpers as CH
+bold = "\x02"
+italic = "\x1D"
+underline = "\x1F"
+reverse = "\x16" 	# swap background and foreground colors ("reverse video")
+reset = "\x0F"
+blue = "\x0302"
+green = "\x0303"
+red = "\x0304"
+grey = "\x0314"
+
+def do_command(self, connection, event):
+    cmdtype, trigger, command, replyto = CH.disect_command(self, event)
+    if not command: # Do nothing if there is no command.
+        return
+
+    if command == "cmd" or command == "commands":
+        if cmdtype == "help":    #Display help text.
+            if len(command.split()) is not 1:
+                return
+            connection.privmsg(replyto, "Displays a list of commands.")
+        elif cmdtype == "cmd":
+            connection.privmsg(replyto, grey + "Statistics commands: " + CH.ccc(self, "joins") + CH.ccc(self, "kicks")[:-2] + ".")
+    
+    
+    elif command.split()[0] == "joins" or command.split()[0] == "kicks":
+        if cmdtype == "help":    #Display help text.
+            if len(command.split()) is not 1:
+                return
+            connection.privmsg(replyto, "Display amount of " + command.split()[0] + " of user and or channel. Channel and user optional.")
+            connection.privmsg(replyto, grey + "Usage: " + blue + self.cmdchar + command.split()[0] + " " + reset + italic + "channel user")
+        elif cmdtype == "cmd":
+            
+            # Parse user input
+            user = event.source.nick
+            if not connection.get_nickname() == event.target:   # Channel message.
+                channel = event.target
+            if len(command.split()) == 1:    # Command contains only !joins.
+                user = event.source.nick
+            elif len(command.split()) == 2:  # Command has one argument.
+                if command.split()[1] in self.channels:
+                    channel = command.split()[1]
+                else:
+                    user = command.split()[1]
+            elif len(command.split()) == 3: # Command has two arguments.
+                if not command.split()[1] in self.channels: # Bot does not inhabit requested channel.
+                    if not command.split()[2] in self.channels: # User did not revert channel and user in command syntax.
+                        connection.action(replyto, "does not inhabit " + red + command.split()[1] + reset + ".")
+                        return
+                    else:   # User reverted user and channel in command syntax.
+                        user = command.split()[1]
+                        channel = command.split()[2]
+                else: # Bot does inhabit requested channel.
+                    user = command.split()[2]
+                    channel = command.split()[1]
+            elif len(command.split()) < 5:  # To many arguments
+                connection.privmsg(replyto, "To many arguments. For help type " + blue + self.helpchar + "joins" + reset + ".")
+                return
+            
+            try:
+                if user and channel:
+                    if command.split()[0] == "joins":
+                        userstat = str(sum(self.db.all("SELECT " + command.split()[0].lower() + " FROM " + command.split()[0].lower() + " WHERE channel_network='" + self.network + "' AND LOWER(\"user\")=LOWER('" + user + "') AND user_network='" + self.network + "'")))
+                        userchannelstat = str(self.db.one("SELECT " + command.split()[0].lower() + " FROM " + command.split()[0].lower() + " WHERE LOWER(channel)=LOWER('" + channel + "') AND channel_network='" + self.network + "' AND LOWER(\"user\")=LOWER('" + user + "') AND user_network='" + self.network + "'"))
+                        channelstat = str(sum(self.db.all("SELECT " + command.split()[0].lower() + " FROM " + command.split()[0].lower() + " WHERE LOWER(channel)=LOWER('" + channel + "') AND channel_network='" + self.network + "' AND user_network='" + self.network + "'")))
+                        if userchannelstat == "None":
+                            userchannelstat = "0"
+                        connection.privmsg(replyto, red + user + reset + " has " + green + userstat + reset + " " + command.split()[0] + ". Of which " + green + userchannelstat + reset + " have been in " + red + channel + reset + ", that has " + green + channelstat + reset + " " + command.split()[0] + reset + " in total.")
+                    elif command.split()[0] == "kicks":
+                        try:
+                            givenkicks, receivedkicks = self.db.one("SELECT given, received FROM " + command.split()[0].lower() + " WHERE LOWER(channel)=LOWER('" + channel + "') AND channel_network='" + self.network + "' AND LOWER(\"user\")=LOWER('" + user + "') AND user_network='" + self.network + "'")
+                        except:
+                            givenkicks = 0
+                            receivedkicks = 0
+                        channelkicks = self.db.all("SELECT given FROM " + command.split()[0].lower() + " WHERE LOWER(channel)=LOWER('" + channel + "') AND channel_network='" + self.network + "'")
+                        connection.privmsg(replyto, red + user + reset + " has kicked " + green + str(givenkicks) + reset + " and been kicked " + green + str(receivedkicks) + reset + " times in " + red + channel + reset + ", where were " + green + str(sum(channelkicks)) + reset + " kicks in total.")
+                    return
+            except:
+                pass
+            try:
+                if user:
+                    if command.split()[0] == "joins":
+                        userstat = str(sum(self.db.all("SELECT " + command.split()[0] + " FROM " + command.split()[0] + " WHERE channel_network='" + self.network + "' AND LOWER(\"user\")=LOWER('" + user + "') AND user_network='" + self.network + "'")))
+                        if userstat == "[]":
+                            userstat = "[0]"
+                        connection.privmsg(replyto,  red + user + reset + " has " + green + userstat + reset + " " + command.split()[0] + " in channels I monitor.")
+                    elif command.split()[0] == "kicks":
+                        userstat = self.db.all("SELECT given, received FROM kicks WHERE channel_network='" + self.network + "' AND LOWER(\"user\")=LOWER('" + user + "') AND user_network='" + self.network + "'")
+                        kicksgiven = 0
+                        kicksreceived = 0
+                        for record in userstat:
+                            kicksgiven += record[0]
+                            kicksreceived += record[1]
+                            print(record)
+                        print(userstat)
+                        connection.privmsg(replyto, red + user + reset + " has given " + green + str(kicksgiven) + reset + " and received " + green + str(kicksreceived) + reset + " kicks")
+                return
+            except:
+                pass
+            try:
+                if channel: # This situation should not occur anymore.
+                    channelstat = str(sum(self.db.all("SELECT " + command.split()[0] + " FROM " + command.split()[0] + " WHERE LOWER(channel)=LOWER('" + channel + "') AND channel_network='" + self.network + "' AND user_network='" + self.network + "'")))
+                    if command.split()[0] == "joins":
+                        connection.privmsg(replyto, red + channel + reset + " has been " + command.split()[0][:-1] + "ed " + green + channelstat + reset + " times.")
+                    elif command.split()[0] == "kicks":
+                        connection.privmsg(replyto, "There have been " + green + channelstat + reset + " kicks in " + red + command.split()[0] + reset + ".")
+                    return
+            except:
+                pass
+            log.error("No situation applied on statistics.py")

BIN
common/__pycache__/log.cpython-36.pyc


BIN
common/__pycache__/userstatus.cpython-36.pyc


+ 8 - 0
common/log.py

@@ -0,0 +1,8 @@
+def info(message):
+    print("INFO: " + str(message))
+
+def notice(message):
+    print("NOTICE: " + str(message))
+
+def error(message):
+    print("ERROR: " + str(message))

+ 9 - 1
common/userstatus.py

@@ -6,10 +6,18 @@ def atleast_oper(self, user, channel):
     else:
         return False
 
-def  atleast_halfop(self, user, channel):
+def atleast_halfop(self, user, channel):
     if channel not in self.channels:
         return False
     if self.channels[channel].is_owner(user) or self.channels[channel].is_admin(user) or self.channels[channel].is_oper(user) or self.channels[channel].is_halfop(user):
         return True
     else:
         return False
+
+def atleast_voiced(self, user, channel):
+    if channel not in self.channels:
+        return False
+    if self.channels[channel].is_owner(user) or self.channels[channel].is_admin(user) or self.channels[channel].is_oper(user) or self.channels[channel].is_halfop(user) or self.channels[channel].is_voiced(user):
+        return True
+    else:
+        return False

BIN
events/__pycache__/on_join.cpython-36.pyc


BIN
events/__pycache__/on_kick.cpython-36.pyc


BIN
events/__pycache__/on_pubmsg.cpython-36.pyc


+ 1 - 3
events/on_join.py

@@ -24,13 +24,11 @@ def process_event(self, connection, event):
     # Stop if greeting is not wanted.
     joingreeting = self.db.one("SELECT join_greeting FROM channels WHERE name='" + event.target + "' AND network='" + self.network + "'")
     stopgreet = self.db.one("SELECT stopgreet FROM joins WHERE channel='" + event.target + "' AND channel_network='" + self.network + "' AND \"user\"='" + event.source.nick + "' AND user_network='" + self.network + "'")
-    print(joingreeting)
-    print(stopgreet)
     if not joingreeting or stopgreet:
         return
     
     # Show greeting.
-    joins = self.db.one("SELECT joins FROM joins WHERE channel='" + event.target + "' AND channel_network='" + self.network + "' AND \"user\"='" + event.source.nick + "' AND user_network='" + self.network + "'")
+    joins = str(self.db.one("SELECT joins FROM joins WHERE channel='" + event.target + "' AND channel_network='" + self.network + "' AND \"user\"='" + event.source.nick + "' AND user_network='" + self.network + "'"))
     if not self.db.one("SELECT join_greeting FROM channels WHERE name='" + event.target + "' AND network ='" + self.network + "'"): # Do not greet users joining the channel.
         return
     if joins == 1:

+ 29 - 0
events/on_kick.py

@@ -0,0 +1,29 @@
+def process_event(self, connection, event):
+    kicker = event.source.nick
+    channel = event.target
+    kicked = event.arguments[0]
+    #reason = event.arguments[1]
+    
+    # Create user records if they don't exist.
+    if not self.db.one("SELECT id FROM users WHERE name='" + kicker + "' AND network='" + self.network + "'"):   # Kicker does not have a user record.
+        self.db.run("INSERT INTO \"users\" (name, network) VALUES ('" + kicker + "', '" + self.network + "')")  # Create user record.
+    if not self.db.one("SELECT id FROM users WHERE name='" + kicked + "' AND network='" + self.network + "'"):  # Kicked does not have a user record.
+        self.db.run("INSERT INTO \"users\" (name, network) VALUES ('" + kicked + "', '" + self.network + "')")  # Create user record.
+    
+    # Create kick records if they don't exist.
+    if not self.db.one("SELECT id FROM kicks WHERE channel='" + channel + "'AND channel_network='" + self.network + "' AND \"user\"='" + kicker + "' AND user_network='" + self.network + "'"): # No records for kicker channel combination.
+        self.db.run("INSERT INTO kicks (channel, channel_network, \"user\", user_network) VALUES ('" + channel + "', '" + self.network + "', '" + kicker + "', '" + self.network + "')")
+    if not self.db.one("SELECT id FROM kicks WHERE channel='" + channel + "'AND channel_network='" + self.network + "' AND \"user\"='" + kicked + "' AND user_network='" + self.network + "'"): # No records for kicked channel combination.
+        self.db.run("INSERT INTO kicks (channel, channel_network, \"user\", user_network) VALUES ('" + channel + "', '" + self.network + "', '" + kicked + "', '" + self.network + "')")
+    
+    # Save statistic.
+    self.db.run("UPDATE kicks SET given = given + 1 WHERE channel='" + channel + "'AND channel_network='" + self.network + "' AND \"user\"='" + kicker + "' AND user_network='" + self.network + "'")
+    self.db.run("UPDATE kicks SET received = received + 1 WHERE channel='" + channel + "'AND channel_network='" + self.network + "' AND \"user\"='" + kicked + "' AND user_network='" + self.network + "'")
+    
+    # React.
+    if kicked == connection.get_nickname(): # Bot was kicked.
+        if channel == self.homechannel: # From it's homechannel.
+            connection.join(self.homechannel)
+            return
+        #if self.db.one("SELECT aggressiveness FROM channels WHERE name='" + channel + "' AND network='" + self.network + "'") == "passive"):    # Passiev behaviour.
+            #return  # Do nothing.

+ 55 - 0
events/on_pubmsg.py

@@ -0,0 +1,55 @@
+import random
+
+def process_event(self, connection, event):
+    
+    if connection.get_nickname().lower() in event.arguments[0].lower() and event.source.nick is not connection.get_nickname(): # Bot's name was mentioned
+        
+        messages = [
+            "Hello " + event.source.nick + ".", 
+            "How are you today " + event.source.nick +  "?", 
+            "Piss off " + event.source.nick + "!", 
+            event.source.nick + ", what are you botherring me for?", 
+            "Go bother someone else...", 
+            "Is life treating you fair?", 
+            "What's up?", 
+            "Why are you talking to me?", 
+            "I'm not talking to you!", 
+            "What have you been up to?", 
+            "How is life?", 
+            "What do you wan't from me?", 
+            event.source.nick + ", why are you bothering me?", 
+            event.source.nick + ", when will you stop talking about me?", 
+            event.source.nick + " I hate you!", 
+            "Get bent!", 
+            "Go and cut yourself.", 
+            "Do you think i care about you?", 
+            "Stop nickalerting me " + event.source.nick + ", you wanker!", 
+        ]
+        actions = [
+            "hides!",
+            "dies.", 
+            "runs away.", 
+            "is ignoring that.", 
+            "is not feeling like caring.", 
+            "is away", 
+            "will be ignoring that.", 
+            "is faggaliciouz!! <333", 
+            "likes you! <3", 
+            "looks the other way...", 
+            "does a little dance with " + event.source.nick + ".", 
+            "makes a little love.", 
+            "get's down tonight.", 
+            "thinks SAM Broadcaster sucks raw cocks in hell!", 
+            "is secretly in love with " + event.source.nick + ".", 
+            "tosses " + event.source.nick + "'s salad.", 
+            "tortures " + event.source.nick + " horribly!", 
+            "is smelling like tuna when looking at " + event.source.nick + ".", 
+            "sniffing armpits.. Eew! Smells like " + event.source.nick + ".", 
+            "rapes " + event.source.nick + ".", 
+        ]
+        
+        # REply with a random message or action.
+        if random.randint(0, 1) == 0:
+            connection.privmsg(event.target, random.choice(messages))
+        else:
+            connection.action(event.target, random.choice(actions))

+ 59 - 79
rotbot.py

@@ -1,14 +1,13 @@
 #! /usr/bin/env python
-#
-# Version 0.0a2 by tBKwtWS.
 
 import sys, random, string, ssl
-import irc.bot, irc.strings
+import irc.bot#, irc.strings
 from irc.client import ip_numstr_to_quad#, ip_quad_to_numstr
 from postgres import Postgres
-import commands.public, commands.admin, commands.games
-import events.on_join
+import commands.public, commands.admin, commands.games, commands.statistics
+import events.on_join, events.on_kick, events.on_pubmsg
 from common.networkservices import NickServ
+from common import log
 bold = "\x02"
 italic = "\x1D"
 underline = "\x1F"
@@ -32,8 +31,7 @@ class PyRot(irc.bot.SingleServerIRCBot):
             factory = irc.connection.Factory(wrapper=ssl.wrap_socket)
         else:
             factory = irc.connection.Factory()
-        
-        print("Connecting to " + host + ":" + str(port) + "/" + self.homechannel)
+        log.info("Connecting to " + host + ":" + str(port) + "/" + self.homechannel)
         try:
             irc.bot.SingleServerIRCBot.__init__(self, [(host, port)], nickname, username, connect_factory=factory)
         except irc.client.ServerConnectionError:
@@ -41,17 +39,12 @@ class PyRot(irc.bot.SingleServerIRCBot):
     
     # Events.
     def on_nicknameinuse(self, connection, event):
-        print("Nickname in use, attempting to recover: " + connection.nickname)
+        log.info("Nickname in use, attempting to recover: " + connection.nickname)
         connection.nick(connection.nickname + ''.join(random.choice(string.digits) for _ in range(3)))    # Take temporary nick. Without this recovering via NickServ won't work.
         NickServ.recover_nick(connection, self.password)
     
-    def on_nick(self,  connection,  event):
-        if event.source.nick == connection.nickname:    # If the nick boing changes is the bots prefered nickname.
-            print("Assuming original nick.")
-            NickServ.recover_nick(connection, self.password)
-    
     def on_welcome(self, connection, event):
-        print(event)    # Handy for debugging. Keep this.
+        log.info(event)    # Handy for debugging. Keep this.
         if self.password:   # Id with NickServ
             connection.privmsg("NickServ", "identify " + connection.nickname + " " + self.password) # Identify with NickServ.
         channels = self.db.all("SELECT name FROM channels WHERE network='" + self.network + "' AND autojoin=True")
@@ -59,88 +52,74 @@ class PyRot(irc.bot.SingleServerIRCBot):
         for channel in channels:    # Join channels with autojoin function.
             connection.join(channel)
         connection.join(self.homechannel)
+        
+    def on_error(self, connection, event):
+        log.notice(event)
+        connection.privmsg(self.homechannel, "ERROR: " + event)
     
+    def on_nick(self,  connection,  event):
+        if event.source.nick == connection.nickname:    # If the nick boing changes is the bots prefered nickname.
+            log.info("Assuming original nick.")
+            NickServ.recover_nick(connection, self.password)
     
     def on_join(self, connection, event):
         print(event)
         events.on_join.process_event(self, connection, event)
     
+    def on_kick(self, connection, event):
+        print(event)
+        events.on_kick.process_event(self, connection, event)
     
-    def on_privmsg(self, connection, event):
-        print(str(event))
-        commands.public.do_command(self, connection, event)
-        commands.admin.do_command(self, connection, event)
-        commands.games.do_command(self, connection, event)
+    def on_mode(self, connection, event):
+        print(event)
     
-    def on_privnotice(self,  connection,  event):
-        print(str(event))
-        commands.public.do_command(self, connection, event)
-        commands.admin.do_command(self, connection, event)
-        commands.games.do_command(self, connection, event)
-        if event.source.nick == NickServ and event.arguments[0].startswith("This nickname is registered"):
-            connection.privmsg("NickServ", "identify " + connection.nickname + " " + connection.password) # Identify with NickServ.
+    def on_part(self, connection, event):
+        print(event)
+    
+    def on_quit(self, connection, event):
+        print(event)
+    
+    def on_invite(self, connection, event):
+        print(event)
+    
+    def on_topic(self, connection, event):
+        print(event)
 
     def on_pubmsg(self, connection,  event):
         commands.public.do_command(self, connection, event)
         commands.admin.do_command(self, connection, event)
+        commands.statistics.do_command(self, connection, event)
         try:
             games = self.db.one("SELECT games FROM channels WHERE name='" + event.target + "' AND network='" + self.network + "'")
         except:
             pass
         if games:
             commands.games.do_command(self, connection, event)
-        
-        if connection.get_nickname().lower() in event.arguments[0].lower() and event.source.nick is not connection.get_nickname(): # Bot's name was mentioned
-            
-            messages = [
-                "Hello " + event.source.nick + ".", 
-                "How are you today " + event.source.nick +  "?", 
-                "Piss off " + event.source.nick + "!", 
-                event.source.nick + ", what are you botherring me for?", 
-                "Go bother someone else...", 
-                "Is life treating you fair?", 
-                "What's up?", 
-                "Why are you talking to me?", 
-                "I'm not talking to you!", 
-                "What have you been up to?", 
-                "How is life?", 
-                "What do you wan't from me?", 
-                event.source.nick + ", why are you bothering me?", 
-                event.source.nick + ", when will you stop talking about me?", 
-                event.source.nick + " I hate you!", 
-                "Get bent!", 
-                "Go and cut yourself.", 
-                "Do you think i care about you?", 
-                "Stop nickalerting me " + event.source.nick + ", you wanker!", 
-            ]
-            actions = [
-                "hides!",
-                "dies.", 
-                "runs away.", 
-                "is ignoring that.", 
-                "is not feeling like caring.", 
-                "is away", 
-                "will be ignoring that.", 
-                "is faggaliciouz!! <333", 
-                "likes you! <3", 
-                "looks the other way...", 
-                "does a little dance with " + event.source.nick + ".", 
-                "makes a little love.", 
-                "get's down tonight.", 
-                "thinks SAM Broadcaster sucks raw cocks in hell!", 
-                "is secretly in love with " + event.source.nick + ".", 
-                "tosses " + event.source.nick + "'s salad.", 
-                "tortures " + event.source.nick + " horribly!", 
-                "is smelling like tuna when looking at " + event.source.nick + ".", 
-                "sniffing armpits.. Eew! Smells like " + event.source.nick + ".", 
-                "rapes " + event.source.nick + ".", 
-            ]
-            if random.randint(0, 1) == 0:
-                connection.privmsg(event.target, random.choice(messages))
-            else:
-                connection.action(event.target, random.choice(actions))
+        events.on_pubmsg.process_event(self, connection, event)
+    
+    def on_pubnotice(self, connection, event):
+        print(event)
+    
+    def on_privmsg(self, connection, event):
+        log.info(event)
+        commands.public.do_command(self, connection, event)
+        commands.admin.do_command(self, connection, event)
+        commands.statistics.do_command(self, connection, event)
+        commands.games.do_command(self, connection, event)
     
+    def on_privnotice(self,  connection,  event):
+        log.info(event)
+        commands.public.do_command(self, connection, event)
+        commands.admin.do_command(self, connection, event)
+        commands.statistics.do_command(self, connection, event)
+        commands.games.do_command(self, connection, event)
+        if event.source.nick == NickServ and event.arguments[0].startswith("This nickname is registered"):
+            connection.privmsg("NickServ", "identify " + connection.nickname + " " + connection.password) # Identify with NickServ.
     
+    def on_action(self, connection, event):
+        print(event)
+    
+    # DCC stuff from originalexample file.
     def on_dccmsg(self, c, e):
         # non-chat DCC messages are raw bytes; decode as text
         text = e.arguments[0].decode('utf-8')
@@ -165,10 +144,11 @@ def main():
         print(sys.argv)
         print("Usage: rotbot <server ID from database>")
         sys.exit(1)
+    instance = sys.argv[1]  # Instance is the database network id.
+    
+    db = Postgres("postgres://pyRot:4h8q(.@localhost/pyRot")    # Database.
     
-    # Check instance. Instance is the database network id.
-    instance = sys.argv[1]
-    db = Postgres("postgres://pyRot:4h8q(.@localhost/pyRot")
+    # Get network from database.
     try:
         network = db.one("SELECT * FROM networks WHERE id=" + str(instance))
     except: