You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123 lines
3.8 KiB
Python

"""sobelbot: techchat bot
TODO:
* add more commands
* config file
* setup.py
"""
import os
import random
import time
import yaml
from shlex import split
from pykeybase import KeybaseChat, KeybaseBot, KeybaseTeam
# Check env for conf
conf_file = os.getenv('SOBEL_CONF')
if not conf_file:
print('ERROR: ENV var SOBEL_CONF not found')
exit(1)
# Read in conf yaml file
with open(conf_file) as cfile:
conf = yaml.load(cfile)
# Team channels to monitor
channels = conf['channels']
kb = KeybaseChat()
bot = KeybaseBot(kb, channels, help_command=r'^\.help$', help_trigger='.help')
@bot.command(r'^\.member\sadd\s\w+', help_trigger='.member add <user>')
def add_member(message_data):
"""Add a member to the current team"""
try:
kbt = KeybaseTeam()
req = kbt.add_members(
message_data['team'],
[{
'username': message_data['body'].split()[-1].strip(),
'role': 'reader'
}]
)
except Exception as err:
resp = "error adding user"
if req.get('error'):
if 'admin' in req['error']['message']:
resp = 'Error: I do not have the proper rights'
elif 'already' in req['error']['message']:
resp = 'Error: user is already a member'
else:
resp = 'member added'
return bot.respond(resp, message_data, at_mention=True)
@bot.command(r'^\.member\sremove\s\w+', help_trigger='.member remove <user>')
def remove_member(message_data):
"""Remove a member from the current team"""
try:
kbt = KeybaseTeam()
req = kbt.remove_member(
message_data['team'],
message_data['body'].split()[-1].strip()
)
except Exception as err:
resp['error'] = "error removing user"
if req.get('error'):
if 'admin' in req['error']['message']:
resp = 'Error: I do not have the proper rights'
elif 'is not' in req['error']['message']:
resp = 'Error: user is not a member'
else:
resp = 'member removed'
return bot.respond(resp, message_data, at_mention=True)
@bot.command(r'\b(fuck|shit|ass|pussy|bitch)\b', show_help=False)
def swear_cmd(message_data):
"""Respond to swear words"""
response_text = "Please dont use that kind of language in here."
return bot.respond(response_text, message_data, at_mention=True)
@bot.command(r'^\.roll', help_trigger='.roll <dice> <sides>')
def roll_cmd(message_data):
'''Roll <dice> amount of <side>-sided dice. If <dice> and <sides> are not
provided, default is to roll 2 6-sided dice. If <dice> and <sides> are
provided, <dice> must be between 1 and 10, and <sides> must be between 2
and 100.'''
try:
num_of_dice = int(split(message_data['body'])[1])
num_of_sides = int(split(message_data['body'])[2]) + 1
except ValueError:
num_of_dice = 0
num_of_sides = 0
except IndexError:
num_of_dice = 2
num_of_sides = 7
if (num_of_dice not in range(1, 11) or
num_of_sides not in range(2, 102)):
response_text = '`<dice>` must be a number from 1 to 10, and ' \
'`<sides>` must be a number from 2 to 100'
return bot.respond(response_text, message_data, at_mention=True)
dice = []
for _ in range(num_of_dice):
dice.append(random.choice(range(1, num_of_sides)))
response_text = 'You rolled a '
if len(dice) > 1:
for n in dice[0:-1]:
response_text += '`{}`, '.format(n)
response_text += 'and `{}`, for a total of `{}`.'.format(dice[-1],
sum(dice))
else:
response_text += '`{}`.'.format(dice[0])
return bot.respond(response_text, message_data, at_mention=True)
try:
bot.run(respond=True)
except KeyboardInterrupt:
exit(0)