dustbot/modules/misc.py

62 lines
1.7 KiB
Python

"""
misc
"""
import os
import urllib
import random
import functools
import gzip
import requests
import bot
# public python GAE api url. thanks sopel
PY_APP_URL = 'https://tumbolia-sopel.appspot.com/py/{}'
# requests session
#req_ses = requests.Session()
@bot.command( 'eval' )
async def cmd_eval( p, c ):
""" Evaluates Python code. Trust me, it's safe. """
evstr = urllib.parse.quote( ' '.join( p ) )
try:
res = await bot.get_loop().run_in_executor( None,
functools.partial( requests.get, PY_APP_URL.format( evstr ), timeout=3.0 ) )
rstr = res.text.rstrip().lstrip()
if not res.ok: rstr = 'Server error ' + str(res.status_code)+'.'
if rstr == '': rstr = 'No response.'
except requests.RequestException as e:
rstr = random.random() < 0.25 and 'I\'ll do it later.' or 'Exception during request.'
if not isinstance( e, requests.Timeout ):
rstr += ' ({})'.format( str(e) )
else: rstr += ' (Request timed out.)'
c['con'].say_to( c['dst'], '> {}'.format( rstr ) )
# parser test
@bot.parser()
def parse_misc( c, d, n, m ):
if len(m.split()) > 0:
if m.split()[0].lower() == '!botlist' or m.split()[0].lower() == '!rollcall':
pfx = bot.get_cfg()['bot'].get( 'prefix' )
c.say_to( d, ('> dustbot | Owner: {} | '
'Source: https://tildegit.org/slipyx/dustbot | Prefix: \'{}\'. | '
'Commands: See \'{}list\'.').format( c.cfg['usr']['owner'],pfx,pfx ) )
@bot.command( 'rfk' )
async def cmd_rfk( p, c ):
""" Try to find kitten! """
nki = ''
if random.randint(1,20) == random.randint(1,20): nki = 'YOU FOUND KITTEN! CONGRATULATIONS!'
else: nki = random.choice( gzip.open(
os.path.join( os.path.dirname( __file__ ), 'text/vanilla.nki.gz' ) ).readlines()
).rstrip().decode()
c['con'].say_to( c['dst'], '{}: {}'.format( c['nick'], nki ) )