-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbot.py
More file actions
98 lines (80 loc) · 2.86 KB
/
bot.py
File metadata and controls
98 lines (80 loc) · 2.86 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
import interactions
from interactions import slash_command, SlashContext, listen, Intents, SlashCommandChoice, slash_option, OptionType
import yaml
scope = "<DISCORD GUILD ID>"
bot = interactions.Client(token="<DISCORD BOT TOKEN>", intents=Intents.DEFAULT)
def write_to_yaml(data):
with open('queries.yml', 'r') as file:
queries = yaml.safe_load(file)
search_engine_data = {
"google": {
"search_string": data["search_string"],
"cx": data.get("cx", "web")
},
"bing": {
"search_string": data["search_string"]
}
}
search_engine = data["search_engine"]
for engine in ("google", "bing"):
if search_engine in ("all", engine):
queries[engine].append(search_engine_data[engine])
with open('queries.yml', 'w') as file:
yaml.safe_dump(queries, file)
@listen()
async def on_ready():
print("Ready")
@slash_command(name="list_queries", description="List of queries that are currently being monitored.")
async def list_queries(ctx: SlashContext):
with open('queries.yml', 'r') as file:
queries = yaml.safe_load(file)
json_queries = yaml.dump(queries)
await ctx.respond(f"```{json_queries}```", ephemeral=True)
@slash_command(name="search", description="Add a search query you want to be monitored.")
@slash_option(
name="query",
description="The search query you want monitored.",
required=True,
opt_type=OptionType.STRING,
)
@slash_option(
name="search_engine",
description="Search engine",
required=True,
opt_type=OptionType.STRING,
choices=[
SlashCommandChoice(name="All", value="all"),
SlashCommandChoice(name="Google", value="google"),
SlashCommandChoice(name="Bing", value="bing"),
]
)
@slash_option(
name="cx",
description="ONLY IF 'GOOGLE' OR 'ALL' IS USED: Custom search engine",
required=False,
opt_type=OptionType.STRING,
choices=[
SlashCommandChoice(name="Web", value="web"),
SlashCommandChoice(name="Social Medias", value="social"),
SlashCommandChoice(name="Facebook", value="facebook"),
SlashCommandChoice(name="Linkedin", value="linkedin"),
SlashCommandChoice(name="StackOverflow", value="stackoverflow"),
SlashCommandChoice(name="GitHub", value="github")
]
)
async def search(ctx: SlashContext, query: str, search_engine: str, cx: str | None = None):
if search_engine == "all" or search_engine == "google":
data = {
"search_string": query,
"search_engine": search_engine,
"cx": cx or "web"
}
else:
data = {
"search_string": query,
"search_engine": search_engine,
}
write_to_yaml(data)
await ctx.respond(f"Searching for '{query}' using {search_engine}.", ephemeral=True)
if __name__ == '__main__':
bot.start()