/
response_handler.py
324 lines (238 loc) · 11.3 KB
/
response_handler.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
import discord
import scrython
import re
import asyncio
import requests
from PIL import Image
from svglib.svglib import svg2rlg
from reportlab.graphics import renderPM
import io
#TODO: add more error handling, add MagicCard class wich will hold all the data and methods for card
def handle_response(message, client):
"""
Handles all the responses to the user
Parameters:
message (discord.Message): message from the user
client (discord.Client): client object
Returns:
list: message on index 1, list of embeds on index 2
or
str: message
"""
card_names = re.findall(r'\[(.*?)\]', message.content)
if card_names != []:
set_of_embeds = []
embeds = []
try:
for card_name in card_names:
card_name = card_name.replace('[', '').replace(']','').replace(',',' ')
if card_name.startswith('$'):
card_data = get_card_data(card_name[1:])
if isinstance(card_data, scrython.cards.named.Named):
card_prices = get_price(card_data)
name = get_name(card_data)
embed = discord.Embed()
embed.title = f'{name} prices'
embed.url = get_link(card_data)
try:
for price in card_prices:
embed.add_field(name=f'> {price[0]}', value=f'> {price[1]}', inline=True)
except Exception as e:
continue
return [f'Here are known prices for card {name}:', [[embed]]]
else:
return (f'Sorry, but but there is problem with your search: {card_data[6:]}')
elif card_name.startswith('?'):
card_data = get_card_data(card_name[1:])
if isinstance(card_data, scrython.cards.named.Named):
card_rulings = get_rulings(card_data)
name = get_name(card_data)
data = requests.get(card_rulings)
data = data.json()
data = [f'\n - **{object["comment"]}**' for object in data['data']]
if data == []:
data = ['ReAdinG ThE cArD, ExPlaIns ThE cArD :point_down::fire:']
embed = discord.Embed()
embed.title = f'{name} rulings'
embed.url = get_link(card_data)
embed.description = f'> {"".join(data)}'
return [f'Here are rulings for {name}:', [[embed]]]
else:
return (f'Sorry, but but there is problem with your search: {card_data[6:]}')
elif card_name.startswith('@'):
try:
results = search_cards(card_name[1:])
except Exception as e:
return (f'Sorry, but but there is problem with your search: {e}')
for i in results[1]:
if len(set_of_embeds) < 1:
oracle = emojize(i[-1],client, message)
mana_cost = emojize(i[-2],client, message)
else:
oracle = i[-1]
mana_cost = i[-2]
card_type = i[-3]
embed = discord.Embed()
embed.title = i[0]
embed.set_thumbnail(url=i[2])
embed.description = f'{mana_cost}\n{card_type}\n{oracle}'
if 'gatherer' in i[1].keys():
embed.url = i[1]['gatherer']
elif 'edhrec' in i[1].keys():
embed.url = i[1]['edhrec']
embeds.append(embed)
if len(embeds) == 10:
set_of_embeds.append(embeds)
embeds = []
elif card_name.startswith('!'):
card_data = get_card_data(card_name[1:])
if isinstance(card_data, scrython.cards.named.Named):
card_flavor = get_flavor(card_data)
name = get_name(card_data)
embed = discord.Embed()
embed.title = f'{name}'
embed.image = get_card_image(card_data)
embed.url = get_link(card_data)
embeds.append(embed)
if embeds == 10:
set_of_embeds.append(embeds)
embeds = []
else:
return (f'Sorry, but but there is problem with your search: {card_data[6:]}')
else:
card_data = get_card_data(card_name)
if isinstance(card_data, scrython.cards.named.Named):
mana_cost = emojize(get_mana_cost(card_data), client, message)
oracle = emojize(get_oracle(card_data), client, message)
try:
flavor = f'*{get_flavor(card_data)}*'
except Exception as e:
flavor = ''
embed = discord.Embed(title=f'{get_name(card_data)} {mana_cost}', description=f'\n{get_type(card_data)}\n\n{oracle}\n\n{flavor}', url=get_link(card_data))
embed.set_thumbnail(url=get_card_image(card_data))
embeds.append(embed)
if len(embeds) == 10:
set_of_embeds.append(embeds)
embeds = []
else:
return (f'Sorry, but but there is problem with your search: {card_data[6:]}')
set_of_embeds.append(embeds)
if len(set_of_embeds) <= 10:
variable_message = 'Here you go:\n'
else:
variable_message = f'Here is first {len(set_of_embeds[0])}:\n'
return [f'I found total {sum([len(i) for i in set_of_embeds])} cards with this parameters. {variable_message}', [set_of_embeds[0]]]
except Exception as e:
#print(e) # for debugging purposes
return('Some error occured, i dont feel so good. Contact admin and tell him to help me.')
def get_card_data(card_name):
"""
Uses scrython module to search cards via scryfall api with exact search or fuzzy search and get card data
Parameters:
card_name (str): name of the card to search
Returns:
scrython.cards.named.Named: card data object or error message if card not found
"""
try:
if card_name[0] == '=':
card_data = scrython.cards.Named(exact=card_name[1:])
else:
card_data = scrython.cards.Named(fuzzy=card_name)
return (card_data)
except Exception as e:
return f"Error: {e}"
def search_cards(query):
"""
Uses scrython module to search cards via scryfall api with scryfall query
Parameters:
query (str): scryfall query
Returns:
tuple: total number of cards found, list of lists with card data
"""
results = scrython.cards.Search(q=query, order='name', unique='cards')
return (results.total_cards(),[[object['name'], object['related_uris'], object['image_uris']['normal'], object['type_line'], object['mana_cost'], object['oracle_text']] for object in results.data() if 'image_uris' and 'oracle_text' in object.keys()])
def get_name(card_data):
return card_data.name()
def get_mana_cost(card_data):
return card_data.mana_cost()
def get_card_image(card_data):
return card_data.image_uris()['normal']
def get_type(card_data):
return card_data.type_line()
def get_oracle(card_data):
return card_data. oracle_text()
def get_flavor(card_data):
return card_data.flavor_text()
def get_link(card_data):
try:
return card_data.related_uris()['gatherer']
except Exception:
return None
def get_price(card_data):
prices = []
for currency in ['usd', 'usd_foil', 'usd_etched', 'usd_glossy', 'eur', 'tix']:
try:
price = card_data.prices(currency)
if price == None:
price = 'N/A'
except KeyError:
price = 'N/A'
prices.append([currency,price])
return prices
def get_rulings(card_data):
return card_data.rulings_uri()
def emojize(text, client, message):
"""
Replaces all mana symbols in text with discord emojis and adds them to server,
if they are not already there.
Parameters:
text (str): text to replace mana symbols in
client (discord.Client): client object
message (discord.Message): message from the user
Returns:
str: text with replaced mana symbols
"""
try:
guild = client.get_guild(message.guild.id)
all_symbols = re.findall(r'{.[^}]*}', text)
symbol_data = scrython.symbology.Symbology().data()
for i in all_symbols:
symbol_object = next((obj for obj in symbol_data if obj['symbol'] == i), False)
if symbol_object:
name = i.replace('}','symbol').replace('{','').replace('/','')
if name not in [e.name for e in message.guild.emojis]:
img_byte_arr = download_emoji(symbol_object['svg_uri'])
asyncio.run(add_moji(guild, name, img_byte_arr))
emoji = next((obj for obj in message.guild.emojis if obj.name == name), i)
text = text.replace(i, str(emoji))
return text
except Exception as e:
print(e)
def download_emoji(url):
"""
Downloads svg image and converts it to png
Parameters:
url (str): url of the svg image
Returns:
bytes: png image
"""
img_response = requests.get(url).text
with open('media/swap_pic.svg', 'w+') as file:
file.write(img_response)
drawing = svg2rlg('media/swap_pic.svg')
img = renderPM.drawToPIL(drawing)
img = img.convert("RGBA")
datas = img.getdata()
newData = []
for item in datas:
if item[0] == 255 and item[1] == 255 and item[2] == 255:
newData.append((255, 255, 255, 0))
else:
newData.append(item)
img.putdata(newData)
img_byte_arr = io.BytesIO()
img.save(img_byte_arr, format='PNG')
img_byte_arr = img_byte_arr.getvalue()
return img_byte_arr
async def add_moji(guild, name, image):
await guild.create_custom_emoji(name=name, image=image)