forked from Gnomeball/SwackQuote
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbot.py
338 lines (287 loc) · 12.1 KB
/
bot.py
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
"""
A polite little Discord bot that can send out a quote each day.
`bot.py` focuses on the Discord integration only.
"""
import asyncio
import colorsys
import contextlib
import logging
import random
import re
import string
import tomllib
from collections import Counter
from datetime import UTC, datetime
from operator import attrgetter, itemgetter
from pathlib import Path
from typing import NoReturn
import discord
with contextlib.suppress(ModuleNotFoundError):
from ada_url import URL
import logs
from quotes import (
QUOTE_DECK_PATH,
QUOTE_DUD_PATH,
QUOTE_FILE_PATH,
QUOTE_HISTORY_PATH,
calculate_swack_level,
format_quote_text,
pull_random_quote,
pull_specific_quote,
refresh_quotes,
)
LOCAL_DIR = Path(__file__).parent.resolve()
"Where this file and other files are placed"
REPO_LINK = "https://github.com/Gnomeball/SwackQuote"
"Where is this repo, in case anybody asks?"
HELP_DOC = """Commands:
```js
#repo - Prints out the URL for the GitHub repository
#authors - Prints a list of authors, with quote contribution counts
#help - Prints out this message
```
Admin commands:
```js
#test <ID> - Sends a test quote, ID optional
#reroll - Reroll todays quote
#colour <HEX> - Updates the lucky colour, HEX (RRGGBB) optional
```
How to add a Quote:
```js
1. Go to the GitHub repository (#repo)
2. Fork the repository
3. Add your quote to quotes.toml (please read the formatting guide)
4. Update the comments with the current count
5. Open a pull request
Each step has a full guide and can be done in browser, no downloads required.
```
"""
"What SwackQuote can be asked to do."
RE_IS_URL = re.compile(r"^https?://[^\s/$.?#].[^\s]*$", flags=re.IGNORECASE | re.MULTILINE | re.UNICODE)
"Pattern to check if a string is most likely a URL. Credit to @stephenhay."
MINUTE = 60
"How long is a minute?"
# Variables and stuff
intents = discord.Intents.default()
intents.message_content = True
client = discord.Client(intents=intents)
# Prepare the client and logging
logs.init()
logger = logging.getLogger("SwackQuote")
def is_url(url: str) -> bool:
"""
Check if a string is a valid URL for Discord.
Some invalid URLs may get through, but all valid URLs will pass.
Things that are absolutely not URLs will fail.
:returns: whether the given string is a valid URL.
:rtype: bool
"""
try:
_ = URL(url)
except (NameError, ValueError):
return re.match(RE_IS_URL, url) is not None
else:
return True
def random_colour() -> int:
"""
Calculates a random (ish) Hex colour - Used to make embeds a little less boring.
:returns: integer value for a colour.
:rtype: int
"""
colour = colorsys.hsv_to_rgb(random.random(), random.uniform(0.42, 0.98), random.uniform(0.4, 0.9))
colour_hex = f"0x{''.join(hex(int(x * 255))[2:].zfill(2) for x in colour)}"
return int(colour_hex, 16)
@client.event
async def on_ready() -> None:
"""SwackQuote has joined the server!"""
logging.info(f"We have logged in as {client.user}")
await client.change_presence(activity=discord.Game(name="Selecting a quote!"))
@client.event
async def on_message(message: discord.Message) -> None:
"""SwackQuote has been sent a message! Exciting!"""
logger = logging.getLogger("on_message")
if message.channel.id != CHANNEL:
return
match message.author.id, str.split(message.content):
case user, ["#reroll", *_] if user in ADMINS:
logger.info("Requesting quote re-roll")
await send_quote("Re-rolled Quote", log="request_quote")
case user, ["#test"] if user in ADMINS:
logger.info("Requesting test quote")
await test_quote()
case user, ["#test", which, *_] if user in ADMINS:
logger.info(f"Requesting test quote '{which}'")
await test_quote(which)
case user, ["#colour" | "#color"] if user in ADMINS and LUCKY_ROLE is not None:
await change_lucky_colour(silent_update=False)
case user, ["#colour" | "#color", colour, *_] if user in ADMINS and LUCKY_ROLE is not None:
await change_lucky_colour(colour, silent_update=False)
case _, ["#repo", *_]:
await client.get_channel(CHANNEL).send(content=REPO_LINK)
case _, ["#authors", *_]:
await author_counts()
case _, ["#help", *_]:
await send_help()
@client.event
async def send_help() -> None:
"""Prints out the help."""
embed_msg = discord.Embed(title="Sending help!", colour=random_colour(), description=HELP_DOC)
embed_msg.set_footer(text=f"Help for {await current_date_time()}")
await client.get_channel(CHANNEL).send(embed=embed_msg)
@client.event
async def author_counts() -> None:
"""Who has contributed quotes (as self-assessed by the submitter field)."""
quotes = await refresh_quotes() # This way we have access to the latest quotes
authors = dict(
sorted(Counter(map(attrgetter("submitter"), quotes.values())).items(), key=itemgetter(1), reverse=True)
)
pad_name = max(map(len, authors)) + 1
pad_num = max(map(len, map(str, authors.values()))) + 1
# in py312 we can use f"{f"{author} ".ljust(pad_name, ".")}.{f" {count}".rjust(pad_num, ".")}"
author_list = [
".".join([f"{author} ".ljust(pad_name, "."), f" {count}".rjust(pad_num, ".")])
for author, count in authors.items()
]
"Their name and number of submitted quotes, with our fun dot-based padding."
author_string = "\n".join(["```", *author_list, "```"])
"Formatted as a fenced block, so everything lines up nicely."
embed_msg = discord.Embed(title="Submitters", colour=random_colour(), description=author_string)
embed_msg.set_footer(text=f"Submitter table as of {await current_date_time()}")
await client.get_channel(CHANNEL).send(embed=embed_msg)
@client.event
async def dud_quotes() -> None:
"""For when things go less than correct, try to let us know."""
logger = logging.getLogger("dud_quotes")
# We just print verbatim, no need to parse
duds = QUOTE_DUD_PATH.read_text().strip()
if len(duds):
logger.info(f"Sending dud quotes: \n{duds}")
embed_msg = discord.Embed(
title="These quotes need fixing", description=f"```toml\n{duds[:3900]}\n```", colour=random_colour()
)
await client.get_channel(CHANNEL).send(embed=embed_msg)
logger.info("Dud quotes have been sent")
else:
logger.info("There were no dud quotes today")
@client.event
async def quote_loop() -> NoReturn:
"""Idles until we should start."""
await client.wait_until_ready()
logging.debug("Running quote loop")
while True:
previous = datetime.now(UTC)
await asyncio.sleep(MINUTE)
now = datetime.now(UTC)
if previous.hour != now.now().hour and now.hour == 12:
await asyncio.sleep(15)
await send_quote()
if LUCKY_ROLE is not None:
await asyncio.sleep(15)
await change_lucky_colour(silent_update=False)
@client.event
async def current_date_time() -> str:
"""Excuse me, could I bother you for the time?"""
day_n = datetime.now(UTC).day
day_ord = {1: "st", 2: "nd", 3: "rd", 7: "nth", 17: "nth", 21: "st", 22: "nd", 23: "rd", 27: "nth", 31: "st"}.get(
day_n, "th"
)
return datetime.now(UTC).strftime("%A %d# %B %Y").replace("#", day_ord)
@client.event
async def change_lucky_colour(pick: str | None = None, *, silent_update: bool = False) -> None:
"""Change today's colour for the "I'm feeling lucky" role."""
if LUCKY_ROLE is None:
logger.info("Attempted to change lucky colour, but we have no lucky role.")
else:
role: discord.Role = client.get_channel(CHANNEL).guild.get_role(LUCKY_ROLE)
colour = None
logger.info("Time for a new lucky colour!")
match pick:
case str(colour_str) if len(colour_str) > 1:
logger.info(f"Someone wanted {colour_str} to be the lucky colour.")
# Common prefixes
if colour_str.startswith("#"):
colour_str = colour_str.removeprefix("#")
elif colour_str.startswith("0x"):
colour_str = colour_str.removeprefix("0x")
# They might use _ separators
if "_" in colour_str:
colour_str = colour_str.replace("_", "")
# We only accept hex, for now
if set(colour_str).issubset(string.hexdigits):
colour = int(colour_str, 16)
else:
logger.info("I'm going to ignore that colour as it's not hex.")
case _:
colour = random_colour()
match colour:
case int(colour) if 0 <= colour <= 0xFF_FF_FF:
logger.info(f"Attempting to set the lucky colour to {hex(colour)}")
try:
await role.edit(colour=colour)
logger.info("Quote sent successfully")
if not silent_update:
await client.get_channel(CHANNEL).send(
embed=(
discord.Embed(
title="Today's lucky colour is...",
colour=colour,
description=f"**#{hex(colour).removeprefix('0x').upper()}**",
)
)
)
except Exception:
logger.exception("Error setting the lucky colour or sending the update message")
case None:
logger.info("Colour was not set")
case _:
logger.info(f"Malformed colour: {colour}")
@client.event
async def send_quote(
pre: str = "Quote", title: str | None = None, which: str | None = None, log: str = "send_quote"
) -> None:
"""SwackQuote deployed. Quote inbound."""
logger = logging.getLogger(log)
quotes = await refresh_quotes() # This way we have access to the latest quotes
await dud_quotes()
quote, i = pull_random_quote(quotes) if which is None else pull_specific_quote(which, quotes)
title = title or calculate_swack_level()
quote_text = format_quote_text(quote)
# Build the quote embed we will send
embed_msg = discord.Embed(title=title, colour=random_colour(), description=quote_text)
embed_msg.set_footer(
text=f"""{pre} for {await current_date_time()}
Quote {i}/{len(quotes)}, Submitted by {quote.submitter}"""
)
if quote.source and is_url(quote.source):
embed_msg.url = quote.source
embed_msg.title += " 🔗"
# Try and send the quote
logger.info(f"Attempting to send quote #{i}, submitted by {quote.submitter}")
try:
await client.get_channel(CHANNEL).send(embed=embed_msg)
if quote.embed and quote.source and is_url(quote.source):
await client.get_channel(CHANNEL).send(content=quote.source)
logger.info("Quote sent successfully")
except Exception:
logger.exception(f"Error sending quote #{i}")
@client.event
async def test_quote(which: str = "<testing>", log: str = "test_quote") -> None:
"""Send our default testing quote, or another one of your choice, marked up so we can tell."""
await send_quote(pre="Testing", title="Testing the Swack", which=which, log=log)
if __name__ == "__main__":
# Permissions and directions for SwackQuote
ADMINS = set(tomllib.loads((LOCAL_DIR / "admins.toml").read_text()).values())
"Those able to send commands to SwackQuote."
CHANNEL = int((LOCAL_DIR / "channel.txt").read_text())
"Which channel SwackQuote will move to, place quotes in, and monitor for commands."
LUCKY_ROLE = (
int((LOCAL_DIR / "lucky_role.txt").read_text()) if (LOCAL_DIR / "lucky_role.txt").is_file() else None
) or None
"Which role SwackQuote will change the daily colour of (optional, will do nothing if not present)."
# Ensure necessary files exist
QUOTE_FILE_PATH.touch()
QUOTE_DUD_PATH.touch()
QUOTE_DECK_PATH.touch()
QUOTE_HISTORY_PATH.touch()
client.loop.create_task(quote_loop())
client.run((LOCAL_DIR / "token.txt").read_text())