244 lines
8.1 KiB
Python
244 lines
8.1 KiB
Python
import re
|
|
|
|
import aiohttp
|
|
from discord.ext import commands
|
|
from discord_slash import cog_ext
|
|
from discord_slash.utils.manage_commands import create_option
|
|
|
|
import jarvis
|
|
from jarvis.config import get_config
|
|
from jarvis.data.dbrand import shipping_lookup
|
|
from jarvis.utils import build_embed
|
|
from jarvis.utils.field import Field
|
|
|
|
|
|
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"]
|
|
self.cache = {}
|
|
|
|
@cog_ext.cog_subcommand(
|
|
base="db",
|
|
name="skin",
|
|
guild_ids=[578757004059738142],
|
|
description="See what skins are available",
|
|
)
|
|
async def _skin(self, ctx):
|
|
await ctx.send(self.base_url + "shop/skins")
|
|
|
|
@cog_ext.cog_subcommand(
|
|
base="db",
|
|
name="robotcamo",
|
|
guild_ids=[578757004059738142],
|
|
description="Get some robot camo. Make Tony Stark proud",
|
|
)
|
|
async def _camo(self, ctx):
|
|
await ctx.send(self.base_url + "shop/special-edition/robot-camo")
|
|
|
|
@cog_ext.cog_subcommand(
|
|
base="db",
|
|
name="grip",
|
|
guild_ids=[578757004059738142],
|
|
description="See devices with Grip support",
|
|
)
|
|
async def _grip(self, ctx):
|
|
await ctx.send(self.base_url + "shop/grip/#grip-devices")
|
|
|
|
@cog_ext.cog_subcommand(
|
|
base="db",
|
|
name="contact",
|
|
guild_ids=[578757004059738142],
|
|
description="Contact support",
|
|
)
|
|
async def _contact(self, ctx):
|
|
await ctx.send(
|
|
"Contact dbrand support here: " + self.base_url + "contact"
|
|
)
|
|
|
|
@cog_ext.cog_subcommand(
|
|
base="db",
|
|
name="support",
|
|
guild_ids=[578757004059738142],
|
|
description="Contact support",
|
|
)
|
|
async def _support(self, ctx):
|
|
await ctx.send(
|
|
"Contact dbrand support here: " + self.base_url + "contact"
|
|
)
|
|
|
|
@cog_ext.cog_subcommand(
|
|
base="db",
|
|
name="orderstat",
|
|
guild_ids=[578757004059738142],
|
|
description="Get your order status",
|
|
)
|
|
async def _orderstat(self, ctx):
|
|
await ctx.send(self.base_url + "order-status")
|
|
|
|
@cog_ext.cog_subcommand(
|
|
base="db",
|
|
name="orders",
|
|
guild_ids=[578757004059738142],
|
|
description="Get your order status",
|
|
)
|
|
async def _orders(self, ctx):
|
|
await ctx.send(self.base_url + "order-status")
|
|
|
|
@cog_ext.cog_subcommand(
|
|
base="db",
|
|
name="status",
|
|
guild_ids=[578757004059738142],
|
|
description="dbrand status",
|
|
)
|
|
async def _status(self, ctx):
|
|
await ctx.send(self.base_url + "status")
|
|
|
|
@cog_ext.cog_subcommand(
|
|
base="db",
|
|
name="buy",
|
|
guild_ids=[578757004059738142],
|
|
description="Give us your money!",
|
|
)
|
|
async def _buy(self, ctx):
|
|
await ctx.send("Give us your money! " + self.base_url + "shop")
|
|
|
|
@cog_ext.cog_subcommand(
|
|
base="db",
|
|
name="ship",
|
|
description="Get shipping information for your country",
|
|
guild_ids=[578757004059738142],
|
|
options=[
|
|
(
|
|
create_option(
|
|
name="search",
|
|
description="Country search query (2 character code, country name, emoji)",
|
|
option_type=3,
|
|
required=True,
|
|
)
|
|
)
|
|
],
|
|
)
|
|
async def _shipping(self, ctx, *, search: str):
|
|
await ctx.defer()
|
|
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
|
|
)
|
|
elif search == "🏳️":
|
|
search = "fr"
|
|
else:
|
|
print(search)
|
|
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.lower()
|
|
data = self.cache.get(dest, None)
|
|
if not data:
|
|
api_link = self.api_url + dest
|
|
data = await self._session.get(api_link)
|
|
if 200 <= data.status < 400:
|
|
data = await data.json()
|
|
else:
|
|
data = None
|
|
self.cache[dest] = data
|
|
fields = 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))
|