-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.py
148 lines (121 loc) · 4.82 KB
/
app.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
from discord import Bot, ApplicationContext, Message, Embed, Colour, Intents, Message, default_permissions, guild_only
from discord.commands import Option
from random import choice
from yarl import URL
import re
from search import Searcher
from config import config, DEBUG, GOOGLE_SEARCH_API_KEYS
from logger import get_logger
intents = Intents.all()
bot = Bot(intents=intents)
searcher = Searcher(
api_keys=GOOGLE_SEARCH_API_KEYS,
engine_id=config("search.engine_id")
)
logger = get_logger(config("logs_dir"), DEBUG)
async def get_search_embed(query: str, page: int=1):
search_results = await searcher.search(
query=f"site:{config("site")} {query}",
offset=(page-1)*10,
lang=config("search.lang"),
timeout=config("search.timeout")
)
if not search_results:
return None
results = []
for title, link in search_results:
path = URL(link).path
prefixes = {
"Обсуждение: ": path.startswith("/forum/") or "/comments/show" in path,
"Черновик: ": path.startswith("/draft:"),
"Полигон: ": path.startswith("/sandbox:"),
"Удалено: ": path.startswith("/deleted:"),
"Пользователь: ": path.startswith("/-/users/"),
}
result = f"[{title}]({link})"
for prefix, condition in prefixes.items():
if condition:
result = f"{prefix}{result}"
break
results.append(result)
embed = Embed(
title=config("search.report.title"),
description="\n".join(results),
color=Colour(config("search.report.embed_color"))
)
return embed
@bot.event
async def on_connect():
logger.info(f"Запускаю {config("name")} v{config("version")}")
await bot.sync_commands()
await searcher.start_client()
@bot.event
async def on_disconnect():
logger.info(f"Соединение с Discord утеряно")
@bot.event
async def on_resumed():
logger.info(f"Соединение с Discord восcтановлено")
@bot.event
async def on_ready():
logger.info(f"{config("name")} v{config("version")} готов к работе")
@bot.listen("on_message")
async def handle_text_search(message: Message):
if message.author == bot.user:
return
if message.content.startswith("? "):
query = message.content[2:]
logger.info(f"Выполняю ? команду поиска по фразе \"{query}\" от пользователя {message.author}")
embed = await get_search_embed(query)
if embed:
await message.reply(embed=embed, mention_author=False)
else:
error_message = config("search.report.error_message")
await message.reply(error_message, mention_author=False)
@bot.listen("on_message")
async def handle_gratitude(message: Message):
if message.author == bot.user:
return
if re.search(config("gratitudes.match"), message.content):
await message.add_reaction(choice(config("gratitudes.reactions")))
@bot.slash_command(
name="search",
description=config("commands.search.description.default"),
description_localizations=config("commands.search.description.localizations")
)
async def cmd_search(
ctx: ApplicationContext,
query = Option(
str,
description=config("commands.search.arguments.query.description.default"),
description_localizations=config("commands.search.arguments.query.description.localizations"),
required=True),
page = Option(
int,
default=1,
min_value=1,
max_value=10,
required=False)
):
logger.info(f"Выполняю слеш-команду поиска по фразе \"{query}\" от пользователя {ctx.author}")
embed = await get_search_embed(query, page)
if embed:
await ctx.respond(embed=embed)
else:
error_message = config("search.report.error_message")
await ctx.respond(error_message)
@bot.slash_command(name="fox")
@guild_only()
@default_permissions(manage_messages=True)
async def cmd_drug_fox(ctx: ApplicationContext):
logger.info(f"/fox от {ctx.author}")
await ctx.respond("👌.", ephemeral=True)
await ctx.delete()
await ctx.send("<:drug_fox:1304246546644209695>")
@bot.slash_command(name="say")
@guild_only()
@default_permissions(manage_messages=True)
async def cmd_drug_fox(ctx: ApplicationContext, msg: str, reply_to: Message=None, mention: bool=False):
logger.info(f"/say \"{msg}\" от {ctx.author}")
await ctx.respond("👌.", ephemeral=True)
await ctx.delete()
await ctx.send(msg, reference=reply_to, mention_author=mention)