我正在编写一个机器人,当工作人员单击特定消息的响应时,该机器人将给用户赋予Muted角色,但是我有一个问题,我想从另一个函数获取消息ID并检查是否与工作人员已在其中作出反应的消息具有相同的ID。 我该怎么办?
这是我的代码:
class mute(commands.Cog):
def __init__(self,client):
self.client = client
@commands.has_role("Staff")
@commands.command()
async def mute(self,ctx,member:discord.Member=None):
role = discord.utils.get(ctx.guild.roles, name="Muted")
reasons = discord.Embed(title="قم بأختيار سبب الميوت",color=0x00ff00,description="1-\n2-\n3-\n4-")
reasons.set_footer(text=member,icon_url=member.avatar_url)
msg = await ctx.send(embed=reasons)
await msg.add_reaction(str("1️⃣"))
await msg.add_reaction(str("2️⃣"))
await msg.add_reaction(str("3️⃣"))
await msg.add_reaction(str("4️⃣"))
await msg.add_reaction(str("5️⃣"))
await msg.add_reaction(str("6️⃣"))
await msg.add_reaction(str("7️⃣"))
await msg.add_reaction(str("8️⃣"))
await msg.add_reaction(str("9️⃣"))
await msg.add_reaction(str("?"))
@commands.Cog.listener()
async def on_reaction_add(self,reaction, user):
if user.id == self.client.user.id:
return
if reaction.message.id == self.mute.msg.id:
print("correct message")
答案 0 :(得分:0)
您无法访问函数内部的变量,因为它们是局部变量。您可以使用全局变量,但这确实很昂贵而且很丑陋。那你该怎么办? 您可以在静音的对象上创建一个属性,然后保存消息的对象或ID。
一个例子?
def __init__(self, client):
self.client = client
self.msgs = {}
...
async def mute(self, ctx, ...):
msg = await ctx.send(...)
self.msgs[ctx.author] = msg
...
async def on_reaction_add(self, ctx, ...):
try:
msg = self.msgs[ctx.author]
except KeyError:
return
if ctx.message.id == msg.id:
#correct message