-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
166 lines (145 loc) · 5.91 KB
/
main.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
import discord
from os import path
from BladeAndSoul import character as Character
from BladeAndSoul.bns import avg_dmg
from BladeAndSoul.bns import fetch_profile
from BladeAndSoul.errors import (CharacterNotFound, Error, InvalidData,
ServiceUnavialable)
from discord.ext import commands
import json
from .values import DATA
def fetch_user(id):
P = path.join(DATA, id)
if not path.exists:
return
with open(P, errors='backslashreplace') as f:
return json.load(f)
async def find_character(ctx, char):
if char and not char.isnumeric():
return await Character(char)
if len(ctx.message.raw_mentions):
char = ctx.message.raw_mentions[0]
char = fetch_user(str(char))
if not char:
raise CharacterNotFound
char = char.get('Character Name')
if not char:
raise CharacterNotFound
return await Character(char)
if char:
fetch_user(char)
if not char:
raise CharacterNotFound
char = await Character(char)
if char is None:
raise InvalidData
return char
class BladeAndSoul:
"""Blade And Soul Commands."""
def __init__(self, bot: commands.Bot):
"""Init Statement."""
self.bot = bot
@staticmethod
def color_pick(faction):
if faction is None:
return discord.Color.darker_grey()
if faction == 'Cerulean Order':
return discord.Color.blue()
return discord.Color.red()
@commands.group(pass_context=True)
async def bns(self, ctx):
"""Blade And Soul Command Group."""
pass
@bns.command(pass_context=True)
async def profile(self, ctx, *, char=None):
"""Blade And Soul Profile."""
try:
await self.bot.say((await find_character(ctx,
char)).pretty_profile())
return
except CharacterNotFound:
await self.bot.say('Could not find character')
return
except ServiceUnavialable:
await self.bot.say('Cannot access BNS try again later.')
except (Error, InvalidData):
await self.bot.say('An unexpected error has occured.')
@bns.command(pass_context=True)
async def stats(self, ctx, *, char=None):
"""Blade And Soul Stats."""
try:
await self.bot.say((await find_character(ctx,
char)).pretty_stats())
return
except CharacterNotFound:
await self.bot.say('Could not find character')
return
except ServiceUnavialable:
await self.bot.say('Cannot access BNS try again later.')
except (Error, InvalidData):
await self.bot.say('An unexpected error has occured.')
@bns.command(pass_context=True)
async def gear(self, ctx, *, char=None):
"""Blade And Soul Gear, for the requested character."""
try:
await self.bot.say((await find_character(ctx, char)).pretty_gear())
return
except CharacterNotFound:
await self.bot.say('Could not find character')
return
except ServiceUnavialable:
await self.bot.say('Cannot access BNS try again later.')
except (Error, InvalidData):
await self.bot.say('An unexpected error has occured.')
@bns.command(pass_context=True)
async def outfit(self, ctx, *, char=None):
"""Blade And Soul Outfit, for the requested character."""
try:
character = await find_character(ctx, char)
embed = discord.Embed(color=self.color_pick(character['Faction']))
auth = character['Character Name']
avatar = character['Picture']
embed.set_author(name=auth, icon_url=avatar)
outfit = character['Outfit']
embed.add_field(name='Body', value=outfit['Clothes'])
embed.add_field(name='Head', value=outfit['Head'])
embed.add_field(name='Face', value=outfit['Face'])
embed.add_field(name='Adornment', value=outfit['Adornment'])
try:
await self.bot.say(embed=embed)
except:
embed.set_author(name=auth)
try:
await self.bot.say(embed=embed)
except:
await self.bot.say(character.pretty_outfit())
except CharacterNotFound:
await self.bot.say('Could not find character')
return
except ServiceUnavialable:
await self.bot.say('Cannot access BNS try again later.')
except (Error, InvalidData):
await self.bot.say('An unexpected error has occured.')
@bns.command(pass_context=True)
async def save(self, ctx, *, char):
"""Save Character, so you dont have to type the name everytime."""
with open(path.join(DATA, str(ctx.message.author.id)), 'w',
errors='backslashreplace') as f:
json.dump({'Character Name': (await fetch_profile(char))['Character Name']}, f)
await self.bot.say('Saved')
@bns.command(pass_context=True, name='avg')
async def avg_dmg(self, ctx, attack_power: str, critical_rate: str,
critical_damage: str, elemental_bonus: str='100%'):
"""Inaccurate, maybe broken."""
embed = discord.Embed()
embed.title = 'Average Damage'
auth = ctx.message.author
avatar = auth.avatar_url or auth.default_avatar_url
embed.set_author(name=str(auth), icon_url=avatar)
no_blue, with_blue = avg_dmg(attack_power, critical_rate,
critical_damage, elemental_bonus)
embed.add_field(name='No Buff', value=no_blue)
embed.add_field(name='Blue Buff', value=with_blue)
await self.bot.say(embed=embed)
def setup(bot: commands.Bot):
bot.add_cog(BladeAndSoul(bot))