diff --git a/dbrand.py b/dbrand.py new file mode 100644 index 0000000..b4da455 --- /dev/null +++ b/dbrand.py @@ -0,0 +1,160 @@ +import jarvis +import re +import aiohttp +import html +from jarvis.config import get_config +from jarvis.utils import build_embed +from jarvis.utils.field import Field +from jarvis.data.dbrand import shipping_lookup +from discord.ext import commands + + +class DbrandCog(commands.Cog): + """ + dbrand functions for J.A.R.V.I.S. + + Mostly support functions. Credit @cpixl for the shipping API + """ + + def __init__(self, bot): + self.bot = bot + self.base_url = "https://dbrand.com/" + self._session = aiohttp.ClientSession() + self._session.headers.update({"Content-Type": "application/json"}) + self.api_url = get_config().urls["dbrand_shipping"] + + @commands.group(name="db", aliases=["dbrand"], pass_context=True) + async def _db(self, ctx): + if ctx.invoked_subcommand is None: + await ctx.send( + "Available subcommands: `grip`, `support`, " + + "`skin`, `prism`, `shipping`" + ) + + @_db.command(name="skin", aliases=["skins", "tape"]) + async def _skin(self, ctx): + await ctx.send(self.base_url + "shop/skins") + + @_db.command(name="robotcamo", aliases=["rc", "robot"]) + async def _skin(self, ctx): + await ctx.send(self.base_url + "shop/special-edition/robot-camo") + + @_db.command(name="grip", aliases=["g", "case"]) + async def _grip(self, ctx): + await ctx.send(self.base_url + "shop/grip/#grip-devices") + + @_db.command(name="support", aliases=["help", "contact"]) + async def _support(self, ctx): + await ctx.send(self.base_url + "contact") + + @_db.command(name="shipping", aliases=["ship", "s"]) + async def _shipping(self, ctx, *, search: str): + if not re.match(r"^[A-Z- ]+$", search, re.IGNORECASE): + if re.match( + r"^[\U0001f1e6-\U0001f1ff]{2}$", + search, + re.IGNORECASE, + ): + # Magic number, subtract from flag char to get ascii char + uni2ascii = 127365 + search = chr(ord(search[0]) - uni2ascii) + chr( + ord(search[1]) - uni2ascii + ) + else: + await ctx.send("Please use text to search for shipping.") + return + if len(search) > 2: + matches = [ + x["code"] + for x in shipping_lookup + if search.lower() in x["country"] + ] + if len(matches) > 0: + search = matches[0] + dest = search + api_link = self.api_url + dest + try: + data = await self._session.get(api_link) + except Exception as e: + print(e) + fields = None + if 200 <= data.status < 400: + data = await data.json() + else: + data = None + if ( + data is not None + and data["is_valid"] + and data["shipping_available"] + ): + fields = [] + fields.append(Field(data["short-name"], data["time-title"])) + for service in data["shipping_services_available"][1:]: + service_data = await self._session.get( + self.api_url + dest + "/" + service["url"] + ) + if service_data.status > 400: + continue + service_data = await service_data.json() + fields.append( + Field( + service_data["short-name"], + service_data["time-title"], + ) + ) + country = "-".join( + x for x in data["country"].split(" ") if x != "the" + ) + country_urlsafe = country.replace("-", "%20") + description = f"Click the link above to see shipping time to {data['country']}." + description += "\n[View all shipping destinations](https://dbrand.com/shipping)" + description += " | [Check shipping status]" + description += f"(https://dbrand.com/status#main-content:~:text={country_urlsafe})" + embed = build_embed( + title="Shipping to {}".format(data["country"]), + description=description, + color="#FFBB00", + fields=fields, + url=self.base_url + "shipping/" + country, + ) + embed.set_thumbnail(url=self.base_url + data["country_flag"][1:]) + embed.set_footer( + text="dbrand.com", + icon_url="https://dev.zevaryx.com/db_logo.png", + ) + await ctx.send(embed=embed) + elif not data["is_valid"]: + embed = build_embed( + title="Check Shipping Times", + description="Country not found.\nYou can [view all shipping " + + "destinations here](https://dbrand.com/shipping)", + fields=[], + url="https://dbrand.com/shipping", + color="#FFBB00", + ) + embed.set_thumbnail(url="https://dev.zevaryx.com/db_logo.png") + embed.set_footer( + text="dbrand.com", + icon_url="https://dev.zevaryx.com/db_logo.png", + ) + await ctx.send(embed=embed) + elif not data["shipping_available"]: + embed = build_embed( + title="Shipping to {}".format(data["country"]), + description="No shipping available.\nTime to move to a country" + + " that has shipping available.\nYou can [find a new country " + + "to live in here](https://dbrand.com/shipping)", + fields=[], + url="https://dbrand.com/shipping", + color="#FFBB00", + ) + embed.set_thumbnail(url=self.base_url + data["country_flag"][1:]) + embed.set_footer( + text="dbrand.com", + icon_url="https://dev.zevaryx.com/db_logo.png", + ) + await ctx.send(embed=embed) + + +def setup(bot): + bot.add_cog(DbrandCog(bot))