Repository navigation
Expand file tree
/
Copy pathbot.py
More file actions
278 lines (242 loc) · 12 KB
/
Copy pathbot.py
File metadata and controls
278 lines (242 loc) · 12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
"""LooscidBOT holidays: posts a short celebration in each language's own channels
on that language's holidays, and faith holidays in each faith channel (no pings).
Runs once a day from GitHub Actions.
Settings come from environment variables:
- DISCORD_TOKEN: the bot token (a GitHub Actions secret, never in the code).
- DRY_RUN: "true" prints what would be posted and posts nothing. Leaves no servers either.
- PRETEND_DATE: YYYY-MM-DD, only honoured in a dry run, to test a given day.
"""
import json
import os
import sys
import time
import unicodedata
from datetime import date, datetime, timezone
import requests
from holidays_rules import HOLIDAYS, holidays_on, upcoming
from faith_holidays import FAITHS, faith_holidays_on, faith_upcoming, message as faith_message
# LooscidBOT is private to the Looscid server. The code is public, the bot is not.
LOOSCID_GUILD_ID = "1500332794327466076"
API = "https://discord.com/api/v10"
# Also post in the text chat of the language's voice channel.
ALSO_POST_IN_VOICE = True
# Every language's channel names live in channels.json (no channel ids are stored).
with open(os.path.join(os.path.dirname(os.path.abspath(__file__)), "channels.json"), encoding="utf-8") as f:
CHANNEL_LABELS = json.load(f)["languages"]
TEXT, VOICE, CATEGORY, ANNOUNCEMENT = 0, 2, 4, 5
VIEW_CHANNEL, SEND_MESSAGES, READ_HISTORY, ADMINISTRATOR = 1 << 10, 1 << 11, 1 << 16, 1 << 3
def norm(text):
"""Lowercase, no emoji or symbols, so "🇪🇸 Español" still matches "Español"."""
text = unicodedata.normalize("NFC", text).casefold()
return "".join(ch for ch in text if unicodedata.category(ch)[0] in "LMN").strip()
class Discord:
def __init__(self, token):
self.session = requests.Session()
self.session.headers.update({
"Authorization": "Bot " + token,
"User-Agent": "LooscidBOT-holidays (https://github.com, 1.0)",
})
def call(self, method, path, **kwargs):
for attempt in range(6):
r = self.session.request(method, API + path, timeout=30, **kwargs)
if r.status_code == 429:
time.sleep(float(r.json().get("retry_after", 2)) + 0.5)
continue
if r.status_code >= 500:
time.sleep(2 + attempt * 2)
continue
return r
return r
def effective_permissions(channel, roles, my_role_ids, my_id, guild):
"""Discord's permission math for one channel, so a dry run can say if posting would work."""
if guild.get("owner_id") == my_id:
return -1
everyone = roles[LOOSCID_GUILD_ID]
perms = int(everyone["permissions"])
for rid in my_role_ids:
perms |= int(roles[rid]["permissions"])
if perms & ADMINISTRATOR:
return -1
overwrites = {o["id"]: o for o in channel.get("permission_overwrites", [])}
if LOOSCID_GUILD_ID in overwrites:
o = overwrites[LOOSCID_GUILD_ID]
perms = (perms & ~int(o["deny"])) | int(o["allow"])
allow = deny = 0
for rid in my_role_ids:
if rid in overwrites:
allow |= int(overwrites[rid]["allow"])
deny |= int(overwrites[rid]["deny"])
perms = (perms & ~deny) | allow
if my_id in overwrites:
o = overwrites[my_id]
perms = (perms & ~int(o["deny"])) | int(o["allow"])
return perms
def can_post(perms):
return perms == -1 or (perms & VIEW_CHANNEL and perms & SEND_MESSAGES)
def find_channels(channels):
"""For each language: its general text channel and its voice channel, found by name."""
categories = [c for c in channels if c["type"] == CATEGORY]
found = {}
for language, labels in CHANNEL_LABELS.items():
wanted = {
"general": (TEXT, norm(labels["channels"]["general"])),
"voice": (VOICE, norm(labels["channels"]["voice"])),
}
result = {"general": None, "voice": None}
for cat_name in labels["categories"]:
for cat in (c for c in categories if norm(c["name"]) == norm(cat_name)):
inside = sorted((c for c in channels if c.get("parent_id") == cat["id"]), key=lambda c: c["position"])
for kind, (ctype, name) in wanted.items():
if result[kind] is None:
match = [c for c in inside if c["type"] == ctype and norm(c["name"]) == name]
if match:
result[kind] = match[0]
found[language] = result
return found
def find_faith_channels(channels):
"""{(faith, language): channel} for every faith channel, found by name inside its language category."""
categories = [c for c in channels if c["type"] == CATEGORY]
found = {}
for language, info in FAITHS["languages"].items():
cats = [c for c in categories if norm(c["name"]) == norm(info["category"])]
inside = [c for c in channels if c["type"] == TEXT and any(c.get("parent_id") == cat["id"] for cat in cats)]
for faith, name in info["channels"].items():
match = [c for c in inside if norm(c["name"]) == norm(name)]
found[(faith, language)] = match[0] if match else None
return found
def already_posted_today(api, channel_id, my_id, text, today):
r = api.call("GET", f"/channels/{channel_id}/messages", params={"limit": 25})
if r.status_code != 200:
return False
for m in r.json():
if m["author"]["id"] == my_id and m["content"] == text and m["timestamp"][:10] == today.isoformat():
return True
return False
def main():
dry_run = os.environ.get("DRY_RUN", "true").strip().lower() != "false"
token = os.environ.get("DISCORD_TOKEN", "").strip()
today = datetime.now(timezone.utc).date()
pretend = os.environ.get("PRETEND_DATE", "").strip()
if pretend and dry_run:
today = date.fromisoformat(pretend)
print(f"LooscidBOT holidays. Date (UTC): {today}. Mode: {'DRY RUN, nothing will be posted' if dry_run else 'LIVE'}.")
if not token:
print("No DISCORD_TOKEN set, so only the holiday calendar is shown.")
api = None
else:
api = Discord(token)
# 1. Private to Looscid: leave any other server.
if api:
me = api.call("GET", "/users/@me").json()
my_id = me["id"]
print(f"Signed in as {me['username']} ({my_id}).")
guilds = api.call("GET", "/users/@me/guilds").json()
for g in guilds:
if g["id"] != LOOSCID_GUILD_ID:
if dry_run:
print(f"Would leave server '{g['name']}' ({g['id']}): LooscidBOT is only for Looscid.")
else:
r = api.call("DELETE", f"/users/@me/guilds/{g['id']}")
print(f"Left server '{g['name']}' ({g['id']}): {r.status_code}.")
if not any(g["id"] == LOOSCID_GUILD_ID for g in guilds):
print("LooscidBOT is not in the Looscid server. Stopping.")
return 1
guild = api.call("GET", f"/guilds/{LOOSCID_GUILD_ID}").json()
channels = api.call("GET", f"/guilds/{LOOSCID_GUILD_ID}/channels").json()
roles = {r["id"]: r for r in api.call("GET", f"/guilds/{LOOSCID_GUILD_ID}/roles").json()}
member = api.call("GET", f"/guilds/{LOOSCID_GUILD_ID}/members/{my_id}").json()
my_roles = member.get("roles", [])
# 2. Find each language's channels by name (no hard-coded channel ids).
found = find_channels(channels)
print("\nChannels found:")
problems = 0
for language, ch in found.items():
parts = []
for kind in ("general", "voice"):
c = ch[kind]
if c is None:
parts.append(f"{kind}: NOT FOUND")
if kind == "general":
problems += 1
continue
ok = can_post(effective_permissions(c, roles, my_roles, my_id, guild))
if not ok and kind == "general":
problems += 1
parts.append(f"{kind}: #{c['name']} ({c['id']}){'' if ok else ' - bot CANNOT post here'}")
print(f"- {language}: " + "; ".join(parts))
print(f"General channels found: {sum(1 for c in found.values() if c['general'])} of {len(found)}. Problems: {problems}.")
else:
found = {}
# 3. Today's holidays, posted only in their own language.
todays = holidays_on(today)
print(f"\nToday ({today}):" + ("" if todays else " no holidays."))
for language, items in todays.items():
text = "\n".join(message for _name, message in items)
names = ", ".join(name for name, _m in items)
targets = []
ch = found.get(language, {})
if ch.get("general"):
targets.append(ch["general"])
if ALSO_POST_IN_VOICE and ch.get("voice"):
targets.append(ch["voice"])
print(f"- {language}: {names}\n Message: {text}")
if not api:
continue
if not targets:
print(" No channel found for this language, skipped.")
for c in targets:
if c.get("guild_id", LOOSCID_GUILD_ID) != LOOSCID_GUILD_ID:
print(f" Refusing #{c['name']}: not in Looscid.")
continue
if dry_run:
print(f" Would post in #{c['name']}.")
continue
if already_posted_today(api, c["id"], my_id, text, today):
print(f" Already posted in #{c['name']} today, skipped.")
continue
r = api.call("POST", f"/channels/{c['id']}/messages", json={"content": text, "allowed_mentions": {"parse": []}})
print(f" Posted in #{c['name']}: {r.status_code}{'' if r.ok else ' ' + r.text[:200]}")
# 4. What's coming up.
print("\nNext 60 days:")
for d, language, name in upcoming(today, 60):
print(f"- {d.isoformat()} {d.strftime('%A')}: {language}, {name}")
# 5. Faith holidays: only in that faith's own channel in each language, never a ping.
# (These channels are only visible to people who picked that language AND that faith.)
faith_found = find_faith_channels(channels) if api else {}
if api:
have = sum(1 for c in faith_found.values() if c)
cant = sum(1 for c in faith_found.values() if c and not can_post(effective_permissions(c, roles, my_roles, my_id, guild)))
print(f"\nFaith channels found: {have} of {len(faith_found)}. Bot cannot post in: {cant}.")
for key, c in faith_found.items():
if c is None:
print(f"- NOT FOUND: {key[0]} channel in {key[1]}")
todays_faith = faith_holidays_on(today)
print(f"\nFaith holidays today ({today}):" + ("" if todays_faith else " none."))
for faith, items in todays_faith.items():
print(f"- {faith}: " + ", ".join(h["names"]["English"] for h in items))
for language in FAITHS["languages"]:
text = "\n".join(faith_message(language, h) for h in items)
c = faith_found.get((faith, language))
if not api:
print(f" {language}: {text}")
continue
if c is None:
print(f" {language}: no channel found, skipped.")
continue
if c.get("guild_id", LOOSCID_GUILD_ID) != LOOSCID_GUILD_ID:
print(f" Refusing #{c['name']}: not in Looscid.")
continue
if dry_run:
print(f" Would post in #{c['name']} ({language}): {text}")
continue
if already_posted_today(api, c["id"], my_id, text, today):
print(f" Already posted in #{c['name']} today, skipped.")
continue
r = api.call("POST", f"/channels/{c['id']}/messages", json={"content": text, "allowed_mentions": {"parse": []}})
print(f" Posted in #{c['name']} ({language}): {r.status_code}{'' if r.ok else ' ' + r.text[:200]}")
print("\nFaith holidays, next 60 days (each goes to that faith's channel in all 13 languages):")
for d, faith, name in faith_upcoming(today, 60):
print(f"- {d.isoformat()} {d.strftime('%A')}: {faith}, {name}")
return 0
if __name__ == "__main__":
sys.exit(main())