Added 'rfk' command (robot finds kitten) to misc. Added 'eval' command to misc (evaluates python code. uses appspot api url request used by sopel).
This commit is contained in:
parent
e8b41a3b2f
commit
39295f3818
|
@ -2,8 +2,56 @@
|
|||
misc
|
||||
"""
|
||||
|
||||
import os
|
||||
import urllib
|
||||
import random
|
||||
import functools
|
||||
import gzip
|
||||
|
||||
import requests
|
||||
|
||||
from bot import command
|
||||
|
||||
# public python GAE api url. thanks sopel
|
||||
PY_APP_URL = 'https://tumbolia-sopel.appspot.com/py/'
|
||||
# requests session
|
||||
#req_ses = requests.Session()
|
||||
|
||||
# function that sends eval request. runs in separate thread
|
||||
def do_eval( evstr ):
|
||||
rstr = 'Error.'
|
||||
|
||||
try:
|
||||
res = requests.get( PY_APP_URL + 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: rstr = random.random() < 0.25 and 'I\'ll do it later.' or 'Response timed out.'
|
||||
|
||||
return rstr
|
||||
|
||||
@command( 'eval' )
|
||||
async def cmd_eval( b, p, c ):
|
||||
""" Evaluate a python expression. Trust me, it's safe. """
|
||||
evstr = urllib.parse.quote( ' '.join( p ) )
|
||||
# do the actual request in a separate thread
|
||||
rstr = await b._loop.run_in_executor( None, functools.partial( do_eval, evstr ) ) #do_eval, evstr, c )
|
||||
|
||||
c['con'].say_to( c['dst'], '> {}'.format( rstr ) )
|
||||
|
||||
@command( 'rfk' )
|
||||
async def cmd_rfk( b, 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 ) )
|
||||
|
||||
@command( 'owo' )
|
||||
async def cmd_owo( b, p, c ):
|
||||
c['con'].say_to( c['dst'], 'nou' )
|
||||
|
|
Binary file not shown.
Loading…
Reference in New Issue