jarvis-bot/jarvis/cogs/dbrand.py

268 lines
9.6 KiB
Python

"""J.A.R.V.I.S. dbrand cog."""
import re
import aiohttp
from dis_snek import InteractionContext, Scale, Snake
from dis_snek.models.discord.embed import EmbedField
from dis_snek.models.snek.application_commands import (
OptionTypes,
slash_command,
slash_option,
)
from dis_snek.models.snek.command import cooldown
from dis_snek.models.snek.cooldowns import Buckets
from jarvis.config import get_config
from jarvis.data.dbrand import shipping_lookup
from jarvis.utils import build_embed
guild_ids = [578757004059738142, 520021794380447745, 862402786116763668]
class DbrandCog(Scale):
"""
dbrand functions for J.A.R.V.I.S.
Mostly support functions. Credit @cpixl for the shipping API
"""
def __init__(self, bot: Snake):
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 = {}
def __del__(self):
self._session.close()
@slash_command(
name="db",
sub_cmd_name="skin",
scopes=guild_ids,
sub_cmd_description="See what skins are available",
)
@cooldown(bucket=Buckets.USER, rate=1, interval=30)
async def _skin(self, ctx: InteractionContext) -> None:
await ctx.send(self.base_url + "/skins")
@slash_command(
name="db",
sub_cmd_name="robotcamo",
scopes=guild_ids,
sub_cmd_description="Get some robot camo. Make Tony Stark proud",
)
@cooldown(bucket=Buckets.USER, rate=1, interval=30)
async def _camo(self, ctx: InteractionContext) -> None:
await ctx.send(self.base_url + "robot-camo")
@slash_command(
name="db",
sub_cmd_name="grip",
scopes=guild_ids,
sub_cmd_description="See devices with Grip support",
)
@cooldown(bucket=Buckets.USER, rate=1, interval=30)
async def _grip(self, ctx: InteractionContext) -> None:
await ctx.send(self.base_url + "grip")
@slash_command(
name="db",
sub_cmd_name="contact",
scopes=guild_ids,
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")
@slash_command(
name="db",
sub_cmd_name="support",
scopes=guild_ids,
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")
@slash_command(
name="db",
sub_cmd_name="orderstat",
scopes=guild_ids,
sub_cmd_description="Get your order status",
)
@cooldown(bucket=Buckets.USER, rate=1, interval=30)
async def _orderstat(self, ctx: InteractionContext) -> None:
await ctx.send(self.base_url + "order-status")
@slash_command(
name="db",
sub_cmd_name="orders",
scopes=guild_ids,
sub_cmd_description="Get your order status",
)
@cooldown(bucket=Buckets.USER, rate=1, interval=30)
async def _orders(self, ctx: InteractionContext) -> None:
await ctx.send(self.base_url + "order-status")
@slash_command(
name="db",
sub_cmd_name="status",
scopes=guild_ids,
sub_cmd_description="dbrand status",
)
@cooldown(bucket=Buckets.USER, rate=1, interval=30)
async def _status(self, ctx: InteractionContext) -> None:
await ctx.send(self.base_url + "status")
@slash_command(
name="db",
sub_cmd_name="buy",
scopes=guild_ids,
sub_cmd_description="Give us your money!",
)
@cooldown(bucket=Buckets.USER, rate=1, interval=30)
async def _buy(self, ctx: InteractionContext) -> None:
await ctx.send("Give us your money! " + self.base_url + "shop")
@slash_command(
name="db",
sub_cmd_name="extortion",
scopes=guild_ids,
sub_cmd_description="(not) extortion",
)
@cooldown(bucket=Buckets.USER, rate=1, interval=30)
async def _extort(self, ctx: InteractionContext) -> None:
await ctx.send("Be (not) extorted here: " + self.base_url + "not-extortion")
@slash_command(
name="db",
sub_cmd_name="wallpapers",
sub_cmd_description="Robot Camo Wallpapers",
scopes=guild_ids,
)
@cooldown(bucket=Buckets.USER, rate=1, interval=30)
async def _wallpapers(self, ctx: InteractionContext) -> None:
await ctx.send("Get robot camo wallpapers here: https://db.io/wallpapers")
@slash_command(
name="db",
sub_cmd_name="ship",
sub_cmd_description="Get shipping information for your country",
scopes=guild_ids,
)
@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["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(
EmbedField(
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']}."
"\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"]),
sub_cmd_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",
sub_cmd_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"]),
sub_cmd_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: Snake) -> None:
"""Add dbrandcog to J.A.R.V.I.S."""
DbrandCog(bot)