Skip to content

Commit

Permalink
Adds clap command
Browse files Browse the repository at this point in the history
  • Loading branch information
austinpray committed Dec 1, 2017
1 parent e51f3a1 commit a2f3f64
Show file tree
Hide file tree
Showing 6 changed files with 106 additions and 15 deletions.
4 changes: 4 additions & 0 deletions bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from sqlalchemy.orm import sessionmaker
from slackclient import SlackClient

from kizuna.ClapCommand import ClapCommand
from kizuna.Kizuna import Kizuna
from kizuna.PingCommand import PingCommand
from kizuna.AtGraphCommand import AtGraphCommand
Expand Down Expand Up @@ -36,6 +37,9 @@ def signal_handler(signal, frame):
pc = PingCommand()
k.register_command(pc)

clap = ClapCommand()
k.register_command(clap)

at_graph_command = AtGraphCommand(Session)
k.register_command(at_graph_command)

Expand Down
20 changes: 5 additions & 15 deletions kizuna/AtGraphCommand.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@
from threading import Thread
from time import sleep

from argparse import ArgumentParser
from io import StringIO
from .SlackArgumentParser import SlackArgumentParser


class AtGraphCommand(Command):
Expand All @@ -19,29 +18,20 @@ def __init__(self, db_session) -> None:

self.available_layouts = ['dot', 'neato', 'fdp', 'sfdp', 'twopi', 'circo', 'raw']

parser = ArgumentParser(prog='kizuna mentions', description='Generate a mentions graph', add_help=False)
parser.add_argument('-h',
'--help',
action='store_true',
dest='help',
help='show this help message and exit')
parser = SlackArgumentParser(prog='kizuna mentions', description='Generate a mentions graph', add_help=False)
self.add_help_command(parser)

markdown_available_layouts = list(map(lambda s: '`{}`'.format(s), self.available_layouts))
parser.add_argument('--layout',
dest='layout',
default='dot',
help='Defaults to `dot`. Can be any of ' + ', '.join(markdown_available_layouts))

help_text_capture = StringIO()
parser.print_help(file=help_text_capture)
help_text = help_text_capture.getvalue()
help_text = help_text.replace('usage: ', '')
self.help_text = help_text

self.set_help_text(parser)
self.parser = parser

pattern = "mentions(?: (.*))?$"
super().__init__('mention-graph', pattern, help_text, True)
super().__init__('mention-graph', pattern, self.help_text, True)

@staticmethod
def linear_scale(old_max, old_min, new_max, new_min, value):
Expand Down
54 changes: 54 additions & 0 deletions kizuna/ClapCommand.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
from .Command import Command
from .strings import random_insult
from .SlackArgumentParser import SlackArgumentParser, SlackArgumentParserException

from argparse import REMAINDER


class ClapCommand(Command):
def __init__(self) -> None:
parser = SlackArgumentParser(prog='kizuna clap', description='obnoxiously clap', add_help=False)
self.add_help_command(parser)

parser.add_argument('-s',
'--separator',
dest='separator',
default=':clap:',
help='defaults to :clap:')

parser.add_argument('-a',
'--at',
dest='at',
metavar='PERSON',
help='a user to send this message to')

parser.add_argument('message',
metavar='MESSAGE',
nargs=REMAINDER,
help='the message to clappify')

self.set_help_text(parser)
self.parser = parser
super().__init__('clap', 'clap (.*)', help_text=self.help_text, is_at=True)

def respond(self, slack_client, message, matches):
send = self.send_factory(slack_client, message['channel'])
try:
args = self.parser.parse_args(matches[0].split(' '))
except SlackArgumentParserException as err:
# lol commented out for max sass
# send(str(err))
return send(random_insult())

if args.help:
return send(self.help_text)

if not args.message:
return send(random_insult())

new_message = ' {} '.format(args.separator).join(args.message)

if args.at:
new_message = '{} {}'.format(args.at, new_message)

send(new_message)
23 changes: 23 additions & 0 deletions kizuna/Command.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import re

from io import StringIO

from functools import partial


class Command:
def __init__(self, name, pattern, help_text='', is_at=True) -> None:
Expand All @@ -12,13 +16,32 @@ def __init__(self, name, pattern, help_text='', is_at=True) -> None:
def help(self, bot_name):
return self.help_text.replace("{bot}", bot_name)

@staticmethod
def add_help_command(parser):
parser.add_argument('-h',
'--help',
action='store_true',
dest='help',
help='show this help message and exit')

def set_help_text(self, parser):
help_text_capture = StringIO()
parser.print_help(file=help_text_capture)
help_text = help_text_capture.getvalue()
help_text = help_text.replace('usage: ', '')
self.help_text = help_text

@staticmethod
def send(slack_client, channel, text):
return slack_client.api_call("chat.postMessage",
channel=channel,
text=text,
as_user=True)

@staticmethod
def send_factory(slack_client, channel):
return partial(Command.send, slack_client, channel)

def respond(self, slack_client, message, matches):
return None

Expand Down
10 changes: 10 additions & 0 deletions kizuna/SlackArgumentParser.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from argparse import ArgumentParser


class SlackArgumentParserException(Exception):
pass


class SlackArgumentParser(ArgumentParser):
def error(self, message):
raise SlackArgumentParserException(message)
10 changes: 10 additions & 0 deletions kizuna/strings.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,14 @@
import random

HAI_DOMO = '\u306F\u3044\u3069\u3082\uFF01\u30AD\u30BA\u30CA\u30FB\u30A2\u30A4\u3067\u3059\uFF01'
KIZUNA = '\u30AD\u30BA\u30CA'
WAIT_A_SEC = '\u3061\u3087\u3063\u3068\u5F85\u3063\u3066\u304F\u3060\u3055\u3044'
JAP_DOT = '\u3002'
AHO = '\u3042\u307B'
BAKA = '\u3070\u304B'
INSULTS = [AHO, BAKA]


def random_insult():
return random.choice(INSULTS)

0 comments on commit a2f3f64

Please sign in to comment.