-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.py
88 lines (72 loc) · 2.78 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
import discord
from discord.ext import commands
from io import BytesIO
import os, requests, time, json, aiohttp
from bs4 import BeautifulSoup
from dotenv import load_dotenv
from utils.imageutils import imageGrid
from utils.htmlutils import *
from utils.pagination import LinePaginator
load_dotenv()
TOKEN = os.getenv('DISCORD_BOT_TOKEN')
bot = commands.Bot(command_prefix='?')
@bot.command(help="Type ?guide to see the list of heroes.")
async def guide(ctx, *, character=None):
with open('names.json', 'r') as f:
names = json.load(f)
with open('realnames.txt', 'r') as f:
realNames = [line.rstrip() for line in f.readlines()]
if not character:
embed = discord.Embed(title="**Current list of Heroes**")
await LinePaginator.paginate(
bot,
realNames,
ctx,
embed,
footer_text=f"Usage is {bot.command_prefix}guide <hero name>.",
empty=False,
max_lines=20
)
return
def namesearch(char):
for key, val in names.items():
if char in val or char == key:
return key
url_character = namesearch(''.join(character).lower())
if not url_character:
await ctx.send(f"`{character}` is not a valid character.")
return
a = round(time.perf_counter(), 3) # start performance timer
url = f'https://www.icy-veins.com/heroes/{url_character}-build-guide'
r = requests.get(url)
soup = BeautifulSoup(r.text, 'html.parser')
buildsinfo = getinfo(soup) # Dict of info for each build.
with open('test.json', 'w') as f:
json.dump(buildsinfo, f, indent=4)
# Fetch the icons
image_urls = []
for b in buildsinfo.values():
for level in b.get('levels').values():
image_urls.append(level.get('url'))
conn = aiohttp.TCPConnector()
image_data = await fetch(conn, image_urls)
# insert the image bytes into the buildsinfo dict
for b in buildsinfo.values():
for level in b.get('levels').values():
_url = level['url']
level['image_data'] = image_data[_url]
# Pass the image list into the imageGrid function. It will return a list of images for each build.
result = imageGrid(buildsinfo)
# Post each image to discord with the build code as plain text, so users can easily copy it.
i = 1
for code, img in result:
buffer = BytesIO()
img.save(buffer, 'png')
buffer.seek(0)
await ctx.send(f"`Build [{i}/{len(result)}]: {code}`", file=discord.File(buffer, filename='unknown.png'))
i += 1
await ctx.send(f'<{url}>')
b = round(time.perf_counter(), 3) # finish time
print(f'{len(result)} image(s) generated in {(str(round(b-a, 3)))} seconds for character {character}.')
return
bot.run(TOKEN)