vibe-plugin

vibe plugin for nurdspace ghbot
git clone git://r-36.net/vibe-plugin
Log | Files | Refs

vibe.py (3121B)


      1 #!/usr/bin/env python3
      2 # coding=utf-8
      3 # 
      4 # adapted from nurdspace-ircbot-plugin-python-example
      5 # by 20h, released under GPLv3
      6 #
      7 
      8 import paho.mqtt.client as mqtt
      9 import threading
     10 import socket
     11 import sys
     12 import time
     13 import random
     14 
     15 mqtt_server  = 'mqtt.vm.nurd.space'
     16 topic_prefix = 'GHBot/'
     17 channels     = ['nurds']
     18 prefix       = '!'
     19 
     20 def gen_dance_moves(nmoves=10, vibemove="ascii"):
     21     ascii_moves = ["\\o/", "\\o_", "_o_", "_o/", "~o/", "\\o~", "~o~",
     22             "-o/", "\\o-", "-o-", "\\o.", ".o/", ".o.",
     23             "\\o7", "_o7", "-o7", ".o7", "~o7", "\\^o^/",
     24             "\\^o_", "_o^/", "~o^/", "\\^o~", "-o^/",
     25             "\\^o-", "\\^o.", ".o^/", "\\^o7",
     26             "(>OoO)>", "<(OoO<)", "/o/", "\\o\\"]
     27     egyptian_moves = [
     28             "𓀀", "𓀁", "𓀂", "𓀃", "𓀉", "𓀊",
     29             "𓀋", "𓀏", "𓀐", "𓀒", "𓀓", "𓀔",
     30             "𓀕", "𓀖", "𓀞", "𓀟", "𓀠", "𓀡",
     31             "𓀢", "𓀣", "𓀤", "𓀥", "𓁌", "𓁎",
     32             "𓁏"]
     33 
     34     moves = ascii_moves
     35     if vibemove == "egytian":
     36         moves = egyptian_moves
     37     elif vibemove == "asciiegyptian":
     38         moves = egyptian_moves + ascii_moves
     39 
     40     ostr = ""
     41     for i in range(nmoves):
     42         if len(ostr) > 0:
     43             ostr += " "
     44         ostr += random.choice(moves)
     45     return ostr
     46 
     47 def announce_commands(client):
     48     target_topic = f'{topic_prefix}to/bot/register'
     49     client.publish(target_topic, 'cmd=vibe|descr=vibe like there is no tomorrow')
     50 
     51 def on_message(client, userdata, message):
     52     global prefix
     53     text = message.payload.decode('utf-8')
     54     topic = message.topic[len(topic_prefix):]
     55     if topic == 'from/bot/command' and text == 'register':
     56         announce_commands(client)
     57         return
     58     if topic == 'from/bot/parameter/prefix':
     59         prefix = text
     60         return
     61     if text[0] != prefix:
     62         return
     63 
     64     parts = topic.split('/')
     65     channel = parts[2] if len(parts) >= 3 else 'nurds'
     66     nick = parts[3] if len(parts) >= 4 else 'jemoeder'
     67     if channel in channels or (len(channel) >= 1 and channel[0] == '\\'):
     68         tokens = text.split(' ')
     69         command = tokens[0][1:]
     70         response_topic = f'{topic_prefix}to/irc/{channel}/notice'
     71         if command == 'vibe':
     72             if len(tokens) > 1:
     73                 dancemoves = gen_dance_moves(vibemove=tokens[1])
     74             else:
     75                 dancemoves = gen_dance_moves()
     76             client.publish(response_topic, dancemoves)
     77 
     78 def on_connect(client, userdata, flags, rc):
     79     client.subscribe(f'{topic_prefix}from/irc/#')
     80     client.subscribe(f'{topic_prefix}from/bot/command')
     81 
     82 def announce_thread(client):
     83     while True:
     84         try:
     85             announce_commands(client)
     86             time.sleep(4.1)
     87         except Exception as e:
     88             print(f'Failed to announce: {e}')
     89 
     90 client = mqtt.Client(f'{socket.gethostname()}_{sys.argv[0]}', clean_session=False)
     91 client.on_message = on_message
     92 client.on_connect = on_connect
     93 client.connect(mqtt_server, port=1883, keepalive=4, bind_address="")
     94 t = threading.Thread(target=announce_thread, args=(client,))
     95 t.start()
     96 client.loop_forever()