forked from galacticwarrior9/IslamBot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
quran.py
391 lines (320 loc) · 14.4 KB
/
quran.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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
import aiohttp
import discord
from discord.ext import commands
from discord.ext.commands import CheckFailure, MissingRequiredArgument, BadArgument
from utils import convert_to_arabic_number, make_embed
from dbhandler import DBHandler
INVALID_TRANSLATION = "**Invalid translation**. List of translations: <https://github.com/galacticwarrior9/is" \
"lambot/blob/master/Translations.md>"
INVALID_ARGUMENTS_ARABIC = "**Invalid arguments!** Type `{0}aquran [surah]:[ayah]`. \n\nExample: `{0}aquran 1:1`" \
"\n\nTo send multiple verses, type `{0}quran [surah]:[first ayah]-[last ayah]`" \
"\n\nExample: `{0}aquran 1:1-7`"
INVALID_ARGUMENTS_ENGLISH = "**Invalid arguments!** Type `{0}quran [surah]:[ayah]`. \n\nExample: `{0}quran 1:1`" \
"\n\nTo send multiple verses, type `{0}quran [surah]:[first ayah]-[last ayah]`" \
"\n\nExample: `{0}quran 1:1-7`"
INVALID_SURAH = "**There only 114 surahs.** Please choose a surah between 1 and 114."
INVALID_AYAH = "**There are only {0} verses in this surah**."
DATABASE_UNREACHABLE = "Could not contact database. Please report this on the support server!"
ICON = 'https://cdn6.aptoide.com/imgs/6/a/6/6a6336c9503e6bd4bdf98fda89381195_icon.png'
class InvalidSurah(commands.CommandError):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
class InvalidAyah(commands.CommandError):
def __init__(self, num_verses, *args, **kwargs):
self.num_verses = num_verses
super().__init__(*args, **kwargs)
class QuranSpecifics:
def __init__(self, ref, edition):
self.max_ayah = None
self.min_ayah = None
self.edition = edition
self.edition_name = None
self.ref = ref
self.surah, self.offset, self.limit = self.process_ref(ref)
self.quran_com = self.is_quran_com(edition)
def return_self(self):
return self
def process_ref(self, ref):
surah = int(ref.split(':')[0])
if not 0 < surah < 115:
raise InvalidSurah
min_ayah = int(ref.split(':')[1].split('-')[0])
try:
max_ayah = int(ref.split(':')[1].split('-')[1]) + 1
except IndexError:
max_ayah = min_ayah + 1
# If the min ayah is larger than the max ayah, we assume this is a mistake and swap their values.
if min_ayah > max_ayah:
temp = min_ayah
min_ayah = max_ayah
max_ayah = temp
offset = min_ayah - 1
limit = max_ayah - min_ayah
if limit > 25:
limit = 25
self.max_ayah = max_ayah - 1
self.min_ayah = min_ayah
return [surah, offset, limit]
@staticmethod
def is_quran_com(edition):
return True if isinstance(edition, int) else False
class Quran(commands.Cog):
def __init__(self, bot):
self.bot = bot
self.session = aiohttp.ClientSession(loop=bot.loop)
self.quran_com_url = 'http://api.quran.com:3000/api/v3/chapters/{}/verses?translations={}&language=en&offset={' \
'}&limit={}&text_type=words'
self.alquran_url = 'http://api.alquran.cloud/surah/{}/{}?offset={}&limit={}'
self.arabic_url = 'http://api.alquran.cloud/surah/{}?offset={}&limit={}'
@staticmethod
def format_edition(edition):
edition_dict = {
'mammadaliyev': 'az.mammadaliyev',
'musayev': 'az.musayev',
'bengali': 'bn.bengali',
'bulgarian': 'bg.theophanov',
'bosnian': 25,
'hrbek': 'cs.hrbek',
'nykl': "cs.nykl",
'aburida': 'de.aburida',
'bubenheim': 'de.bubenheim',
'khoury': 'de.khoury',
'zaidan': "de.zaidan",
'divehi': 'dv.divehi',
'amharic': 87,
'haleem': 85,
'taqiusmani': 84,
'khattab': 101,
'ghali': 17,
'finnish': 30,
'indonesian': 33,
'tajik': 74,
'chechen': 106,
'czech': 26,
'sahih': 20,
'ahmedali': 'en.ahmedali',
'arberry': 'en.arberry',
'asad': 'en.asad',
'daryabadi': 'en.daryabadi',
'hilali': 18,
'pickthall': 19,
'qaribullah': 'en.qaribullah',
'sarwar': 'en.sarwar',
'yusufali': 22,
'shakir': 'en.shakir',
'transliteration': 'en.transliteration',
'spanish': 83,
'ansarian': 'fa.ansarian',
'ayati': 'fa.ayati',
'fooladvand': 'fa.fooladvand',
'ghomshei': 'fa.ghomshei',
'makarem': 'fa.makarem',
'french': 31,
'hausa': 32,
'hindi': 82,
'italian': 34,
'japanese': 'ja.japanese',
'korean': 'ko.korean',
'kurdish': 81,
'malayalam': 37,
'dutch': 40,
'norwegian': 'no.berg',
'polish': 'pl.bielawskiego',
'portuguese': 'pt.elhayek',
'romanian': 'ro.grigore',
'kuliev': 45,
'osmanov': 'ru.osmanov',
'porokhova': 'ru.porokhova',
'sindhi': 'sd.amroti',
'somali': 46,
'ahmeti': 'sq.ahmeti',
'mehdiu': 'sq.mehdiu',
'nahi': 'sq.nahi',
'swedish': 48,
'swahili': 'sw.barwani',
'tamil': 'ta.tamil',
'thai': 'th.thai',
'ates': 'tr.ates',
'bulac': 'tr.bulac',
'diyanet': 77,
'golpinarli': 'tr.golpinarli',
'ozturk': 'tr.ozturk',
'vakfi': 'tr.vakfi',
'yazir': 'tr.yazir',
'yildirim': 'tr.yildirim',
'yuksel': 'tr.yuksel',
'tatar': 'tt.nugman',
'uyghur': 'ug.saleh',
'jalandhry': 'ur.jalandhry',
'jawadi': 'ur.jawadi',
'qadri': 'ur.qadri',
'urdu': 97,
'maududi': 97,
'junagarhi': 54,
'maududi.en': 95,
'malay': 39,
'uzbek': 'uz.sodik',
'chinese': 'zh.jian',
'ukrainian': 104,
'abuadel': 79,
'maranao': 38
}
return edition_dict[edition]
@staticmethod
def get_language_code(edition):
language_codes = {
31: 'fr', # Hamidullah, French
97: 'ur', # Maududi, Urdu
54: 'ur', # Junagarhi, Urdu
'ur.jalandhry': 'ur',
'ur.jawadi': 'ur',
'ur.qadri': 'ur',
83: 'es', # Isa Garcia, Spanish
40: 'nl', # Salomo Keyzar, Dutch
25: 'bs', # Bosnian
33: 'id', # Indonesian
45: 'ru', # Kuliev, Russian
78: 'ru', # Ministry of Awqaf, Russian
79: 'ru', # Abu Adel, Russian
48: 'sv', # Knut Bernström, Swedish
'ar': 'ar'
}
if edition in language_codes:
return language_codes[edition]
return None
@commands.command(name="settranslation")
@commands.has_permissions(administrator=True)
async def settranslation(self, ctx, translation: str):
try:
self.format_edition(translation)
except KeyError:
return await ctx.send(INVALID_TRANSLATION)
try:
await DBHandler.create_connection()
except Exception as e:
print(e)
return await ctx.send(DATABASE_UNREACHABLE)
await DBHandler.update_guild_translation(ctx.guild.id, translation)
await ctx.send(f"**Successfully updated default translation to `{translation}`!**")
@settranslation.error
async def settranslation_error(self, ctx, error):
if isinstance(error, CheckFailure):
await ctx.send("🔒 You need the **Administrator** permission to use this command.")
if isinstance(error, MissingRequiredArgument):
await ctx.send(INVALID_TRANSLATION)
@commands.command(name="quran")
async def quran(self, ctx, ref: str, edition: str = None):
async with ctx.channel.typing():
# If no translation was specified, find a translation to use.
if edition is None:
try:
edition = await DBHandler.get_guild_translation(ctx.message.guild.id)
edition = self.format_edition(edition)
except:
edition = 85
# If a translation was specified in the command, check whether it is valid:
else:
try:
edition = self.format_edition(edition)
except KeyError:
return await ctx.send(INVALID_TRANSLATION)
# Now fetch the verses:
spec = self.get_spec(ref, edition)
name, _, translated_name, revelation_location, _, num_verses, _, _, _ = await self.get_surah_info(spec)
if spec.max_ayah > num_verses or spec.min_ayah < 1:
raise InvalidAyah(num_verses)
if revelation_location == "Makkah":
revelation_location = "Meccan"
elif revelation_location == 'Madinah':
revelation_location = "Medinan"
verses = await self.get_verses(spec)
em = make_embed(fields=verses, author=f"Surah {name} ({translated_name})", author_icon=ICON, colour=0x048c28
, inline=False, footer=f'Translation: {spec.edition_name} | {revelation_location}')
if len(em) > 6000:
return await ctx.send("This passage was too long to send.")
await ctx.send(embed=em)
@commands.command(name="aquran")
async def aquran(self, ctx, ref: str):
spec = self.get_spec(ref)
_, name, _, _, _, _, _, _, _ = await self.get_surah_info(spec)
verses = await self.get_verses(spec)
em = make_embed(fields=verses, author=f' سورة {name}', author_icon=ICON, colour=0x048c28, inline=False,
footer="")
if len(em) > 6000:
return await ctx.send("This passage was too long to send.")
await ctx.send(embed=em)
@quran.error
@aquran.error
async def quran_error(self, ctx, error):
if isinstance(error, InvalidSurah):
await ctx.send(INVALID_SURAH)
if isinstance(error, InvalidAyah):
await ctx.send(INVALID_AYAH.format(error.num_verses))
if isinstance(error, MissingRequiredArgument):
await ctx.send(INVALID_TRANSLATION)
if isinstance(error, BadArgument):
await ctx.send(INVALID_ARGUMENTS_ENGLISH.format(ctx.prefix))
@commands.command(name="surah")
async def surah(self, ctx, surah_number: int):
if not 0 < surah_number < 115:
return await ctx.send(INVALID_SURAH)
spec = self.get_spec(f'{surah_number}:1')
name, arabic_name, translated_name, revelation_location, revelation_order, verses_count, summary, first_page, \
final_page = await self.get_surah_info(spec)
em = discord.Embed(colour=0x048c28, title=f'Surah {name} ({translated_name}) | سورة {arabic_name}')
em.set_author(name="Surah Information", icon_url=ICON)
em.description = f'{summary}' \
f'\n\n• **Number of verses**: {verses_count}' \
f'\n• **Revelation location**: {revelation_location}' \
f'\n• **Revelation order**: {revelation_order} ' \
f'\n• **Pages on mushaf**: {first_page}—{final_page}'
await ctx.send(embed=em)
@staticmethod
def get_spec(ref, edition='ar'):
return QuranSpecifics(ref, edition)
async def get_verses(self, spec):
"""Fetches the verses' text. We use the quran.com API or alquran.cloud API depending on the translation used."""
if spec.quran_com:
async with self.session.get(self.quran_com_url.format(spec.surah, spec.edition, spec.offset, spec.limit)) as r:
data = await r.json()
ayaat = data['verses']
verses = {f"{spec.surah}:{ayah['verse_number']}": ayah['translations'][0]['text'] for ayah in ayaat}
spec.edition_name = data['verses'][0]['translations'][0]['resource_name']
elif spec.edition == 'ar':
async with self.session.get(self.arabic_url.format(spec.surah, spec.offset, spec.limit)) as r:
data = await r.json()
ayaat = data['data']['ayahs']
verses = {f"{convert_to_arabic_number(str(spec.surah))}:{convert_to_arabic_number(str(ayah['numberInSurah']))}": ayah['text'] for ayah in ayaat}
else:
async with self.session.get(self.alquran_url.format(spec.surah, spec.edition, spec.offset, spec.limit)) as r:
data = await r.json()
ayaat = data['data']['ayahs']
verses = {f"{spec.surah}:{ayah['numberInSurah']}": ayah['text'] for ayah in ayaat}
spec.edition_name = data['data']['edition']['name']
verses = self.truncate_verses(verses)
return verses
@staticmethod
def truncate_verses(verses):
"""Truncate verses longer than 1024 characters."""
for key, verse in verses.items():
if len(verse) > 1024:
verses.update({key: f"{verse[0:1021]}..."})
return verses
async def get_surah_info(self, spec):
language_code = self.get_language_code(spec.edition)
async with self.session.get(f'http://api.quran.com/api/v3/chapters/{spec.surah}?language={language_code}') as r:
data = await r.json()
name = data['chapter']['name_simple']
arabic_name = data['chapter']['name_arabic']
translated_name = data['chapter']['translated_name']['name']
revelation_location = data['chapter']['revelation_place'].title()
revelation_order = data['chapter']['revelation_order']
verses_count = data['chapter']['verses_count']
first_page, final_page = [page for page in data['chapter']['pages']]
async with self.session.get(f'http://api.quran.com/api/v3/chapters/{spec.surah}/info') as r:
data = await r.json()
summary = data['chapter_info']['short_text']
return name, arabic_name, translated_name, revelation_location, revelation_order, verses_count, summary,\
first_page, final_page
def setup(bot):
bot.add_cog(Quran(bot))