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

126 lines
4.2 KiB

import os
import re
import json
import slack
import random
EXAMPLE_COMMAND = "do"
MENTION_REGEX = "(!mojo)(.*)"
BOT_CHANNEL = "bots-like-gaston"
def parse_direct_mention(message_text):
"""
Finds a direct mention (a mention that is at the beginning) in message text
and returns the user ID which was mentioned. If there is no direct mention,
returns None
"""
matches = re.search(MENTION_REGEX, message_text)
# the first group contains the username, the second group contains the remaining message
return (matches.group(1), matches.group(2).strip()) if matches else (None, None)
def zalgo_ify(text):
''' Takes some normal text and zalgo-ifies it '''
# "Combining Diacritical Marks" Unicode block.
combining_chars = [chr(n) for n in range(768, 878)]
zalgo_text = ''
for char in text:
combining_char = random.choice(combining_chars)
zalgo_text += f'{char}{combining_char}'
return zalgo_text
def reactable_string(text):
"""
Return regex objects matching interesting strings
"""
reactable_array = []
if re.search(r"\bai\b", text.lower()) is not None:
reactable_array.append('ai')
if 'furry' in text.lower() or 'furries' in text.lower() or 'fursuit' in text.lower():
# no processing needed because:
# honestly its funnier even if it gets the word boundary wrong.
reactable_array.append('furry')
if 'flavor town' in text.lower() or 'flavortown' in text.lower() or 'guy fieri' in text.lower():
# no processing needed because:
# honestly its funnier even if it gets the word boundary wrong.
reactable_array.append('flavortown')
return reactable_array
def react_to_message(reaction, payload, channel_id, thread_ts):
payload['web_client'].reactions_add(
channel=channel_id,
name=reaction,
timestamp=thread_ts
)
return None
@slack.RTMClient.run_on(event='message')
def handle_messages(**payload):
"""
Does a lot. Fucking decorators.
1. if the message has react-able strings, react to the message appropriately
2. if the message starts with @botname and has a command i've planned for, do thing.
"""
data = payload['data']
channel_id = data['channel']
thread_ts = data['ts']
if reactable_string(data['text']):
reactions_needed = reactable_string(data['text'])
if 'ai' in reactions_needed:
react_to_message("robot_face", payload, channel_id, thread_ts)
if 'furry' in reactions_needed:
react_to_message("eggplant", payload, channel_id, thread_ts)
react_to_message("sweat_drops", payload, channel_id, thread_ts)
if 'flavortown' in reactions_needed:
react_to_message("dark_sunglasses", payload, channel_id, thread_ts)
react_to_message("guyfieri", payload, channel_id, thread_ts)
is_command = parse_direct_mention(data['text'])
if is_command != (None, None):
if is_command[1].startswith("say"):
response = f"{is_command[1]}"
elif is_command[1].startswith("furry"):
response = f"{is_command[1]}"
elif is_command[1].startswith("download"):
response = "you wouldn't download a car"
elif "arke" in is_command[1]:
response = []
with open("/shared/state.json", "r") as json_File:
state = json.load(json_File)
for key, value in state.items():
response.append(f"{key}: {value}")
elif "zalgo" in is_command[1]:
# remove zalgo
name_removal = re.sub("^zalgo .*?", "", is_command[1])
response = zalgo_ify(name_removal)
elif "pence" in is_command[1]:
response = "mother wouldn't want me to say that"
else:
response = "that's not my job, bithc"
webclient = payload['web_client']
webclient.chat_postMessage(
channel=channel_id,
text=response,
timestamp=thread_ts
)
if __name__ == '__main__':
SLACK_TOKEN = os.environ["SLACK_BOT_TOKEN"]
CLIENT = slack.WebClient(token=os.environ['SLACK_BOT_TOKEN'])
RTM_CLIENT = slack.RTMClient(token=SLACK_TOKEN)
RTM_CLIENT.start()