Omnimaga

General Discussion => Technology and Development => Computer Programming => Topic started by: Munchor on May 07, 2011, 08:57:50 am

Title: IRC Score Bot
Post by: Munchor on May 07, 2011, 08:57:50 am
I made a score bot for IRC:

Code: [Select]
#!/usr/bin/env python

import datetime
import hashlib
import os
import random
import sys
import time
import urllib

from twisted.words.protocols import irc
from twisted.internet import protocol
from twisted.internet import reactor

class MyBot(irc.IRCClient):

    def _get_nickname(self):
        return self.factory.nickname

    nickname = property(_get_nickname)

    def signedOn(self):
        self.join(self.factory.channel)

    def joined(self, channel):
        print "*** Joined %s" % channel
        self.myDict = {}

    def privmsg(self, user, channel, msg):
        user = user.split('!')[0]
        if msg[len(msg)-2:] == "++":
            if msg.split('++')[0] == user:
                self.msg(channel,"You can't plus yourself")
                return

            try:
                self.myDict[msg.split('++')[0]] += 1
                print "+1 for %s" % user
                return
            except KeyError:
                self.myDict[msg.split('++')[0]] = 1
                print "The first +1 for %s" % user
                return

        if msg[len(msg)-2:] == "--":
            try:
                self.myDict[msg.split('--')[0]] = self.myDict[msg.split('--')[0]] - 1
                print "-1 for "+user
                return
            except:
                self.myDict[msg.split('--')[0]] = -1
                print "The first -1 for %s" % user
                return

        if msg.split()[0] == '!score':
            try:
                msgToDisplay = msg.split()[1] + ": " + str(self.myDict[msg.split()[1]])
                self.msg(channel,msgToDisplay)
                print "I answered to %s: %s" % (user,msgToDisplay)
                return
            except KeyError:
                self.msg(channel,"Non-existing member: %s" % msg.split()[1])
                print "Non-existing member: %s" % msg.split()[1]

class MyBotFactory(protocol.ClientFactory):
    protocol = MyBot

    def __init__(self, channel, nickname='scoreBot'):
        self.channel = channel
        self.nickname = nickname

    def clientConnectionLost(self, connector, reason):
        print "Lost connection (%s), reconnecting." % (reason,)
        connector.connect()

    def clientConnectionFailed(self, connector, reason):
        print "Could not connect: %s" % (reason,)

def main():
    network = 'irc.freenode.net'
    reactor.connectTCP(network, 6667, MyBotFactory('#python-forum'))
    reactor.run()

if __name__ == "__main__":
    main()

If you say stuff like:

Code: [Select]
netham45++

It will set netham45's score to what it was +1.

You can also do netham45--.

If you say !score netham45 it will say in a public message netham45's score.

It also won't let you ++ yourself.

The code is quite clean, but for variables' names. There is not a linking function yet (to link usernames). What do you think?
Title: Re: IRC Score Bot
Post by: Jim Bauwens on May 07, 2011, 09:37:43 am
Looks nice, didn't try it though.

Good work!
Title: Re: IRC Score Bot
Post by: aeTIos on May 07, 2011, 09:53:59 am
Nice work, do you have permission for this (just asking, I think you have though :))
Title: Re: IRC Score Bot
Post by: ZippyDee on May 07, 2011, 09:56:24 am
does this account for omnom messages (meaning messages prefixed by "<username>")
Title: Re: IRC Score Bot
Post by: Munchor on May 07, 2011, 10:40:41 am
Oh my, I should have mentioned this is not online in omnimaga's channel (#omnimaga). I have not asked permission for that and I have no idea how it would account for user messages in the forums (there is already a website rating for that).
Title: Re: IRC Score Bot
Post by: Munchor on May 08, 2011, 08:43:13 am
***BUMP***

I'd like to know what you think (mainly the poll) so I know if I'll contact an admin.
Title: Re: IRC Score Bot
Post by: ZippyDee on May 09, 2011, 08:12:30 am
I am aware that it is not yet in the channel, but if it is going to be in the channel, then it should account for users posting through omnom. That's all I was saying.
Title: Re: IRC Score Bot
Post by: Munchor on May 09, 2011, 08:20:54 am
I am aware that it is not yet in the channel, but if it is going to be in the channel, then it should account for users posting through omnom. That's all I was saying.

Oh yeah that will work no problem :D
Title: Re: IRC Score Bot
Post by: TIfanx1999 on May 09, 2011, 08:49:08 am
Honestly, I don't really see much of a point for this. I could definitely see it adding to spam in the chat though.
Title: Re: IRC Score Bot
Post by: DJ Omnimaga on May 09, 2011, 08:19:10 pm
Yeah it should be separate from the forums. However I kinda agree with Art, we already got enough randomness anyway.
Title: Re: IRC Score Bot
Post by: cooliojazz on May 09, 2011, 08:21:38 pm
I mean, UBystand has the same functionality, but have you ever seen it running in #omnomaga?  Nope, I only turn it on it #IRP ;)  That sort of thing is just a bit spammy =P
Title: Re: IRC Score Bot
Post by: Ashbad on May 09, 2011, 08:23:55 pm
Honestly, I don't really see much of a point for this. I could definitely see it adding to spam in the chat though.

cemetech has it, and their channel has less spam than ours ;)

there's no proven correlation between the two.
Title: Re: IRC Score Bot
Post by: DJ Omnimaga on May 09, 2011, 08:25:49 pm
Yeah but #cemetech had less spam than us without the bot, so adding it there wasn't too bad. In #omnimaga there's more randomness so imagine how bad it will be if people go random and starts abusing the bot too. And then it gets much harder for admins to stop the sapam if there is too much.
Title: Re: IRC Score Bot
Post by: Netham45 on May 09, 2011, 08:50:14 pm
I voted no for this, seems like it'd just promote spamming and conflict.
Title: Re: IRC Score Bot
Post by: z80man on May 09, 2011, 08:54:37 pm
Although I like the idea and think it has the potential to work quite well, I voted no because I can imagine users abusing the system and because IRC isn't where we place important updates and posts there is really no need for a respect system. We really just use IRC as a notifier, place for discussions, and randomness.
Title: Re: IRC Score Bot
Post by: DJ Omnimaga on May 09, 2011, 09:33:43 pm
Yeah true. Also, I think two rating systems would get confusing, and I would prefer that rating remains only on posts.
Title: Re: IRC Score Bot
Post by: merthsoft on May 10, 2011, 09:29:49 am
Yeah but #cemetech had less spam than us without the bot, so adding it there wasn't too bad. In #omnimaga there's more randomness so imagine how bad it will be if people go random and starts abusing the bot too. And then it gets much harder for admins to stop the sapam if there is too much.
It definitely can get spammy. There're also a lot of junk entries that I go through and clean up. An addition that helps to abate everyone wanting to check all the scores is that you can access the scores online, so if people get overly spammy you can point them there. I think if you include that, you can lessen the abuse. That being said, we do have to be pretty vigilant when it comes to moderating DecBot abuse.
Title: Re: IRC Score Bot
Post by: Munchor on May 10, 2011, 04:00:10 pm
Thanks everyone for your opinions. I changed my opinion and will not even ask to upload this at #omnimaga.
Title: Re: IRC Score Bot
Post by: DJ Omnimaga on May 10, 2011, 04:50:42 pm
Ah ok. Yeah I think the issue is that it could get out of control, and since moderators are often busy, it would be hard to stop :<
Title: Re: IRC Score Bot
Post by: AngelFish on May 10, 2011, 05:28:28 pm
As much as I like Decbot on #cemetech, it'd definitely get abused in #omnimaga. For example, "Lobster" would probably get >9000 ratings in the first 42 hours. And the memes that I'm obviously inserting in here would get inflated respect too. Let's just keep it on the forums.
Title: Re: IRC Score Bot
Post by: DJ Omnimaga on May 10, 2011, 09:52:05 pm
Indeed, and I am sure some people would try to make sure someone's rating remains exactly at 666. Omnimaga in general mostly got younger users since it attracts a lot more newer coders than other ones, so inevitably there are some strange things that are going to happen. :P (no offense intended of course)
Title: Re: IRC Score Bot
Post by: Munchor on May 12, 2011, 01:47:10 pm
Even if this is never going to be in #omnimaga, I worked a bit on it:

Code: [Select]
#!/usr/bin/env python

import datetime
import hashlib
import os
import random
import sys
import time
import urllib

from twisted.words.protocols import irc
from twisted.internet import protocol
from twisted.internet import reactor

class MyBot(irc.IRCClient):

    def _get_nickname(self):
        return self.factory.nickname

    nickname = property(_get_nickname)

    def signedOn(self):
        self.join(self.factory.channel)

    def joined(self, channel):
        print "*** Joined %s" % channel
        self.myDict = {}

    def privmsg(self, user, channel, msg):
        user = user.split('!')[0]
        if msg[len(msg)-2:] == "++":
            if msg.split('++')[0] == user:
                self.msg(channel,"You can't plus yourself")
                return

            try:
                self.myDict[msg.split('++')[0]] += 1
                print "+1 for %s by %s" % (msg.split('++')[0],user)
                return
            except KeyError:
                self.myDict[msg.split('++')[0]] = 1
                print "The first +1 for %s by %s" % (msg.split('++')[0],user)
                return

        if msg[len(msg)-2:] == "--":
            try:
                self.myDict[msg.split('--')[0]] = self.myDict[msg.split('--')[0]] - 1
                print "-1 for %s by %s" % (msg.split('++')[0],user)
                return
            except:
                self.myDict[msg.split('--')[0]] = -1
                print "The first -1 for %s by %s" % (msg.split('++')[0],user)
                return

        if msg.split()[0] == '!score':
            try:
                msgToDisplay = msg.split()[1] + ": " + str(self.myDict[msg.split()[1]])
                self.msg(channel,msgToDisplay)
                print "I answered to %s: %s" % (user,msgToDisplay)
                return
            except KeyError:
                self.msg(channel,"Non-existing member: %s" % msg.split()[1])
                print "Non-existing member: %s" % msg.split()[1]

class MyBotFactory(protocol.ClientFactory):
    protocol = MyBot

    def __init__(self, channel, nickname='scoreBot'):
        self.channel = channel
        self.nickname = nickname

    def clientConnectionLost(self, connector, reason):
        print "Lost connection (%s), reconnecting." % (reason,)
        connector.connect()

    def clientConnectionFailed(self, connector, reason):
        print "Could not connect: %s" % (reason,)

def main():
    network = 'irc.freenode.net'
    reactor.connectTCP(network, 6667, MyBotFactory('#python-forum'))
    reactor.run()

if __name__ == "__main__":
    main()

Actually, I just made even more updates:

Code: [Select]
#!/usr/bin/env python

import datetime
import hashlib
import os
import random
import sys
import time
import urllib

from twisted.words.protocols import irc
from twisted.internet import protocol
from twisted.internet import reactor

class MyBot(irc.IRCClient):

    def _get_nickname(self):
        return self.factory.nickname

    nickname = property(_get_nickname)

    def signedOn(self):
        self.join(self.factory.channel)

    def joined(self, channel):
        print "*** Joined %s" % channel
        self.myDict = {}

    def privmsg(self, user, channel, msg):
        user = user.split('!')[0]
        if msg[len(msg)-2:] == "++":
            if msg.split('++')[0] == user:
                self.msg(channel,"You can't plus yourself")
                return

            try:
                self.myDict[msg.split('++')[0]] += 1
                print "+1 for %s by %s" % (msg.split('++')[0],user)
                return
            except KeyError:
                self.myDict[msg.split('++')[0]] = 1
                print "The first +1 for %s by %s" % (msg.split('++')[0],user)
                return

        elif msg[len(msg)-2:] == "--":
            try:
                self.myDict[msg.split('--')[0]] = self.myDict[msg.split('--')[0]] - 1
                print "-1 for %s by %s" % (msg.split('++')[0],user)
                return
            except:
                self.myDict[msg.split('--')[0]] = -1
                print "The first -1 for %s by %s" % (msg.split('++')[0],user)
                return

        elif msg.split()[0] == '!score':
            try:
                msgToDisplay=msg.split()[1]+": %s" % str(self.myDict[msg.split()[1]])
                self.msg(channel,msgToDisplay)
                print "I answered to %s: %s" % (user,msgToDisplay)
                return
            except KeyError:
                msgToDisplay="Non-existing member: %s" % msg.split()[1]
                self.msg(channel,msgToDisplay)
                print "I answered to %s: Non-existing member: %s" % (user,msg.split()[1])

class MyBotFactory(protocol.ClientFactory):
    protocol = MyBot

    def __init__(self, channel, nickname='scoreBot'):
        self.channel = channel
        self.nickname = nickname

    def clientConnectionLost(self, connector, reason):
        print "Lost connection (%s), reconnecting." % (reason,)
        connector.connect()

    def clientConnectionFailed(self, connector, reason):
        print "Could not connect: %s" % (reason,)

def main():
    network = 'irc.freenode.net'
    reactor.connectTCP(network, 6667, MyBotFactory('#python-forum'))
    reactor.run()

if __name__ == "__main__":
    main()