Migrate dbrand, closes #94

This commit is contained in:
Zeva Rose 2022-02-03 05:21:02 -07:00
parent da2a64becd
commit 88b9c63a92

View file

@ -2,26 +2,31 @@
import re import re
import aiohttp import aiohttp
from discord.ext import commands from dis_snek import InteractionContext, Scale, Snek
from discord_slash import SlashContext, cog_ext from dis_snek.models.discord.embed import EmbedField
from discord_slash.utils.manage_commands import create_option 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.config import get_config
from jarvis.data.dbrand import shipping_lookup from jarvis.data.dbrand import shipping_lookup
from jarvis.utils import build_embed from jarvis.utils import build_embed
from jarvis.utils.field import Field
guild_ids = [578757004059738142, 520021794380447745, 862402786116763668] guild_ids = [578757004059738142, 520021794380447745, 862402786116763668]
class DbrandCog(commands.Cog): class DbrandCog(Scale):
""" """
dbrand functions for J.A.R.V.I.S. dbrand functions for J.A.R.V.I.S.
Mostly support functions. Credit @cpixl for the shipping API Mostly support functions. Credit @cpixl for the shipping API
""" """
def __init__(self, bot: commands.Bot): def __init__(self, bot: Snek):
self.bot = bot self.bot = bot
self.base_url = "https://dbrand.com/" self.base_url = "https://dbrand.com/"
self._session = aiohttp.ClientSession() self._session = aiohttp.ClientSession()
@ -32,134 +37,130 @@ class DbrandCog(commands.Cog):
def __del__(self): def __del__(self):
self._session.close() self._session.close()
@cog_ext.cog_subcommand( @slash_command(
base="db", name="db",
name="skin", sub_cmd_name="skin",
guild_ids=guild_ids, scopes=guild_ids,
description="See what skins are available", sub_cmd_description="See what skins are available",
) )
@commands.cooldown(1, 30, commands.BucketType.channel) @cooldown(bucket=Buckets.USER, rate=1, interval=30)
async def _skin(self, ctx: SlashContext) -> None: async def _skin(self, ctx: InteractionContext) -> None:
await ctx.send(self.base_url + "/skins") await ctx.send(self.base_url + "/skins")
@cog_ext.cog_subcommand( @slash_command(
base="db", name="db",
name="robotcamo", sub_cmd_name="robotcamo",
guild_ids=guild_ids, scopes=guild_ids,
description="Get some robot camo. Make Tony Stark proud", sub_cmd_description="Get some robot camo. Make Tony Stark proud",
) )
@commands.cooldown(1, 30, commands.BucketType.channel) @cooldown(bucket=Buckets.USER, rate=1, interval=30)
async def _camo(self, ctx: SlashContext) -> None: async def _camo(self, ctx: InteractionContext) -> None:
await ctx.send(self.base_url + "robot-camo") await ctx.send(self.base_url + "robot-camo")
@cog_ext.cog_subcommand( @slash_command(
base="db", name="db",
name="grip", sub_cmd_name="grip",
guild_ids=guild_ids, scopes=guild_ids,
description="See devices with Grip support", sub_cmd_description="See devices with Grip support",
) )
@commands.cooldown(1, 30, commands.BucketType.channel) @cooldown(bucket=Buckets.USER, rate=1, interval=30)
async def _grip(self, ctx: SlashContext) -> None: async def _grip(self, ctx: InteractionContext) -> None:
await ctx.send(self.base_url + "grip") await ctx.send(self.base_url + "grip")
@cog_ext.cog_subcommand( @slash_command(
base="db", name="db",
name="contact", sub_cmd_name="contact",
guild_ids=guild_ids, scopes=guild_ids,
description="Contact support", sub_cmd_description="Contact support",
) )
@commands.cooldown(1, 30, commands.BucketType.channel) @cooldown(bucket=Buckets.USER, rate=1, interval=30)
async def _contact(self, ctx: SlashContext) -> None: async def _contact(self, ctx: InteractionContext) -> None:
await ctx.send("Contact dbrand support here: " + self.base_url + "contact") await ctx.send("Contact dbrand support here: " + self.base_url + "contact")
@cog_ext.cog_subcommand( @slash_command(
base="db", name="db",
name="support", sub_cmd_name="support",
guild_ids=guild_ids, scopes=guild_ids,
description="Contact support", sub_cmd_description="Contact support",
) )
@commands.cooldown(1, 30, commands.BucketType.channel) @cooldown(bucket=Buckets.USER, rate=1, interval=30)
async def _support(self, ctx: SlashContext) -> None: async def _support(self, ctx: InteractionContext) -> None:
await ctx.send("Contact dbrand support here: " + self.base_url + "contact") await ctx.send("Contact dbrand support here: " + self.base_url + "contact")
@cog_ext.cog_subcommand( @slash_command(
base="db", name="db",
name="orderstat", sub_cmd_name="orderstat",
guild_ids=guild_ids, scopes=guild_ids,
description="Get your order status", sub_cmd_description="Get your order status",
) )
@commands.cooldown(1, 30, commands.BucketType.channel) @cooldown(bucket=Buckets.USER, rate=1, interval=30)
async def _orderstat(self, ctx: SlashContext) -> None: async def _orderstat(self, ctx: InteractionContext) -> None:
await ctx.send(self.base_url + "order-status") await ctx.send(self.base_url + "order-status")
@cog_ext.cog_subcommand( @slash_command(
base="db", name="db",
name="orders", sub_cmd_name="orders",
guild_ids=guild_ids, scopes=guild_ids,
description="Get your order status", sub_cmd_description="Get your order status",
) )
@commands.cooldown(1, 30, commands.BucketType.channel) @cooldown(bucket=Buckets.USER, rate=1, interval=30)
async def _orders(self, ctx: SlashContext) -> None: async def _orders(self, ctx: InteractionContext) -> None:
await ctx.send(self.base_url + "order-status") await ctx.send(self.base_url + "order-status")
@cog_ext.cog_subcommand( @slash_command(
base="db", name="db",
name="status", sub_cmd_name="status",
guild_ids=guild_ids, scopes=guild_ids,
description="dbrand status", sub_cmd_description="dbrand status",
) )
@commands.cooldown(1, 30, commands.BucketType.channel) @cooldown(bucket=Buckets.USER, rate=1, interval=30)
async def _status(self, ctx: SlashContext) -> None: async def _status(self, ctx: InteractionContext) -> None:
await ctx.send(self.base_url + "status") await ctx.send(self.base_url + "status")
@cog_ext.cog_subcommand( @slash_command(
base="db", name="db",
name="buy", sub_cmd_name="buy",
guild_ids=guild_ids, scopes=guild_ids,
description="Give us your money!", sub_cmd_description="Give us your money!",
) )
@commands.cooldown(1, 30, commands.BucketType.channel) @cooldown(bucket=Buckets.USER, rate=1, interval=30)
async def _buy(self, ctx: SlashContext) -> None: async def _buy(self, ctx: InteractionContext) -> None:
await ctx.send("Give us your money! " + self.base_url + "shop") await ctx.send("Give us your money! " + self.base_url + "shop")
@cog_ext.cog_subcommand( @slash_command(
base="db", name="db",
name="extortion", sub_cmd_name="extortion",
guild_ids=guild_ids, scopes=guild_ids,
description="(not) extortion", sub_cmd_description="(not) extortion",
) )
@commands.cooldown(1, 30, commands.BucketType.channel) @cooldown(bucket=Buckets.USER, rate=1, interval=30)
async def _extort(self, ctx: SlashContext) -> None: async def _extort(self, ctx: InteractionContext) -> None:
await ctx.send("Be (not) extorted here: " + self.base_url + "not-extortion") await ctx.send("Be (not) extorted here: " + self.base_url + "not-extortion")
@cog_ext.cog_subcommand( @slash_command(
base="db", name="db",
name="wallpapers", sub_cmd_name="wallpapers",
description="Robot Camo Wallpapers", sub_cmd_description="Robot Camo Wallpapers",
guild_ids=guild_ids, scopes=guild_ids,
) )
@commands.cooldown(1, 30, commands.BucketType.channel) @cooldown(bucket=Buckets.USER, rate=1, interval=30)
async def _wallpapers(self, ctx: SlashContext) -> None: async def _wallpapers(self, ctx: InteractionContext) -> None:
await ctx.send("Get robot camo wallpapers here: https://db.io/wallpapers") await ctx.send("Get robot camo wallpapers here: https://db.io/wallpapers")
@cog_ext.cog_subcommand( @slash_command(
base="db", name="db",
name="ship", sub_cmd_name="ship",
description="Get shipping information for your country", sub_cmd_description="Get shipping information for your country",
guild_ids=guild_ids, scopes=guild_ids,
options=[
(
create_option(
name="search",
description="Country search query (2 character code, country name, emoji)",
option_type=3,
required=True,
)
)
],
) )
@commands.cooldown(1, 2, commands.BucketType.user) @slash_option(
async def _shipping(self, ctx: SlashContext, search: str) -> None: name="search",
description="Country search query (2 character code, country name, flag emoji)",
option_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() await ctx.defer()
if not re.match(r"^[A-Z- ]+$", search, re.IGNORECASE): if not re.match(r"^[A-Z- ]+$", search, re.IGNORECASE):
if re.match( if re.match(
@ -193,14 +194,14 @@ class DbrandCog(commands.Cog):
fields = None fields = None
if data is not None and data["is_valid"] and data["shipping_available"]: if data is not None and data["is_valid"] and data["shipping_available"]:
fields = [] fields = []
fields.append(Field(data["short-name"], data["time-title"])) fields.append(EmbedField(data["short-name"], data["time-title"]))
for service in data["shipping_services_available"][1:]: for service in data["shipping_services_available"][1:]:
service_data = await self._session.get(self.api_url + dest + "/" + service["url"]) service_data = await self._session.get(self.api_url + dest + "/" + service["url"])
if service_data.status > 400: if service_data.status > 400:
continue continue
service_data = await service_data.json() service_data = await service_data.json()
fields.append( fields.append(
Field( EmbedField(
service_data["short-name"], service_data["short-name"],
service_data["time-title"], service_data["time-title"],
) )
@ -215,7 +216,7 @@ class DbrandCog(commands.Cog):
) )
embed = build_embed( embed = build_embed(
title="Shipping to {}".format(data["country"]), title="Shipping to {}".format(data["country"]),
description=description, sub_cmd_description=description,
color="#FFBB00", color="#FFBB00",
fields=fields, fields=fields,
url=self.base_url + "shipping/" + country, url=self.base_url + "shipping/" + country,
@ -229,7 +230,7 @@ class DbrandCog(commands.Cog):
elif not data["is_valid"]: elif not data["is_valid"]:
embed = build_embed( embed = build_embed(
title="Check Shipping Times", title="Check Shipping Times",
description=( sub_cmd_description=(
"Country not found.\nYou can [view all shipping " "Country not found.\nYou can [view all shipping "
"destinations here](https://dbrand.com/shipping)" "destinations here](https://dbrand.com/shipping)"
), ),
@ -246,7 +247,7 @@ class DbrandCog(commands.Cog):
elif not data["shipping_available"]: elif not data["shipping_available"]:
embed = build_embed( embed = build_embed(
title="Shipping to {}".format(data["country"]), title="Shipping to {}".format(data["country"]),
description=( sub_cmd_description=(
"No shipping available.\nTime to move to a country" "No shipping available.\nTime to move to a country"
" that has shipping available.\nYou can [find a new country " " that has shipping available.\nYou can [find a new country "
"to live in here](https://dbrand.com/shipping)" "to live in here](https://dbrand.com/shipping)"
@ -263,6 +264,6 @@ class DbrandCog(commands.Cog):
await ctx.send(embed=embed) await ctx.send(embed=embed)
def setup(bot: commands.Bot) -> None: def setup(bot: Snek) -> None:
"""Add dbrandcog to J.A.R.V.I.S.""" """Add dbrandcog to J.A.R.V.I.S."""
bot.add_cog(DbrandCog(bot)) bot.add_cog(DbrandCog(bot))