Skip to content

Links#

Yuyo provides some utilities for handling Discord links in yuyo.links.

All BaseLink implementations come with 3 classmethods for parsing them from strings.

All of these methods take a Hikari "app" (Bot or REST app object) as their first argument.

link = links.TemplateLink.from_link(app, "https://discord.new/aaaaaaaaaa")

BaseLink.from_link lets you parse a raw link string. This is strict about validation and will raise a ValueError if the full passed string isn't a match for the expected link structure.

if link := links.InviteLink.find(app, "meow you can nyaa us at discord.gg/nekosmeowers"):
    ...

BaseLink.find lets you extract the first link in a string. This will search for a link at any point in the string and returns the parsed link object or None if no link was found.

for link in links.MessageLink.find_iter(app, "message content"):
    ...

BaseLink.find_iter lets you iterate over the matching links in a string by iterating over link objects which were parsed from the string.

There are 5 implementations of BaseLink provided by Yuyo (which all support the parsing methods listed above):

link = links.ChannelLink.from_link(app, "https://discord.com/channels/453123/67765564")

link.is_dm_link  # value: False
link.guild_id  # value: 453123
link.channel_id  # value: 67765564
await link.fetch_channel()  # type: hikari.PartialChannel
link.get_channel()  # type: hikari.GuildChannel | None
await link.fetch_guild()  # type: hikari.RESTGuild | None
link.get_guild()  # type: hikari.GatewayGuild | None
str(link)  # value: "https://discord.com/channels/453123/67765564"

ChannelLink handles parsing channel links.

ChannelLink.guild_id will be None for DM channels.

link = links.make_channel_link(123312, guild=6534234)
link  # value: "https://discord.com/channels/6534234/123312"
link = links.make_channel_link(543123)
link  # value: "https://discord.com/channels/@me/543123"

links.make_channel_link lets you make a channel link string.

link = links.InviteLink.from_link(app, "https://discord.gg/nekosmeowers")

link.code  # value: "nekosmeowers"
await link.fetch_invite()  # type: hikari.Invite
link.get_invite()  # type: hikari.InviteWithMetadata | None
str(link)  # value: "https://discord.gg/nekosmeowers"

InviteLink handles parsing invite links.

link = links.make_invite_link("codecode")
link  # value: "https://discord.gg/codecode"

links.make_invite_link lets you make an invite link string.

link = links.MessageLink.from_link(app, "https://discord.com/channels/54123123321123/2134432234342/56445234124")

link.is_dm_link  # value: False
link.guild_id  # value: 54123123321123
link.channel_id  # value: 2134432234342
link.message_id  # value: 56445234124
await link.fetch_message()  # type: hikari.Message
link.get_message()  # type: hikari.Message | None
await link.fetch_channel()  # type: hikari.PartialChannel
link.get_channel()  # type: hikari.GuildChannel | None
await link.fetch_guild()  # type: hikari.RESTGuild | None
link.get_guild()  # type: hikari.GatewayGuild | None
str(link)  # value: "https://discord.com/channels/54123123321123/2134432234342/56445234124"

MessageLink handles parsing message links.

MessageLink.guild_id will be None for DM messages.

#                             (channel_id, message_id)
link = links.make_message_link(654323412, 4534512332, guild=123321)
link  # value: "https://discord.com/channels/123321/654323412/4534512332"
link = links.make_message_link(333333333, 5555555555)
link  # value: "https://discord.com/channels/@me/333333333/5555555555"

links.make_message_link lets you make a message link string.

link = links.TemplateLink.from_link(app, "https://discord.new/aaaaaaaaaa")

link.code  # value: "aaaaaaaaaa"
await link.fetch_template()  # type: hikari.Template
str(link)  # value: "https://discord.new/aaaaaaaaaa"

TemplateLink handles parsing guild template links.

raw_link = links.make_template_link("cododododoe")
raw_link  # value: "https://discord.new/cododododoe"

links.make_template_link lets you make a template link string.

link = links.WebhookLink.from_link(app, "https://discord.com/api/webhooks/123321123/efsdfasdsa")

link.webhook_id  # value: 123321123
link.token  # value: "efsdfasdsa"
await link.fetch_webhook()  # type: hikari.IncomingWebhook
str(link)  # value: "https://discord.com/api/webhooks/123321123/efsdfasdsa"

WebhookLink handles parsing webhook links.

This class inherits from hikari.ExecutableWebhook and therefore has all the webhook execute methods you'll find on interaction and webhook objects.

raw_link = links.make_webhook_link(123321, "hfdssdasd")
raw_link  # value: "https://discord.com/api/webhooks/123321/hfdssdasd"

links.make_webhook_link lets you make a webhook link string.

permissions = hikari.Permissions.BAN_MEMBERS | hikari.Permissions.MANAGE_CHANNELS
raw_link = links.make_bot_invite(463183358445355009, permissions=permissions)
raw_link  # value: https://discord.com/api/oauth2/authorize?client_id=463183358445355009&scope=bot&permissions=20

links.make_bot_invite lets you make a bot invite link.

This takes the bot's ID as its first argument and has several optional parameters:

  • permissions: Specifies the permissions to request.
  • guild: ID of a specific guild to pre-select for the user.
  • disable_guild_select: Whether to stop the user from selecting another guild when guild has also been passed.

links.make_oauth_link can also be used to make more general Oauth2 authorize links.