{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# IRC Bots"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Making bots as a way to re(turn) to gamification mechanisms and find ways to subvert them.\n",
    "\n",
    "Could the prototyping session of today be a moment of collective digestion and testing? \\\n",
    "A moment to bring back references from the last weeks and test ideas through making small prototypes?\n",
    "\n",
    "Links to start in the middle of:\n",
    "\n",
    "* 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\" \n",
    "* https://pad.xpub.nl/p/2022_onesentencegameideas\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "tags": []
   },
   "source": [
    "## How to make an IRC bot?\n",
    "\n",
    "* **example script**: https://pzwiki.wdka.nl/mediadesign/Building_an_IRC_bot_in_python\n",
    "* **IRC server** for this notebooks: https://libera.chat/\n",
    "* **Python library**: irc"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": true,
    "tags": []
   },
   "outputs": [],
   "source": [
    "! pip3 install irc"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## hellobot"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import irc.bot\n",
    "\n",
    "class HelloBot(irc.bot.SingleServerIRCBot):\n",
    "    def __init__(self, channel, nickname, server, port):\n",
    "        irc.bot.SingleServerIRCBot.__init__(self, [(server, port)], nickname, nickname)\n",
    "        self.channel = channel\n",
    "\n",
    "    def on_welcome(self, connection, event):\n",
    "        connection.join(self.channel)\n",
    "\n",
    "    def on_privmsg(self, connection, event):\n",
    "        pass\n",
    "\n",
    "    def on_pubmsg(self, connection, event):\n",
    "        # event.target, event.source, event.arguments, event.type\n",
    "        # print(event) # type: pubmsg, source: Guest92843793284!~Guest9284@2a10:3781:1734:1:fdd8:ab9a:d925:9106, target: #xpub, arguments: ['...'], tags: []\n",
    "        messages = event.arguments\n",
    "        for message in messages:\n",
    "            if \"hello\" in message:\n",
    "                connection.privmsg(self.channel, \"HELLO!!!\")\n",
    "\n",
    "server = \"irc.libera.chat\"\n",
    "port = 6667\n",
    "channel = \"#xpub\"\n",
    "nickname = \"hellobot\"\n",
    "\n",
    "bot = HelloBot(channel, nickname, server, port)\n",
    "bot.start()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## echobot"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import irc.bot\n",
    "\n",
    "class EchoBot(irc.bot.SingleServerIRCBot):\n",
    "    def __init__(self, channel, nickname, server, port):\n",
    "        irc.bot.SingleServerIRCBot.__init__(self, [(server, port)], nickname, nickname)\n",
    "        self.channel = channel\n",
    "\n",
    "    def on_welcome(self, connection, event):\n",
    "        connection.join(self.channel)\n",
    "\n",
    "    def on_privmsg(self, connection, event):\n",
    "        pass\n",
    "\n",
    "    def on_pubmsg(self, connection, event):\n",
    "        # event.target, event.source, event.arguments, event.type\n",
    "        # print(event) # type: pubmsg, source: Guest92843793284!~Guest9284@2a10:3781:1734:1:fdd8:ab9a:d925:9106, target: #xpub, arguments: ['...'], tags: []\n",
    "        messages = event.arguments\n",
    "        for message in messages:\n",
    "            connection.privmsg(self.channel, message)\n",
    "\n",
    "server = \"irc.libera.chat\"\n",
    "port = 6667\n",
    "channel = \"#xpub\"\n",
    "nickname = \"echobot\"\n",
    "\n",
    "bot = EchoBot(channel, nickname, server, port)\n",
    "bot.start()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## timerbot"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import irc.bot\n",
    "import time \n",
    "\n",
    "class TimerBot(irc.bot.SingleServerIRCBot):\n",
    "    def __init__(self, channel, nickname, server, port):\n",
    "        irc.bot.SingleServerIRCBot.__init__(self, [(server, port)], nickname, nickname)\n",
    "        self.channel = channel\n",
    "        \n",
    "        # This is where the last_timer is initiated\n",
    "        self.last_timer = time.time()\n",
    "\n",
    "    def on_welcome(self, connection, event):\n",
    "        connection.join(self.channel)\n",
    "\n",
    "    def on_privmsg(self, connection, event):\n",
    "        pass\n",
    "\n",
    "    def on_pubmsg(self, connection, event):\n",
    "        # event.target, event.source, event.arguments, event.type\n",
    "        # print(event) # type: pubmsg, source: Guest92843793284!~Guest9284@2a10:3781:1734:1:fdd8:ab9a:d925:9106, target: #xpub, arguments: ['...'], tags: []\n",
    "        \n",
    "        # Make a new timestamp \n",
    "        now = time.time()\n",
    "        \n",
    "        # Compare \"now\" with the last_timer\n",
    "        timer = round((now - self.last_timer), 2)\n",
    "        \n",
    "        reply = f\"{ timer } seconds\"\n",
    "        connection.privmsg(self.channel, reply)\n",
    "        \n",
    "        # Reset the last_timer\n",
    "        self.last_timer = now\n",
    "\n",
    "server = \"irc.libera.chat\"\n",
    "port = 6667\n",
    "channel = \"#xpub\"\n",
    "nickname = \"timerbot\"\n",
    "\n",
    "bot = TimerBot(channel, nickname, server, port)\n",
    "bot.start()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
