You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
6.3 KiB
6.3 KiB
IRC Bots¶
Making bots as a way to re(turn) to gamification mechanisms and find ways to subvert them.
Could the prototyping session of today be a moment of collective digestion and testing? \ A moment to bring back references from the last weeks and test ideas through making small prototypes?
Links to start in the middle of:
- https://pad.xpub.nl/p/18012022 - Pad of the session on "Defining play and games, Blurrying the lines between labour and play: gamification and playbour"
- https://pad.xpub.nl/p/2022_onesentencegameideas
How to make an IRC bot?¶
- example script: https://pzwiki.wdka.nl/mediadesign/Building_an_IRC_bot_in_python
- IRC server for this notebooks: https://libera.chat/
- Python library: irc
In [ ]:
! pip3 install irc
hellobot¶
In [ ]:
import irc.bot class HelloBot(irc.bot.SingleServerIRCBot): def __init__(self, channel, nickname, server, port): irc.bot.SingleServerIRCBot.__init__(self, [(server, port)], nickname, nickname) self.channel = channel def on_welcome(self, connection, event): connection.join(self.channel) def on_privmsg(self, connection, event): pass def on_pubmsg(self, connection, event): # event.target, event.source, event.arguments, event.type # print(event) # type: pubmsg, source: Guest92843793284!~Guest9284@2a10:3781:1734:1:fdd8:ab9a:d925:9106, target: #xpub, arguments: ['...'], tags: [] messages = event.arguments for message in messages: if "hello" in message: connection.privmsg(self.channel, "HELLO!!!") server = "irc.libera.chat" port = 6667 channel = "#xpub" nickname = "hellobot" bot = HelloBot(channel, nickname, server, port) bot.start()
echobot¶
In [ ]:
import irc.bot class EchoBot(irc.bot.SingleServerIRCBot): def __init__(self, channel, nickname, server, port): irc.bot.SingleServerIRCBot.__init__(self, [(server, port)], nickname, nickname) self.channel = channel def on_welcome(self, connection, event): connection.join(self.channel) def on_privmsg(self, connection, event): pass def on_pubmsg(self, connection, event): # event.target, event.source, event.arguments, event.type # print(event) # type: pubmsg, source: Guest92843793284!~Guest9284@2a10:3781:1734:1:fdd8:ab9a:d925:9106, target: #xpub, arguments: ['...'], tags: [] messages = event.arguments for message in messages: connection.privmsg(self.channel, message) server = "irc.libera.chat" port = 6667 channel = "#xpub" nickname = "echobot" bot = EchoBot(channel, nickname, server, port) bot.start()
timerbot¶
In [ ]:
import irc.bot import time class TimerBot(irc.bot.SingleServerIRCBot): def __init__(self, channel, nickname, server, port): irc.bot.SingleServerIRCBot.__init__(self, [(server, port)], nickname, nickname) self.channel = channel # This is where the last_timer is initiated self.last_timer = time.time() def on_welcome(self, connection, event): connection.join(self.channel) def on_privmsg(self, connection, event): pass def on_pubmsg(self, connection, event): # event.target, event.source, event.arguments, event.type # print(event) # type: pubmsg, source: Guest92843793284!~Guest9284@2a10:3781:1734:1:fdd8:ab9a:d925:9106, target: #xpub, arguments: ['...'], tags: [] # Make a new timestamp now = time.time() # Compare "now" with the last_timer timer = round((now - self.last_timer), 2) reply = f"{ timer } seconds" connection.privmsg(self.channel, reply) # Reset the last_timer self.last_timer = now server = "irc.libera.chat" port = 6667 channel = "#xpub" nickname = "timerbot" bot = TimerBot(channel, nickname, server, port) bot.start()
In [ ]: