Skip to content
Browse files

Merge pull request #6 from LiquidHelium/master

Massive update with commands support
  • Loading branch information...
2 parents 6959efb + f65c20b commit 14bc5f86f66ccee03edf2127e48808ac440a426d @LiquidHelium LiquidHelium committed Sep 26, 2012
Showing with 47 additions and 6 deletions.
  1. +18 −4 commandModule.py
  2. +3 −0 commands/__init__.py
  3. +10 −0 commands/speak.py
  4. +7 −2 core.py
  5. +9 −0 ircHelpers.py
View
22 commandModule.py
@@ -1,11 +1,25 @@
import re
+commandList = dict()
+def command(commandName, **options):
+ def decorator(f):
+ commandList[commandName] = f
+ return f
+ return decorator
+
+from commands import *
+
class CommandModule:
- def __init__(self):
+ def __init__(self, ircHelper):
self.regexSplitCommand = re.compile(r"!!(?P<command>\w+)\s(?P<arguments>.*).*")
+
+ self.ircHelper = ircHelper
def runCommand(self, command):
split = self.regexSplitCommand.match(command)
- commandName = split.group('command')
- commandArguments = split.group('arguments')
- print commandName + "---" + commandArguments
+ if split:
+ commandName = split.group('command')
+ commandArguments = split.group('arguments')
+ if commandList.has_key(commandName):
+ commandList[commandName](self.ircHelper, commandArguments)
+ print commandName + "---" + commandArguments
View
3 commands/__init__.py
@@ -0,0 +1,3 @@
+import os
+import glob
+__all__ = [ os.path.basename(f)[:-3] for f in glob.glob(os.path.dirname(__file__)+"/*.py")]
View
10 commands/speak.py
@@ -0,0 +1,10 @@
+#example of how to make commands
+#make another file in this directory called something.py
+
+
+
+from commandModule import command #import the command decorator
+@command('say') #decorate a function with it, and supply the command you want it to be triggerd by. for example this one will be triggered when someone says !!say something
+def speak(ircHelper, arguments):#the function takes an irchelper and arguments
+ ircHelper.send('PRIVMSG %s :%s\r\n' % (ircHelper.channel, arguments)) #arguments are what comes after the command, for example !say hello, hello witll be the argument
+ #irchelper is how you interact with the irc, it has functions and vars that can be used
View
9 core.py
@@ -1,6 +1,7 @@
import socket, re
from commandModule import CommandModule
+from ircHelpers import IRCHelper
class IRCBot:
def __init__(self, network, port, channel, nickname, tempCacheSize=4096):
@@ -12,9 +13,13 @@ def __init__(self, network, port, channel, nickname, tempCacheSize=4096):
self.tempCacheSize = tempCacheSize
- self.commandModule = CommandModule()
+ self.ircHelper = IRCHelper(self)
+
+ self.commandModule = CommandModule(self.ircHelper)
self.regexIsCommand = re.compile(r".*(?P<command>!!.*)")
+
+
def run(self):
self.socket.connect((self.network, self.port))
self.log(self.socket.recv(self.tempCacheSize))
@@ -46,5 +51,5 @@ def log(self, stringToLog):
#change eventually to log in a file but for now print is fine
print stringToLog
-ircbot = IRCBot('irc.freenode.net', 6667, '#progether', 'tmpircbotname')
+ircbot = IRCBot('irc.freenode.net', 6667, '#progether', 'WorkingIRCBot')
ircbot.run()
View
9 ircHelpers.py
@@ -0,0 +1,9 @@
+class IRCHelper:
+ def __init__(self, ircbot):
+ self.ircbot = ircbot
+
+ self.channel = self.ircbot.channel
+
+ def send(self, thingToSend):
+ self.ircbot.socket.send(thingToSend)
+

0 comments on commit 14bc5f8

Please sign in to comment.
Something went wrong with that request. Please try again.