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

Discord机器人Embed随机添加表情反应问题求助

Discord机器人随机添加表情反应问题修复

问题根源

你的代码存在三个关键问题导致随机表情没生效:

  1. 随机反应的代码块没有放到playbs命令函数内部,单独的代码不会被触发执行
  2. 条件判断逻辑错误:random.choice返回的是单个字符串(比如"up"),但你用它和列表["up"]做相等判断,永远不会匹配成功
  3. 添加表情的格式错误:add_reaction不支持:green_square:这种格式,需要使用实际的emoji字符或者自定义表情的完整标识

修正后的完整代码

import random
import discord
from discord.ext import commands

@bot.command(pass_context=True)
async def playbs(self, ctx):
    embed = discord.Embed(title="**BEATSABER**",
                          colour=discord.Colour.dark_red())

    embed.add_field(
        name="*HOW TO PLAY:*",
        value=
        "Under this message is many reacted emojis. In the middle you have 4 arrows. 1 arrow that points up 1 that ponts down and 2 that points on left and right."
    )
    embed.set_footer(text="Made by: Jellie")
    embed.set_thumbnail(
        url=
        "https://i.imgur.com/keLKx3H.png"
    )

    msg = await ctx.reply(embed=embed)
    # 添加固定表情
    await msg.add_reaction("<:gray_cube1:1000731095437934672>")
    await msg.add_reaction("<:up:959720228407894056>")
    await msg.add_reaction("<:right:959720161156407308>")
    await msg.add_reaction("<:left:959720190730436608>")
    await msg.add_reaction("<:down:959720112808673370>")
    await msg.add_reaction("<:gray_cube2:1000731117445464125>")

    # 随机添加绿色方块表情
    # 若使用自定义绿色方块表情,替换为<:green_square:你的表情ID>格式即可
    await msg.add_reaction("🟩")

额外说明

  • 如果你的绿色方块是服务器自定义表情,需要和灰色方块一样使用<:表情名:表情ID>的完整标识
  • 若需要实现「随机给某一个箭头添加绿色方块」的逻辑,可以补充对应映射关系,比如:
    arrow_emojis = {
        "up": "<:up:959720228407894056>",
        "left": "<:left:959720190730436608>",
        "right": "<:right:959720161156407308>",
        "down": "<:down:959720112808673370>"
    }
    target_arrow = random.choice(list(arrow_emojis.values()))
    await msg.add_reaction(target_arrow)
    await msg.add_reaction("🟩")
    

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.20 14:30:53