Skip to content

Commit

Permalink
fix logic for sender and blacken files
Browse files Browse the repository at this point in the history
  • Loading branch information
itsthejoker committed Sep 23, 2022
1 parent fbd407e commit c433147
Show file tree
Hide file tree
Showing 20 changed files with 87 additions and 125 deletions.
7 changes: 2 additions & 5 deletions bubbles/commands/backup_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

from utonium import Payload, Plugin


def backup_db(payload: Payload) -> None:
"""!backup - creates and uploads a full backup of our postgres db."""
# the db info is injected into the bot environment, so we'll grab it
Expand Down Expand Up @@ -36,8 +37,4 @@ def backup_db(payload: Payload) -> None:
os.remove(previous_backups[0])


PLUGIN = Plugin(
func=backup_db,
regex=r"^backup",
interactive_friendly=False
)
PLUGIN = Plugin(func=backup_db, regex=r"^backup", interactive_friendly=False)
5 changes: 1 addition & 4 deletions bubbles/commands/cute.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,4 @@ def cute(payload: Payload) -> None:
payload.say(pic)


PLUGIN = Plugin(
func=cute,
regex=r"^cute"
)
PLUGIN = Plugin(func=cute, regex=r"^cute")
6 changes: 1 addition & 5 deletions bubbles/commands/deploy.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,8 +198,4 @@ def deploy(payload: Payload) -> None:
_deploy_service(service, payload)


PLUGIN = Plugin(
func=deploy,
regex=r"^deploy ?(.+)",
interactive_friendly=False
)
PLUGIN = Plugin(func=deploy, regex=r"^deploy ?(.+)", interactive_friendly=False)
5 changes: 1 addition & 4 deletions bubbles/commands/echo.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,4 @@ def echo(payload: Payload):
payload.say(f"```{' '.join(text.split()[1:])}```")


PLUGIN = Plugin(
func=echo,
regex=r"^echo"
)
PLUGIN = Plugin(func=echo, regex=r"^echo")
4 changes: 1 addition & 3 deletions bubbles/commands/fuckoff.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,4 @@ def fuck_off(payload: Payload) -> None:
payload.say(random.choice(responses))


PLUGIN = Plugin(
func=fuck_off, regex=pattern, flags=re.IGNORECASE, ignore_prefix=True
)
PLUGIN = Plugin(func=fuck_off, regex=pattern, flags=re.IGNORECASE, ignore_prefix=True)
6 changes: 1 addition & 5 deletions bubbles/commands/isup.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,4 @@ def _check(name):
_check(service)


PLUGIN = Plugin(
func=isup,
regex=r"^isup([ a-zA-Z]+)?",
interactive_friendly=False
)
PLUGIN = Plugin(func=isup, regex=r"^isup([ a-zA-Z]+)?", interactive_friendly=False)
6 changes: 1 addition & 5 deletions bubbles/commands/logs.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,4 @@ def logs(payload: Payload) -> None:
)


PLUGIN = Plugin(
func=logs,
regex=r"^logs([ a-zA-Z]+)?",
interactive_friendly=False
)
PLUGIN = Plugin(func=logs, regex=r"^logs([ a-zA-Z]+)?", interactive_friendly=False)
60 changes: 39 additions & 21 deletions bubbles/commands/periodic/modmail.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,30 +18,34 @@ def process_modmail(message_state: str) -> None:
# *.user: Redditor
# *.participant: Redditor
# *.participant_subreddit: dict
if convo.user != {}:
if convo.participant_subreddit != {}:
# this is the least likely, but if it should happen we definitely
# want to catch it
participant = reddit.subreddit(convo.participant_subreddit["name"])
elif convo.user != {}:
participant = convo.user
elif convo.participant != {}:
participant = convo.participant
elif convo.participant_subreddit != {}:
participant = reddit.subreddit(convo.participant_subreddit['name'])
else:
participant = "unknown participant"
participant = None

latest_message = convo.messages[-1]
if latest_message.author == participant and convo.num_messages == 1:
# this is a new message thread and someone sent something in.
if convo.num_messages == 1 and (
latest_message.author == participant or isinstance(participant, Subreddit)
):
# we were sent a message!
sender = participant
recipient = sub
elif latest_message.author == participant:
sender = participant
if len(convo.authors) == 1:
# they just sent another message to the same thread that they started
recipient = sub
else:
recipient = convo.messages[-2].author
else:
elif convo.num_messages == 1 and latest_message.author != participant:
sender = latest_message.author
recipient = participant
elif convo.num_messages > 1:
sender = latest_message.author
recipient = convo.messages[-2].author
else:
# realistically we shouldn't ever hit this, but it's a good safety
sender = "unknown sender"
recipient = "unknown recipient"
convo.read()

if isinstance(sender, Redditor):
Expand All @@ -53,21 +57,35 @@ def process_modmail(message_state: str) -> None:
recipient = f"u/{recipient.name}"
if isinstance(recipient, Subreddit):
recipient = f"r/{recipient.display_name}"

extra = (
" :banhammer_fancy:"
if convo.subject.startswith("You've been permanently banned")
else None
)
app.client.chat_postMessage(
channel=rooms_list["mod_messages"],
as_user=True,
blocks=[
blocks.SectionBlock(text=f"*{sender}* :arrow_right: *{recipient}*"),
blocks.SectionBlock(text=f"Subject: {convo.subject}"),
blocks.SectionBlock(text=f"*Subject*: {convo.subject}{extra}"),
blocks.DividerBlock(),
blocks.SectionBlock(text=latest_message.body_markdown),
blocks.DividerBlock(),
blocks.ContextBlock(elements=[
blocks.MarkdownTextObject(text=f"Conversation ID: {convo.id}"),
blocks.MarkdownTextObject(text=f"Message ID: {latest_message.id}")
])
]
blocks.SectionBlock(
text=":modmail: :link: :arrow_right:",
accessory=blocks.LinkButtonElement(
url=f"https://mod.reddit.com/mail/all/{convo.id}", text="Open"
),
),
blocks.ContextBlock(
elements=[
blocks.MarkdownTextObject(text=f"Conversation ID: {convo.id}"),
blocks.MarkdownTextObject(
text=f"Message ID: {latest_message.id}"
),
]
),
],
)


Expand Down
4 changes: 1 addition & 3 deletions bubbles/commands/plot_comments_history.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,5 @@ def plot_comments_history(payload: Payload) -> None:


PLUGIN = Plugin(
func=plot_comments_history,
regex=r"^history([0-9 ]+)?",
interactive_friendly=False
func=plot_comments_history, regex=r"^history([0-9 ]+)?", interactive_friendly=False
)
5 changes: 1 addition & 4 deletions bubbles/commands/plot_comments_historylist.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,4 @@ def plot_comments_historylist(payload: Payload) -> None:
payload.say(f"Volunteers welcomed by {key}: {list_volunteers_per_person[key]}")


PLUGIN = Plugin(
func=plot_comments_historylist,
regex=r"^listmodsTEST ([0-9 ]+)?"
)
PLUGIN = Plugin(func=plot_comments_historylist, regex=r"^listmodsTEST ([0-9 ]+)?")
5 changes: 1 addition & 4 deletions bubbles/commands/plot_comments_historywho.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,4 @@ def plot_comments_historywho(payload: Payload) -> None:
payload.upload_file(file="plotHourMods.png")


PLUGIN = Plugin(
func=plot_comments_historywho,
regex=r"^historywho([ \"a-zA-Z]+)?"
)
PLUGIN = Plugin(func=plot_comments_historywho, regex=r"^historywho([ \"a-zA-Z]+)?")
6 changes: 1 addition & 5 deletions bubbles/commands/restart.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,4 @@ def restart(payload: Payload):
_restart_service(service, payload.say)


PLUGIN = Plugin(
func=restart,
regex=r"^restart ?(.+)",
interactive_friendly=False
)
PLUGIN = Plugin(func=restart, regex=r"^restart ?(.+)", interactive_friendly=False)
6 changes: 1 addition & 5 deletions bubbles/commands/start.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,4 @@ def start(payload: Payload) -> None:
_start_service(service, payload.say)


PLUGIN = Plugin(
func=start,
regex=r"^start ?(.+)",
interactive_friendly=False
)
PLUGIN = Plugin(func=start, regex=r"^start ?(.+)", interactive_friendly=False)
6 changes: 1 addition & 5 deletions bubbles/commands/stop.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,4 @@ def stop(payload: Payload) -> None:
_stop_service(service, payload.say)


PLUGIN = Plugin(
func=stop,
regex=r"^stop ?(.+)",
interactive_friendly=False
)
PLUGIN = Plugin(func=stop, regex=r"^stop ?(.+)", interactive_friendly=False)
5 changes: 1 addition & 4 deletions bubbles/commands/suggest_upvote_filter_val.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,4 @@ def suggest_filter(payload: Payload) -> None:
)


PLUGIN = Plugin(
func=suggest_filter,
regex=SUGGEST_FILTER_RE
)
PLUGIN = Plugin(func=suggest_filter, regex=SUGGEST_FILTER_RE)
6 changes: 1 addition & 5 deletions bubbles/commands/update_and_restart.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,4 @@ def update(payload: Payload) -> None:
)


PLUGIN = Plugin(
func=update,
regex=r"^update$",
interactive_friendly=False
)
PLUGIN = Plugin(func=update, regex=r"^update$", interactive_friendly=False)
5 changes: 1 addition & 4 deletions bubbles/commands/vote.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,4 @@ def vote(payload: Payload) -> None:
payload.reaction_add(response, vote)


PLUGIN = Plugin(
func=vote,
regex=r"^vote([ \S]+)?|poll([ \S]+)?"
)
PLUGIN = Plugin(func=vote, regex=r"^vote([ \S]+)?|poll([ \S]+)?")
2 changes: 1 addition & 1 deletion bubbles/commands/yell.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,5 +52,5 @@ def yell_callback(payload: Payload) -> None:
regex=raw_pattern,
flags=re.IGNORECASE | re.MULTILINE | re.VERBOSE,
callback=yell_callback,
ignore_prefix=True
ignore_prefix=True,
)
55 changes: 26 additions & 29 deletions bubbles/interactive.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,29 +18,26 @@ def files_upload(self, *args, **kwargs) -> None:
def reactions_list(self, *args, **kwargs) -> dict:
"""Triggers short circuit condition in Payload.get_reaction_message()."""
return {
'items': [
"items": [
{
'type': 'message',
'channel': 'console',
'message': {
'client_msg_id': '3456c594-3024-404d-9e08-3eb4fe0924c0',
'type': 'message',
'text': 'This is pretending to be the message you reacted to!',
'user': 'console',
'ts': '1661965345.288219',
'team': 'GFEDCBA',
'blocks': [...],
'reactions': [
{
'name': 'upvote',
'users': ['console'], 'count': 1
}
"type": "message",
"channel": "console",
"message": {
"client_msg_id": "3456c594-3024-404d-9e08-3eb4fe0924c0",
"type": "message",
"text": "This is pretending to be the message you reacted to!",
"user": "console",
"ts": "1661965345.288219",
"team": "GFEDCBA",
"blocks": [...],
"reactions": [
{"name": "upvote", "users": ["console"], "count": 1}
],
'permalink': 'https://...'
}
"permalink": "https://...",
},
}
],
'ok': True
"ok": True,
}


Expand All @@ -67,35 +64,35 @@ def _base_payload(self) -> dict:

def build_message_payload(self, text) -> dict:
resp = self._base_payload()
resp |= {'type': 'message', 'text': text, 'item_user': 'console'}
resp |= {"type": "message", "text": text, "item_user": "console"}
return resp

def build_fake_slack_response(self, payload: dict, context=None) -> dict:
context = context or {}
return {
'payload': payload,
'client': MockClient(),
'context': context,
'say': self.say
"payload": payload,
"client": MockClient(),
"context": context,
"say": self.say,
}

def build_message(self, text: str) -> dict:
payload = self.build_message_payload(text)
resp = self.build_fake_slack_response(payload)
# messages expect the `body` argument from slack_bolt
resp |= {'body': None}
resp |= {"body": None}
return resp

def build_reaction(self, reaction: str) -> dict:
payload = self._base_payload()
payload |= {
'type': 'reaction_added',
'reaction': reaction,
'item_user': 'console',
"type": "reaction_added",
"reaction": reaction,
"item_user": "console",
"item": {
"type": "message",
"channel": "console",
"ts": str(random.randint(0, 9999) + random.random())
"ts": str(random.randint(0, 9999) + random.random()),
},
}
return self.build_fake_slack_response(payload)
Expand Down
8 changes: 4 additions & 4 deletions bubbles/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
COMMAND_PREFIXES,
DEFAULT_CHANNEL,
users_list,
rooms_list
rooms_list,
)
from bubbles.interactive import InteractiveSession, MockClient
from bubbles.reaction_added import reaction_added_callback
Expand Down Expand Up @@ -117,7 +117,7 @@ def main(ctx: Context, command: str, interactive: bool) -> None:
slack_app=app,
interactive_mode=interactive,
users_dict=users_list,
rooms_dict=rooms_list
rooms_dict=rooms_list,
)
plugin_manager.load_all_plugins()

Expand All @@ -131,7 +131,7 @@ def main(ctx: Context, command: str, interactive: bool) -> None:
"text": f"!{command}",
"channel": "console",
},
say=click.echo
say=click.echo,
)
)
return
Expand Down Expand Up @@ -181,7 +181,7 @@ def selfcheck(verbose: bool) -> None:
command_folder=command_folder_path,
slack_app=app,
users_dict=users_list,
rooms_dict=rooms_list
rooms_dict=rooms_list,
)
plugin_manager.load_all_plugins()
except Exception as e:
Expand Down

0 comments on commit c433147

Please sign in to comment.