You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何实现Discord按钮点击跳转至其他文本频道指定消息?

实现Discord按钮点击跳转至指定消息

要实现点击按钮跳转至指定频道的消息,推荐用Discord原生的链接按钮,也可以在按钮回调中发送跳转链接,两种方案如下:

方案1:原生链接按钮(推荐)

链接按钮(ButtonStyle.link)是Discord官方支持的跳转方式,点击后直接打开目标消息,无需编写回调逻辑,体验更流畅。

修改后的代码:

class MyView(discord.ui.View):
    def __init__(self, target_message_url):
        super().__init__(timeout=None)  # 若需按钮长期保留,设置timeout=None
        # 添加链接按钮,直接绑定目标消息的跳转链接
        self.add_item(discord.ui.Button(
            label="->",
            style=discord.ButtonStyle.link,
            emoji="😎",
            url=target_message_url
        ))

@commands.has_permissions(administrator=True, ban_members=True) 
@bot.command()
async def button(ctx):
    # 替换为你的目标消息跳转链接,可通过message.jump_url或手动复制获取
    target_jump_url = "https://discord.com/channels/服务器ID/频道ID/消息ID"
    await ctx.send(view=MyView(target_jump_url))

方案2:回调中发送跳转链接

如果需要先执行某些逻辑再引导跳转,可以在按钮回调里发送带跳转链接的消息:

class MyView(discord.ui.View):
    def __init__(self, target_message_url):
        super().__init__()
        self.target_url = target_message_url

    @discord.ui.button(label="->", style=discord.ButtonStyle.primary, emoji="😎")
    async def button_callback(self, button, interaction):
        # 发送仅点击者可见的跳转链接消息(ephemeral=True)
        await interaction.response.send_message(
            f"点击跳转至目标消息:[点我]({self.target_url})",
            ephemeral=True
        )

@commands.has_permissions(administrator=True, ban_members=True) 
@bot.command()
async def button(ctx):
    target_jump_url = "https://discord.com/channels/服务器ID/频道ID/消息ID"
    await ctx.send(view=MyView(target_jump_url))

获取目标消息的跳转链接(jump_url)

  • 代码获取:如果已有目标消息对象message,直接调用message.jump_url即可得到链接
  • 手动获取:在Discord客户端右键目标消息,选择「复制链接」,粘贴到代码中即可

内容的提问来源于stack exchange,提问作者Test

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.12 20:20:29