204 lines
7.9 KiB
Python
204 lines
7.9 KiB
Python
"""JARVIS dbrand cog."""
|
|
import logging
|
|
import re
|
|
|
|
import aiohttp
|
|
from naff import Client, Extension, InteractionContext
|
|
from naff.models.discord.embed import EmbedField
|
|
from naff.models.naff.application_commands import (
|
|
OptionTypes,
|
|
SlashCommand,
|
|
slash_option,
|
|
)
|
|
from naff.models.naff.command import cooldown
|
|
from naff.models.naff.cooldowns import Buckets
|
|
|
|
from jarvis.config import JarvisConfig
|
|
from jarvis.data.dbrand import shipping_lookup
|
|
from jarvis.utils import build_embed
|
|
|
|
guild_ids = [862402786116763668] # [578757004059738142, 520021794380447745, 862402786116763668]
|
|
|
|
|
|
class DbrandCog(Extension):
|
|
"""
|
|
dbrand functions for JARVIS
|
|
|
|
Mostly support functions. Credit @cpixl for the shipping API
|
|
"""
|
|
|
|
def __init__(self, bot: Client):
|
|
self.bot = bot
|
|
self.logger = logging.getLogger(__name__)
|
|
self.base_url = "https://dbrand.com/"
|
|
self._session = aiohttp.ClientSession()
|
|
self._session.headers.update({"Content-Type": "application/json"})
|
|
self.api_url = JarvisConfig.from_yaml().urls["dbrand_shipping"]
|
|
self.cache = {}
|
|
|
|
def __del__(self):
|
|
self._session.close()
|
|
|
|
db = SlashCommand(name="db", description="dbrand commands", scopes=guild_ids)
|
|
|
|
@db.subcommand(sub_cmd_name="info", sub_cmd_description="Get useful links")
|
|
@cooldown(bucket=Buckets.USER, rate=1, interval=30)
|
|
async def _info(self, ctx: InteractionContext) -> None:
|
|
urls = [
|
|
f"[Get Skins]({self.base_url + 'skins'})",
|
|
f"[Robot Camo]({self.base_url + 'robot-camo'})",
|
|
f"[Get a Grip]({self.base_url + 'grip'})",
|
|
f"[Shop All Products]({self.base_url + 'shop'})",
|
|
f"[Order Status]({self.base_url + 'order-status'})",
|
|
f"[dbrand Status]({self.base_url + 'status'})",
|
|
f"[Be (not) extorted]({self.base_url + 'not-extortion'})",
|
|
"[Robot Camo Wallpapers](https://db.io/wallpapers)",
|
|
]
|
|
embed = build_embed(
|
|
title="Useful Links", description="\n\n".join(urls), fields=[], color="#FFBB00"
|
|
)
|
|
embed.set_footer(
|
|
text="dbrand.com",
|
|
icon_url="https://dev.zevaryx.com/db_logo.png",
|
|
)
|
|
embed.set_thumbnail(url="https://dev.zevaryx.com/db_logo.png")
|
|
embed.set_author(
|
|
name="dbrand", url=self.base_url, icon_url="https://dev.zevaryx.com/db_logo.png"
|
|
)
|
|
await ctx.send(embeds=embed)
|
|
|
|
@db.subcommand(
|
|
sub_cmd_name="contact",
|
|
sub_cmd_description="Contact support",
|
|
)
|
|
@cooldown(bucket=Buckets.USER, rate=1, interval=30)
|
|
async def _contact(self, ctx: InteractionContext) -> None:
|
|
await ctx.send("Contact dbrand support here: " + self.base_url + "contact")
|
|
|
|
@db.subcommand(
|
|
sub_cmd_name="support",
|
|
sub_cmd_description="Contact support",
|
|
)
|
|
@cooldown(bucket=Buckets.USER, rate=1, interval=30)
|
|
async def _support(self, ctx: InteractionContext) -> None:
|
|
await ctx.send("Contact dbrand support here: " + self.base_url + "contact")
|
|
|
|
@db.subcommand(
|
|
sub_cmd_name="ship",
|
|
sub_cmd_description="Get shipping information for your country",
|
|
)
|
|
@slash_option(
|
|
name="search",
|
|
description="Country search query (2 character code, country name, flag emoji)",
|
|
opt_type=OptionTypes.STRING,
|
|
required=True,
|
|
)
|
|
@cooldown(bucket=Buckets.USER, rate=1, interval=2)
|
|
async def _shipping(self, ctx: InteractionContext, search: str) -> None:
|
|
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:
|
|
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(
|
|
EmbedField(data["carrier"] + " " + data["tier-title"], 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(
|
|
EmbedField(
|
|
service_data["carrier"] + " " + service_data["tier-title"],
|
|
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']}."
|
|
"\n[View all shipping destinations](https://dbrand.com/shipping)"
|
|
" | [Check shipping status]"
|
|
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(embeds=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(embeds=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(embeds=embed)
|
|
|
|
|
|
def setup(bot: Client) -> None:
|
|
"""Add dbrandcog to JARVIS"""
|
|
DbrandCog(bot)
|