1
|
|
|
""" |
2
|
|
|
byceps.announce.discord.board |
3
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
4
|
|
|
|
5
|
|
|
Announce board events on Discord via its webhooks API. |
6
|
|
|
|
7
|
|
|
:Copyright: 2006-2020 Jochen Kupperschmidt |
8
|
|
|
:License: Modified BSD, see LICENSE for details. |
9
|
|
|
""" |
10
|
|
|
|
11
|
1 |
|
from typing import Optional |
12
|
|
|
|
13
|
1 |
|
from flask import current_app |
14
|
1 |
|
import requests |
15
|
|
|
|
16
|
1 |
|
from ...events.board import BoardPostingCreated, BoardTopicCreated |
17
|
1 |
|
from ...services.brand import settings_service as brand_settings_service |
18
|
1 |
|
from ...signals import board as board_signals |
19
|
1 |
|
from ...util.jobqueue import enqueue |
20
|
|
|
|
21
|
1 |
|
from ..helpers import get_screen_name_or_fallback |
22
|
|
|
|
23
|
|
|
|
24
|
|
|
# This is a pretty basic implementation that only supports a single |
25
|
|
|
# webhook and that fetches its configuration from a brand's setting. |
26
|
|
|
# For now, some customization is necessary to post announcements |
27
|
|
|
# (especially for different brands, or even different parties) to |
28
|
|
|
# different webhooks (i.e. effectively Discord channels). |
29
|
|
|
|
30
|
|
|
|
31
|
1 |
|
BRAND_ID = 'YOUR-BRAND-HERE' |
32
|
|
|
|
33
|
|
|
|
34
|
1 |
|
def send_message(text: str) -> None: |
35
|
|
|
"""Send text to the webhook API. |
36
|
|
|
|
37
|
|
|
The endpoint URL already includes the target channel. |
38
|
|
|
""" |
39
|
1 |
|
if not _is_enabled(): |
40
|
1 |
|
current_app.logger.warning('Announcements on Discord are disabled.') |
41
|
1 |
|
return |
42
|
|
|
|
43
|
1 |
|
url = _get_webhook_url() |
44
|
1 |
|
if not url: |
45
|
|
|
current_app.logger.warning( |
46
|
|
|
'No webhook URL configured for announcements on Discord.' |
47
|
|
|
) |
48
|
|
|
return |
49
|
|
|
|
50
|
1 |
|
text_prefix = _get_text_prefix() |
51
|
1 |
|
if text_prefix: |
52
|
1 |
|
text = text_prefix + text |
53
|
|
|
|
54
|
1 |
|
data = {'content': text} |
55
|
|
|
|
56
|
1 |
|
requests.post(url, json=data) # Ignore response code for now. |
57
|
|
|
|
58
|
|
|
|
59
|
1 |
|
def _is_enabled() -> bool: |
60
|
|
|
"""Return `true' if announcements on Discord are enabled.""" |
61
|
1 |
|
value = brand_settings_service.find_setting_value( |
62
|
|
|
BRAND_ID, 'announce_discord_enabled' |
63
|
|
|
) |
64
|
1 |
|
return value == 'true' |
65
|
|
|
|
66
|
|
|
|
67
|
1 |
|
def _get_webhook_url() -> str: |
68
|
|
|
"""Return the configured webhook URL.""" |
69
|
1 |
|
return brand_settings_service.find_setting_value( |
70
|
|
|
BRAND_ID, 'announce_discord_webhook_url' |
71
|
|
|
) |
72
|
|
|
|
73
|
|
|
|
74
|
1 |
|
def _get_text_prefix() -> Optional[str]: |
75
|
|
|
"""Return the configured text prefix.""" |
76
|
1 |
|
return brand_settings_service.find_setting_value( |
77
|
|
|
BRAND_ID, 'announce_discord_text_prefix' |
78
|
|
|
) |
79
|
|
|
|
80
|
|
|
|
81
|
|
|
# board events |
82
|
|
|
|
83
|
|
|
|
84
|
|
|
# Note: URLs are wrapped in `<…>` because that prevents |
85
|
|
|
# preview embedding on Discord. |
86
|
|
|
|
87
|
|
|
|
88
|
1 |
|
@board_signals.topic_created.connect |
89
|
1 |
|
def _on_board_topic_created(sender, *, event: BoardTopicCreated = None) -> None: |
90
|
1 |
|
enqueue(announce_board_topic_created, event) |
91
|
|
|
|
92
|
|
|
|
93
|
1 |
|
def announce_board_topic_created(event: BoardTopicCreated) -> None: |
94
|
|
|
"""Announce that someone has created a board topic.""" |
95
|
1 |
|
topic_creator_screen_name = get_screen_name_or_fallback( |
96
|
|
|
event.topic_creator_screen_name |
97
|
|
|
) |
98
|
|
|
|
99
|
1 |
|
text = ( |
100
|
|
|
f'{topic_creator_screen_name} hat das Thema ' |
101
|
|
|
f'"{event.topic_title}" erstellt: <{event.url}>' |
102
|
|
|
) |
103
|
|
|
|
104
|
1 |
|
send_message(text) |
105
|
|
|
|
106
|
|
|
|
107
|
1 |
|
@board_signals.posting_created.connect |
108
|
1 |
|
def _on_board_posting_created( |
109
|
|
|
sender, *, event: BoardPostingCreated = None |
110
|
|
|
) -> None: |
111
|
1 |
|
enqueue(announce_board_posting_created, event) |
112
|
|
|
|
113
|
|
|
|
114
|
1 |
|
def announce_board_posting_created(event: BoardPostingCreated) -> None: |
115
|
|
|
"""Announce that someone has created a board posting.""" |
116
|
1 |
|
if event.topic_muted: |
117
|
1 |
|
return |
118
|
|
|
|
119
|
1 |
|
posting_creator_screen_name = get_screen_name_or_fallback( |
120
|
|
|
event.posting_creator_screen_name |
121
|
|
|
) |
122
|
|
|
|
123
|
1 |
|
text = ( |
124
|
|
|
f'{posting_creator_screen_name} hat auf das Thema ' |
125
|
|
|
f'"{event.topic_title}" geantwortet: <{event.url}>' |
126
|
|
|
) |
127
|
|
|
|
128
|
|
|
send_message(text) |
129
|
|
|
|