消息比较返回false

我正在使用Discord.py Rewrite,我想检查用户作出反应的消息是否与我的机器人通过在我的检查功能中使用reaction.message == msg发送的消息相同。由于某些原因,尽管单独打印每个结果都相同,但返回“ False”。我可以通过将每个类型强制转换为字符串来获取返回True的检查,但是我想了解为什么reaction.messagemsg不同在类型强制转换之前。

def check(reaction,user):
    print("Reaction message: ",reaction.message)
    print("Original Message: ",msg)
    print(msg == reaction.message)
    return user != client.user and msg == reaction.message

msg = await ctx.send("test")
await msg.add_reaction('\u2705')
await msg.add_reaction('\u274C')

reaction,user = await client.wait_for('reaction_add',check = check)
prince373 回答:消息比较返回false

尽管两者的字符串表示形式相同,但对象可能(在这里不同)是不同的。您可能要检查消息的字符串内容:

def step(self,joint_values):

    self.iterator += 1

    self._coll = self.observation_callback_collision()
    coll_force = self._coll.wrench.force.x


    while i<=1000:
      if coll_force >= 150 or coll_force <= -150:
            self.reset_robot2.call()
            self.reward_coll = -0.5
            self.collided += 1
            print("#############Collision##############")
            rospy.sleep(1)

      else:
            self.traj.header.stamp = rospy.Time.now()
            action = JointTrajectoryPoint()
            action.positions = joint_values
            #print("W",action.positions)
            action.time_from_start = rospy.Duration(0.7)
            self.traj.points = []
            self.traj.points.append(action)
            self.action_pub.publish(self.traj)

      i +=0.1

更好的解决方案是改为比较消息ID:

    return user != client.user and str(msg) == str(reaction.message)
    # or
    return user != client.user and msg.content == reaction.message.content

docs

中的更多信息
本文链接:https://www.f2er.com/3052099.html

大家都在问