uniborg/stdplugins/headpat.py

45 lines
1.3 KiB
Python
Raw Normal View History

2018-05-07 18:09:42 +00:00
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
from telethon import events, utils
from telethon.tl import types, functions
import re
import asyncio
import random
import json
import urllib.request
import urllib.parse
pats = []
2018-05-13 10:33:40 +00:00
oops = "OOPSIE WOOPSIE!! Uwu We madea fucky wucky!! A wittle fucko boingo! " \
"The code monkeys at our headquarters are working VEWY HAWD to fix " \
"this!"
2018-05-07 18:09:42 +00:00
@borg.on(events.NewMessage)
async def on_pat(event):
2018-05-16 19:16:38 +00:00
if event.forward:
return
2018-05-13 10:33:40 +00:00
user = borg.me.username
2018-05-07 18:09:42 +00:00
2018-05-13 10:33:40 +00:00
if not user or not re.match(fr"(?i)/headpat@{user}", event.raw_text):
2018-05-07 18:09:42 +00:00
return
global pats
if not pats:
try:
pats = json.loads(urllib.request.urlopen(urllib.request.Request(
2018-05-13 10:33:40 +00:00
"http://headp.at/js/pats.json",
headers={"User-Agent": "Mozilla/5.0 (X11; U; Linux i686) "
"Gecko/20071127 Firefox/2.0.0.11"}
)).read().decode("utf-8"))
2018-05-07 18:09:42 +00:00
except Exception as e:
print(e)
await event.reply(oops)
return
2018-05-13 10:33:40 +00:00
choice = urllib.parse.quote(random.choice(pats))
await event.reply(f"[Pat!](https://headp.at/pats/{choice})")