You need to enable JavaScript to run this app. discord.py开发英雄联盟战队随机器时无法获取消息反应用户列表
优惠活动
大模型
产品
解决方案
定价
更多

discord.py开发英雄联盟战队随机器时无法获取消息反应用户列表

问题根因

1. check函数不生效的原因

  • checkReaction函数没有加return关键字,函数执行完默认返回None,wait_for会认为校验永远不通过
  • checkReaction1的参数不符合wait_for的要求:reaction_add事件的check函数只能接收reaction和user两个入参,你额外加的limit参数无法被自动传入,且逻辑中没有动态获取当前🎲反应的实际参与人数,limit==10的判断永远不成立

2. reactions属性返回空列表的原因

  • 你代码里保存的msg_players1是发送消息时的静态对象,discord.py不会自动同步后续新增的反应数据,需要主动调用接口拉取最新的消息状态
  • 如果你使用的是discord.py v2.x版本,没有开启message_content、reactions、members特权意图的话,也会无法读取反应和用户数据

解决步骤

第一步:开启必要的Discord意图

在Discord开发者后台开启Bot的Server Members Intent、Message Content Intent,同时初始化bot的时候声明意图:

import discord
from discord.ext import commands

intents = discord.Intents.default()
intents.members = True
intents.message_content = True
intents.reactions = True

bot = commands.Bot(command_prefix='!', intents=intents)

第二步:修正校验逻辑与反应获取逻辑

import random

async def scrim2(ctx, bot):
    embed = discord.Embed(
        title='*SORTEIO X5 DOS CRIAS*',
        description='Deseja sortear as lanes? \n ✅: Sim \n ❌: Não',
        colour= discord.Colour.blue()
    )
    await ctx.channel.purge(limit=1)
    msg_scrim = await ctx.send(embed=embed)

    await msg_scrim.add_reaction('✅')
    await msg_scrim.add_reaction('❌')

    # 修正第一个check函数,添加return和消息ID校验避免误触其他消息的反应
    def checkReaction(reaction, user):
        return user != bot.user and str(reaction.emoji) in ['✅', '❌'] and reaction.message.id == msg_scrim.id

    reaction, user = await bot.wait_for('reaction_add', timeout=200, check=checkReaction)
    sortear_lanes = str(reaction.emoji) == '✅'
    
    msg_players = await ctx.send('Invocadores, reajam abaixo: ')
    await msg_players.add_reaction('🎲')

    # 修正10人反应校验逻辑
    def checkReaction1(reaction, user):
        if user == bot.user or str(reaction.emoji) != '🎲' or reaction.message.id != msg_players.id:
            return False
        # 统计当前反应的非Bot用户数
        reacted_users = [u for u in await reaction.users().flatten() if u != bot.user]
        return len(reacted_users) == 10

    # 等待满10人反应
    await bot.wait_for('reaction_add', timeout=200, check=checkReaction1)

    # 重新拉取最新消息,确保拿到所有反应数据
    latest_msg = await ctx.fetch_message(msg_players.id)
    target_reaction = next(r for r in latest_msg.reactions if str(r.emoji) == '🎲')
    all_players = [u for u in await target_reaction.users().flatten() if u != bot.user]
    
    # 调用分组函数传入玩家列表
    await scrim_sorteio(ctx, all_players, sortear_lanes)

第三步:实现随机分组逻辑示例

async def scrim_sorteio(ctx, players, sortear_lanes):
    # 打乱玩家列表
    random.shuffle(players)
    team1 = players[:5]
    team2 = players[5:]
    
    # 输出结果
    embed = discord.Embed(title="分组结果", color=discord.Color.green())
    embed.add_field(name="蓝队", value="\n".join([u.mention for u in team1]), inline=False)
    embed.add_field(name="红队", value="\n".join([u.mention for u in team2]), inline=False)
    await ctx.send(embed=embed)
    
    # 需要分路的话在此处添加对应逻辑即可

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.12 04:48:01