-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbot.py
352 lines (288 loc) · 14.2 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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
import argparse
import os
import discord
from discord.ext import commands
from dotenv import load_dotenv
import urllib.request
import json
import tempfile
import os.path
import shutil
SEARCH_RESULTS_LIMIT_PER_PAGE = 5
JISHO_IN_KANA_ALONE_TEXT = "Usually written using kana alone"
SENSE_DETAILS_MAP = {"parts_of_speech": "Parts of speech",
"links": "Links",
"tags": "Tags",
"restrictions": "Restrictions",
"see_also": "See also",
"antonyms": "Antonyms",
"source": "Source",
"info": "Info"}
TMP_FOLDER = os.path.join(tempfile.gettempdir(), 'discord_jisho_bot')
SEARCH_API_MAX_RESULTS = 10
TRANSLATE_API_MAX_RESULTS = 20
if not os.path.isdir(TMP_FOLDER):
os.makedirs(TMP_FOLDER)
load_dotenv()
TOKEN = os.getenv('DISCORD_TOKEN')
bot = commands.Bot(command_prefix='-')
@bot.event
async def on_ready():
print(f'{bot.user.name} has connected to Discord!')
@bot.event
async def on_message(message):
await bot.process_commands(message)
@bot.command(name='translate', description="Translation", brief="Translates a Japanese word to English", help='Translates a Japanese word to English\n[args...]:\t[-p PAGE_NUMBER] [-d]', aliases=['t'])
async def translate(ctx, query: str, *args):
print(f"{ctx.message.author.name} wrote: {ctx.message.content}")
if query is None:
print('No arguments given!')
return
options = parse_translate_options(args)
if options is None:
print('Wrong options given!')
return
page = options['page']
details = options['details']
jisho_request_url = f"https://jisho.org/api/v1/search/words?keyword=\"{urllib.parse.quote(query)}\""
print(f"Request: {jisho_request_url}")
try:
tmp_query_filepath = os.path.join(TMP_FOLDER, f"{query}.json")
if os.path.exists(tmp_query_filepath):
with open(tmp_query_filepath, "r", encoding='utf-8') as f:
data = json.loads(f.read())
print(f"Read from cache: {data}")
else:
with urllib.request.urlopen(jisho_request_url) as url:
msg = url.read().decode()
data = json.loads(msg)
# Remove Wikipedia entries
for datum in data['data']:
if isinstance(datum['attribution']['dbpedia'], str):
datum['senses'] = [sense for sense in datum['senses'] if "Wikipedia definition" not in sense['parts_of_speech']]
if any([len(datum['senses']) == 0 for datum in data['data']]):
data['data'] = [datum for datum in data['data'] if len(datum['senses']) > 0]
with open(tmp_query_filepath, "w", encoding="utf-8") as f:
f.write(json.dumps(data))
print(f"Received from jisho.org: {data}")
except urllib.error.HTTPError as e:
print(f'HTTPError: {e.code}')
return
except urllib.error.URLError as e:
print(f'URLError: {e.reason}')
return
embed = create_translation_embed(query, data, page_index=page, show_details=details)
await ctx.channel.send(embed=embed)
@bot.command(name='search', description="Search", brief="Search for a word", help='Search for a word\n[args...]:\t[-p PAGE_NUMBER] [-d]', aliases=['s'])
async def search(ctx, query: str, *args):
print(f"{ctx.message.author.name} wrote: {ctx.message.content}")
if query is None:
print('No arguments given!')
return
options = parse_search_options(args)
if options is None:
print('Wrong options given!')
return
page = options['page']
details = options['details']
jisho_request_url = f"https://jisho.org/api/v1/search/words?keyword={urllib.parse.quote(query)}"
print(f"Request: {jisho_request_url}")
try:
tmp_query_filepath = os.path.join(TMP_FOLDER, f"{query}.json")
if os.path.exists(tmp_query_filepath):
with open(tmp_query_filepath, "r", encoding='utf-8') as f:
data = json.loads(f.read())
print(f"Read from cache: {data}")
else:
with urllib.request.urlopen(jisho_request_url) as url:
msg = url.read().decode()
data = json.loads(msg)
# Remove Wikipedia entries
for datum in data['data']:
if isinstance(datum['attribution']['dbpedia'], str):
datum['senses'] = [sense for sense in datum['senses']
if "Wikipedia definition" not in sense['parts_of_speech']]
if any([len(datum['senses']) == 0 for datum in data['data']]):
data['data'] = [datum for datum in data['data'] if len(datum['senses']) > 0]
with open(tmp_query_filepath, "w", encoding="utf-8") as f:
f.write(json.dumps(data))
print(f"Received from jisho.org: {data}")
except urllib.error.HTTPError as e:
print(f'HTTPError: {e.code}')
return
except urllib.error.URLError as e:
print(f'URLError: {e.reason}')
return
embed = create_search_embed(query, data, page_index=page, show_details=details)
await ctx.channel.send(embed=embed)
@bot.command(name='clearcache', help='Clears cache', aliases=['cc'])
async def clear_cache(ctx):
try:
shutil.rmtree(TMP_FOLDER)
os.makedirs(TMP_FOLDER)
print("Cache cleared!")
await ctx.channel.send("Cache cleared!")
except OSError as e:
print("Error: %s - %s." % (e.filename, e.strerror))
await ctx.channel.send("An error occurred!")
def parse_translate_options(options):
parser = argparse.ArgumentParser()
parser.add_argument("-p", "--page", nargs='?', type=int, default=1)
parser.add_argument("-d", "--details", action='store_true')
try:
parsed_args = parser.parse_args(options)
except:
return None
return {'page': parsed_args.page, 'details': parsed_args.details}
def parse_search_options(options):
parser = argparse.ArgumentParser()
parser.add_argument("-p", "--page", nargs='?', type=int, default=1)
parser.add_argument("-d", "--details", action='store_true')
try:
parsed_args = parser.parse_args(options)
except:
return None
return {'page': parsed_args.page, 'details': parsed_args.details}
def create_translation_embed(query, data, page_index=1, show_details=False):
if len(data['data']) == 0:
return discord.Embed(
title=f"Translations of {query}",
url=f"https://jisho.org/search/\"{urllib.parse.quote(query)}\"",
description="No results found!",
color=discord.Color.green())
if page_index is None:
page_index = 1
embed = discord.Embed(
title=f"Translations of {query}",
url=f"https://jisho.org/search/\"{urllib.parse.quote(query)}\"",
description=f"{len(data['data'])}{'+' if len(data['data']) >= TRANSLATE_API_MAX_RESULTS else ''} result{'s' if len(data['data']) > 1 else ''} found:",
color=discord.Color.green())
max_visible_results_on_page = min(len(data['data']) - ((page_index - 1) * SEARCH_RESULTS_LIMIT_PER_PAGE), SEARCH_RESULTS_LIMIT_PER_PAGE)
is_only_one_page = (len(data['data']) - 1) // SEARCH_RESULTS_LIMIT_PER_PAGE == 0
for i in range((page_index - 1) * SEARCH_RESULTS_LIMIT_PER_PAGE, (page_index - 1) * SEARCH_RESULTS_LIMIT_PER_PAGE + max_visible_results_on_page):
result = data['data'][i]
if 'word' in result['japanese'][0]:
japanese_word = result['japanese'][0]['word']
reading = result['japanese'][0]['reading']
else:
japanese_word = result['japanese'][0]['reading']
reading = None
if show_details:
more_details_per_english_definition_lst = more_details_per_english_definition(result)
english_definitions = [
f"{index + 1}.\t{', '.join(sense['english_definitions'])}\n{more_details_per_english_definition_lst[index]}\n"
if len(result['senses']) > 1
else f" \t{', '.join(sense['english_definitions'])}"
for index, sense in
enumerate(result['senses'])]
else:
english_definitions = [f"{index + 1}.\t{', '.join(sense['english_definitions'])}"
if len(result['senses']) > 1
else f" \t{', '.join(sense['english_definitions'])}"
for index, sense in
enumerate(result['senses'])]
additional_info = None
if JISHO_IN_KANA_ALONE_TEXT in result['senses'][0]['tags'] and not show_details:
additional_info = "*Usually in Kana alone*"
if len(data['data']) == 1:
embed_name = f"{japanese_word}"
else:
embed_name = f"{i + 1}. {japanese_word}"
if show_details:
embed_name += more_details_per_japanese_word(result)
embed_value_details_links = f"[Details](https://jisho.org/word/{japanese_word})\n[Kanji details](https://jisho.org/search/{japanese_word}%20%23kanji)"
embed_value_intro = '\n'.join(filter(None, [f"{japanese_word} {'[{0}]'.format(reading) if reading is not None else ''}", additional_info]))
embed_value_results = '\n'.join(english_definitions)
embed_value = f"{embed_value_details_links}```{embed_value_intro}\n{embed_value_results}```"
if i < max_visible_results_on_page - 1:
embed_value += '\n\u200B'
embed.add_field(name=embed_name, value=embed_value,
inline=False)
if is_only_one_page:
embed.set_footer(text=f"Data provided by jisho.org")
else:
embed.set_footer(text=f"Page {page_index}/{((len(data['data']) - 1) // SEARCH_RESULTS_LIMIT_PER_PAGE) + 1}\nData provided by jisho.org")
return embed
def create_search_embed(query, data, page_index=1, show_details=False):
if len(data['data']) == 0:
return discord.Embed(
title=f"Searching for {query}",
url=f"https://jisho.org/search/{urllib.parse.quote(query)}",
description="No results found!",
color=discord.Color.green())
if page_index is None:
page_index = 1
embed = discord.Embed(
title=f"Searching for {query}",
url=f"https://jisho.org/search/{urllib.parse.quote(query)}",
description=f"{len(data['data'])}{'+' if len(data['data']) >= SEARCH_API_MAX_RESULTS else ''} result{'s' if len(data['data']) > 1 else ''} found:",
color=discord.Color.green())
limit_per_page = min(len(data['data']), SEARCH_RESULTS_LIMIT_PER_PAGE)
is_only_one_page = (len(data['data']) - 1) // SEARCH_RESULTS_LIMIT_PER_PAGE == 0
for i in range((page_index - 1) * SEARCH_RESULTS_LIMIT_PER_PAGE, (page_index - 1) * SEARCH_RESULTS_LIMIT_PER_PAGE + limit_per_page):
result = data['data'][i]
if 'word' in result['japanese'][0]:
japanese_word = result['japanese'][0]['word']
reading = result['japanese'][0]['reading']
else:
japanese_word = result['japanese'][0]['reading']
reading = None
if show_details:
more_details_per_english_definition_lst = more_details_per_english_definition(result)
english_definitions = [
f"{index + 1}.\t{', '.join(sense['english_definitions'])}\n{more_details_per_english_definition_lst[index]}\n"
if len(result['senses']) > 1
else f" \t{', '.join(sense['english_definitions'])}"
for index, sense in
enumerate(result['senses'])]
else:
english_definitions = [f"{index + 1}.\t{', '.join(sense['english_definitions'])}"
if len(result['senses']) > 1
else f" \t{', '.join(sense['english_definitions'])}"
for index, sense in
enumerate(result['senses'])]
additional_info = None
if JISHO_IN_KANA_ALONE_TEXT in result['senses'][0]['tags'] and not show_details:
additional_info = "*Usually in Kana alone*"
if len(data['data']) == 1:
embed_name = f"{japanese_word}"
else:
embed_name = f"{i + 1}. {japanese_word}"
if show_details:
embed_name += more_details_per_japanese_word(result)
embed_value_details_links = f"[Details](https://jisho.org/word/{japanese_word})\n[Kanji details](https://jisho.org/search/{japanese_word}%20%23kanji)"
embed_value_intro = '\n'.join(filter(None, [
f"{japanese_word} {'[{0}]'.format(reading) if reading is not None else ''}", additional_info]))
embed_value_results = '\n'.join(english_definitions)
embed_value = f"{embed_value_details_links}```{embed_value_intro}\n{embed_value_results}```"
if i < limit_per_page - 1:
embed_value += '\n\u200B'
embed.add_field(name=embed_name, value=embed_value,
inline=False)
if is_only_one_page:
embed.set_footer(text=f"Data provided by jisho.org")
else:
embed.set_footer(text=f"Page {page_index}/{((len(data['data']) - 1) // SEARCH_RESULTS_LIMIT_PER_PAGE) + 1}\nData provided by jisho.org")
return embed
def more_details_per_english_definition(result):
more_details = []
for sense in result['senses']:
details_of_sense = []
for key, value in sense.items():
if len(value) > 0 and key != 'english_definitions' and key != 'source':
# TODO consider "source", e.g. appearing with "-t ある -d"
spaces = ' ' * (16 - len(key))
details_of_sense.append(f"*\t {SENSE_DETAILS_MAP[key]}:{spaces}{', '.join(value)}")
more_details.append('\n'.join(details_of_sense))
return more_details
def more_details_per_japanese_word(result):
details_str = ""
if result['is_common']:
details_str += "\n[common]"
if len(result['jlpt']) > 0:
# only show lowest JLPT level
sorted_jlpt_lst = sorted(result['jlpt'], reverse=True)
details_str += f"\n[JLPT N{sorted_jlpt_lst[0][6]}]"
return details_str
if __name__ == "__main__":
bot.run(TOKEN)