This repository has been archived on 2024-05-20. You can view files and clone it, but cannot push or open issues or pull requests.
PriceyBot3/slackbot.py

150 lines
4.6 KiB
Python

import logging
import asyncio
import aiomysql
import random
from os import environ
from pprint import pprint
from slack_bolt.async_app import AsyncApp
import libs.block_utils as block_utils
import libs.benv
libs.benv.load_env()
logging.basicConfig(level=logging.DEBUG)
# Dataset for automatic reactions, the lower the chance the more often it triggers
auto_react_lookup = {
"U02GTCPB2CB": {"text": "glass_of_milk", "chance": -1}, # agoodlet
"U05ES1730UE": {"text": "clown_face", "chance": 3}, # mprice
"U05FN1F9ZCG": {"text": "frannodders", "chance": 4}, # blove
"U05EY6DQR9R": {"text": "kekw", "chance": 4}, # jfox
}
app = AsyncApp(token=environ.get("SLACK_BOT_TOKEN"))
messages_already_reacted = {}
@app.event("reaction_added")
async def reaction_add(body, logger, ack, say, client):
await ack()
event = body.get("event")
user = event.get("item_user")
channel_id = event.get("channel")
reaction = event.get("reaction")
if (
user == "U05ES1730UE" and event["reaction"] == "clown_face"
): # If pricey gets clown reacted
reactions = await client.reactions_get(
channel=event["item"]["channel"], timestamp=event["item"]["ts"]
)
for reaction in reactions.data["message"]["reactions"]:
if reaction["name"] == "clown_face":
if (
int(reaction["count"]) >= 3
and messages_already_reacted.get(event["item"]["ts"], False)
is False
):
messages_already_reacted[event["item"]["ts"]] = True
await say(
f"Pricey has been clown reacted {reaction['count']} times on a recent post"
)
@app.event("reaction_removed")
async def reaction_del(body, logger, ack):
await ack()
@app.message(":glass_of_milk:")
async def milk(body, ack, say, logger):
await say(":glass_of_milk:")
@app.event(event="message")
async def handle_message_events(body, say, ack, logger, client, message, response):
# logger.info(body)
pprint(body)
event = body.get("event", {})
channel_id = event.get("channel")
user_id = event.get("user")
# Check user is the milk man and then add a milk react
if user_id in auto_react_lookup.keys():
rand_num = random.randrange(0, 6)
print(rand_num)
if rand_num > auto_react_lookup[user_id]["chance"]:
await client.reactions_add(
channel=channel_id,
name=auto_react_lookup[user_id]["text"],
timestamp=message["ts"],
)
text = event.get("text")
if text == "x":
await ack()
pprint(environ)
if text == "!milk":
await ack()
members = await client.conversations_members(channel=channel_id)
members = members["members"]
member_data = await client.users_info(user=members[0])
await client.reactions_add(
channel=channel_id, name="glass_of_milk", timestamp=message["ts"]
)
# pprint(member_data.data)
# await say("test")
if text == "vader":
await ack()
blocks = await block_utils.image_block(
"https://benjamyn.love/vader.gif", "I fixed it mother bitch"
)
await say(blocks=blocks)
if text == "test image action":
await ack()
blocks = await block_utils.image_block(
"https://benjamyn.love/topright.png", "Top right buddy"
)
await say(blocks=blocks)
if text == "gimmie":
pool = await aiomysql.create_pool(
host=environ.get("MYSQL_HOST"),
port=int(environ.get("MYSQL_PORT")),
user=environ.get("MYSQL_USER"),
password=environ.get("MYSQL_PASS"),
db=environ.get("MYSQL_DB"),
loop=asyncio.get_event_loop(),
)
async with pool.acquire() as conn:
async with conn.cursor() as cur:
await cur.execute(
"select quote from tblQuotes order by rand() limit 1;"
)
(r,) = await cur.fetchone()
await say(text=r.decode())
pool.close()
await pool.wait_closed()
await ack()
@app.command(command="/socialcredit")
async def handle_social_credit(body, client, say, logger, ack):
await ack()
user_id = body.get("user_id")
await say(f"<@{user_id}> has so many social credits, long live poo bear")
@app.command(command="/test")
async def handle_test_command(body, say, ack, respond, context, logger):
logger.info(body)
await ack()
await say("Responding to slash commands")
if __name__ == "__main__":
app.start(5000)