from twisted.web.resource import Resource from bot import BotPlugin class HelloPage(Resource): def render_GET(self, request): return 'Hello World' helloweb = BotPlugin() @helloweb.init def init(bot): if bot.webresource is None: print "Webserver isn't available!" return bot.webresource.putChild("hello", HelloPage())
# -*- coding: utf-8 -*- from bot import BotPlugin import requests btc = BotPlugin() @btc.command('btc') def get_btc(bot, user, channel, msg): """ Current bitcoin value on Mt. Gox. Usage: !btc [last|buy|sell|last_local|last_all|last_orig] default is last """ args = msg.split()[0:] value_type = 'last' if len(args) > 0: if args[0] in [ 'last', 'buy', 'sell', 'last_local', 'last_all', 'last_orig' ]: value_type = args[0] usd = requests.get('http://data.mtgox.com/api/1/BTCUSD/ticker_fast') eur = requests.get('http://data.mtgox.com/api/1/BTCEUR/ticker_fast') m = u"\u0002MtGox\u000F (%s): %.2f USD - %.2f EUR" % ( value_type, float(usd.json()['return'][value_type]['value']), float(eur.json()['return'][value_type]['value']), ) bot.msg(channel, m)
# -*- coding: utf-8 -*- from bot import BotPlugin test = BotPlugin() @test.init def init(bot): print "init" test.stored.setdefault("abc", 0) @test.command('test') def say_something(bot, user, channel, msg): print "test" with test.stored("abc") as x: bot.msg(channel, x.data) x.data += 1 # @test.periodic(1) # def x(): # print "a"
# -*- coding: utf-8 -*- from bot import BotPlugin channel = BotPlugin() @channel.command('join') def join(bot, user, channel, msg): """ Join a channel: join <channel> [<channel> ...] """ args = msg.split() if msg and len(args) > 0: for arg in args: bot.join(str(arg)) bot.msg(channel, 'Joined %s' % arg) @channel.command('part') def part(bot, user, channel, msg): """ Leave a channel: part <channel> [<channel> ...] """ args = msg.split() if msg and len(args) > 0: for arg in args: bot.part(str(arg))
# coding=utf-8 from bot import BotPlugin import time date = BotPlugin() @date.command('date') def say_date(bot, user, channel, msg): """ show current date """ m = "%s" % time.strftime("%a, %b %d, %Y", time.localtime()) bot.msg(channel, m) @date.command('time') def say_time(bot, user, channel, msg): """ show current time """ m = "%s" % time.strftime("%I:%M %p", time.localtime()) bot.msg(channel, m) @date.command('dt') def say_datetime(bot, user, channel, msg): """Print current date and time""" m = "Today is %s and it's %s" % (time.strftime( "%a, %b %d, %Y", time.localtime()), time.strftime("%I:%M %p", time.localtime())) bot.msg(channel, m)
# -*- coding: utf-8 -*- from bot import BotPlugin from os.path import expanduser, expandvars import json shorten = BotPlugin() def get_google_api_key(fn='~/.config/.google-api-key'): """ Reads the google api key from a file. Put only the key inside the file. Nothing else. Get your own api key from https://code.google.com/apis/console """ fn = expandvars(expanduser(fn)) try: f = open(fn) key = f.read().strip() f.close() except: key = False return key def shortenit(longurl): """ shorten the longurl using google shortener adds http:// if missing. for https you have to add it yourself before calling this function. reads the google api key using the get_google_api_key function return json object if successfull; False otherwise.
# -*- coding: utf-8 -*- from bot import BotPlugin from dice import roll import random dice = BotPlugin() @dice.init def init(bot): random.seed() @dice.command(['roll', 'dice', 'wuerfel']) def do_roll(bot, user, channel, msg): bot.msg(channel, roll(msg))
# -*- coding: utf-8 -*- from bot import BotPlugin import datetime import math from twisted.python import log import logging alarm = BotPlugin() @alarm.command('alarm') def alarmcmd(bot, user, channel, msg): """ alarm is the German name for remind. """ # TODOs: # - save all reminders in the plugin for a !showalarms command # - maybe: add !delalarm <alarmid> def showhelp(): bot.msg(channel, "[<nick>] <time or minutes> <message>") def startmsg(username, seconds): minutes = math.floor(seconds / 60) pl = '' if minutes != 1: pl = 's' m = "Reminding %s in %i minute%s: %s" % (username, minutes, pl, message) bot.msg(channel, m)
# -*- coding: utf-8 -*- from bot import BotPlugin yo = BotPlugin() def send_message(bot, user, channel, msg, message): username = user.split('!')[0] m = "%s sends you: %s" % (username, message) sp = msg.split() try: for elem in sp: to_user = str(elem) bot.msg(to_user, m) except: return False @yo.command('yo') def show_yo(bot, user, channel, msg): """ Send yo to someone """ if send_message(bot, user, channel, msg, "Yo!"): bot.msg(channel, "!yo <nick>") @yo.command('noe') def show_noe(bot, user, channel, msg): """ Send noe to someone
# -*- coding: utf-8 -*- from bot import BotPlugin import subprocess git = BotPlugin() def git_head(folder, branch='master'): m = subprocess.Popen('cd %s; git log %s --pretty=format:' '"%%h >>>%%s<<< [%%aN] -- %%ar" HEAD -n 1' % (folder, branch), shell=True, stdout=subprocess.PIPE).stdout return m.read() @git.command('pull') def git_pull(bot, user, channel, msg): b = subprocess.Popen('cd ~/entchen; git pull', shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE).stdout m = b.read().strip() bot.msg(channel, m) #@git.command('botversion') def say_git_version(bot, user, channel, msg): b = subprocess.Popen('git log --pretty=format:"%h -- %ar" HEAD -n 1', shell=True, stdout=subprocess.PIPE).stdout
# -*- coding: utf-8 -*- from bot import BotPlugin import re regex = BotPlugin() search_re = re.compile(r'(?:^|\s)s/([^/]+)/([^/]*)/([gi]*)(?:\s|$)') match_re = re.compile(r'^([^\s]+):\s+s/([^/]+)/([^/]*)/([gi]*)$') lastline = {} def dosub(orig, repl, line, flags): f = 0 count = 1 if "i" in flags: f = re.I if "g" in flags: count = 0 line2 = re.sub(orig, repl, line, count, f) if line2 != line: return line2 return False @regex.any def find_regex(bot, user, channel, msg): user = user.split('!')[0] match = match_re.match(msg) if match: (user2, orig, repl, flags) = match.groups() line = lastline.get((user2, channel)) if not line:
# -*- coding: utf-8 -*- from bot import BotPlugin from lib.pluginloader import get_plugin, _plugins, _factory helper = BotPlugin() @helper.command('help') def help(bot, user, channel, msg): """help <plugin> [<command>] - Show help for plugin/command""" args = msg.split(' ', 2)[0:] if len(args) == 1 and args[0]: for arg in args: plugin = get_plugin(arg) if plugin: l = [] for k in plugin._handlers_msg.values(): if hasattr(k, '__handler_type__') and \ k.__handler_type__ == 'command': if isinstance(k.__command_head__, list): l.append( '(%s)' % unicode( '|'.join(k.__command_head__) ) ) else: l.append(unicode(k.__command_head__)) bot.msg( channel, u'Available commands for %s: %s' % ( arg,
# -*- coding: utf-8 -*- from bot import BotPlugin sysinfo = BotPlugin() def parse_meminfo(): m = {} f = open('/proc/meminfo') while True: l = f.readline() if not l: break (key, value) = l.split(':') m[key.strip()] = value.strip() return m @sysinfo.command('mem') def meminfo(bot, user, channel, msg): meminfo = parse_meminfo() (total, tunit) = meminfo['MemTotal'].split() (free, funit) = meminfo['MemFree'].split() total = int(total) free = int(free) usedp = float(total - free) / float(total) * 100.0 if tunit == funit: if tunit == "kB": bot.msg( channel, "Memory usage: %.2f%% total: %s mB free: %s mB" % (usedp, total / 1024, free / 1024))
# coding=utf-8 from bot import BotPlugin birthday = BotPlugin() @birthday.command('birthday') def birthdaycmd(bot, user, channel, msg): bot.msg(channel, 'kindchen singt mit allen im Chor: ' '"Happy Birthday dear %s, Happy Birthday to you!"' % msg)
# -*- coding: utf-8 -*- from bot import BotPlugin #import copy import random #import os.path #import re randomness = BotPlugin() @randomness.command(['coin', 'flip']) def coincmd(bot, user, channel, msg): bot.msg(channel, random.choice(('head', 'tail'))) @randomness.command([u'münze', 'muenze']) def muenzecmd(bot, user, channel, msg): bot.msg(channel, random.choice(('Kopf', 'Zahl'))) @randomness.command('frage') def magiccmd(bot, user, channel, msg): choices = [ "Ja.", "Nein.", "ja, gewiss", "nein, niemals", "was faellt dir ein!", "Welch schaendlicher Gedanke!", "Das wuerde dem Meister gefallen", "Mitnichten wuerde ich das verneinen",
# coding=utf-8 import re from twisted.web.resource import Resource from bot import BotPlugin # noinspection PyUnresolvedReferences from zipa import api_github_com as gh issues = BotPlugin() regex = re.compile(r'^#?(?P<n>\d+)$') def numberish(message_string): """ Checks if the given string could represent a issue ID for github :param message_string: string to check :return: bool """ assert message_string potential_number = message_string.split()[0] if regex.match(potential_number): return True return False
# -*- coding: utf-8 -*- from bot import BotPlugin #import time import os gw = BotPlugin() def get_gw_from_file(fn=None): """ Read data from file. Syntax: <nickname>=<todos> """ d = {} if not fn: fn = os.path.join(os.path.dirname(__file__), 'gw.data') fp = open(fn) for line in fp.readlines(): x = line.split('=') if len(x) > 1: d[x[0]] = x[1].strip() fp.close() return d @gw.command('gw') def gw_command(bot, user, channel, msg): """ !gw <nick> <command> [<args>] default show """
# -*- coding: utf-8 -*- from bot import BotPlugin from twisted.internet import defer userinfo = BotPlugin() @userinfo.command("whoami") def whoami(bot, user, channel, message): nickname, hostmask = user.split('!', 1) def say_result(result): if result: bot.msg(channel, "%s is logged in as %s" % (nickname, result)) else: bot.msg(channel, "%s is not logged in" % (nickname)) if nickname not in bot._whoiscallbacks: bot._whoiscallbacks[nickname] = defer.Deferred() d = bot._whoiscallbacks[nickname] d.addCallback(say_result) bot.msg(channel, "looking up %s" % (nickname)) bot.whois(nickname)