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

如何在Discord机器人代码中用自定义表情替代默认骷髅表情?

解决Discord机器人自定义表情无法显示的问题

问题原因

  1. 你将Grid内容放在了代码块(```)中,Discord会把代码块内的内容当作纯文本处理,不会解析自定义表情的<:skulls:1098972621070139473>格式,导致表情无法渲染。
  2. 需确保机器人已加入该自定义表情所在的服务器,否则无权调用该表情。

解决方案

移除Grid部分的代码块包裹,改用单个反引号包裹每一行(保留固定宽度字体样式),让Discord能够正确解析自定义表情格式。

修改后的完整代码

from discord import app_commands 
import discord
from random import randint
import time


class aclient(discord.Client):
    def __init__(self):
        super().__init__(intents = discord.Intents.all())
        self.synced = False 

    async def on_ready(self):
        await self.wait_until_ready()
        if not self.synced: 
            await tree.sync()
            self.synced = True
        print(f"We have logged in as {self.user}.")

client = aclient()
tree = app_commands.CommandTree(client)


@tree.command(name = 'towers', description='towers gamemode')
async def towers(interaction: discord.Interaction, round_id:str, tile_amount:int):
    if len(round_id) == 8:
        start_time = time.time()
        # 替换为自定义表情
        custom_skull = '<:skulls:1098972621070139473>'
        grid = [
            [custom_skull, custom_skull, custom_skull],
            [custom_skull, custom_skull, custom_skull],
            [custom_skull, custom_skull, custom_skull],
            [custom_skull, custom_skull, custom_skull],
            [custom_skull, custom_skull, custom_skull],
            [custom_skull, custom_skull, custom_skull],
            [custom_skull, custom_skull, custom_skull],
            [custom_skull, custom_skull, custom_skull]
        ]
       
        for count in range(tile_amount):
            a = randint(0,2)
            grid[count][a] = '💎'

        chance = randint(45,95)
        if tile_amount < 4:
                chance -= 15

        em = discord.Embed(color=0x0025ff)
        # 用单个反引号包裹每一行,保留排版同时渲染表情
        grid_text = "\n".join([
            f"`{row[0]}{row[1]}{row[2]}`"
            for row in reversed(grid)
        ])
        em.add_field(
            name="Grid", 
            value=f"{grid_text}\n\n**Accuracy**\n```{chance}%```\n**Round ID**\n```{round_id}```\n**Response Time:**\n```{int(time.time() - start_time)}```"
        )
        
        await interaction.response.send_message(embed=em)
    else:
        em = discord.Embed(color=0xff0000)
        em.add_field(name='Error', value="Invalid round id")
        await interaction.response.send_message(embed=em)


client.run('你的机器人Token')

关键修改点

  1. 定义custom_skull变量统一管理自定义表情,方便后续修改维护。
  2. 使用列表推导式和reversed(grid)简化Grid文本生成逻辑,代码更简洁易读。
  3. 移除Grid部分的代码块,改用单个反引号包裹每一行,既保留类似代码块的排版效果,又能让Discord正确渲染自定义表情。
  4. 简化响应时间的计算逻辑,无需多余的类型转换。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.23 21:12:10