[英]how can i get the message id from another function
i'm coding a bot that give a Muted role to the user when a staff member click the reaction of a specific the message, but i have a problem that i want to get a message id from another function and check if it is has the same id as the message that the staff member has reacted in it.我正在编写一个机器人,当工作人员单击特定消息的反应时,该机器人为用户提供静音角色,但我有一个问题,我想从另一个功能获取消息 ID 并检查它是否具有与工作人员在其中做出反应的消息的 ID 相同。 how can i do that ?
我怎样才能做到这一点 ?
this is my code:这是我的代码:
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")
You can't access variables inside a function because they are local variables.您无法访问函数内部的变量,因为它们是局部变量。 You could use global variables but that's really expensive and ugly.
您可以使用全局变量,但这真的很昂贵而且很丑陋。 So what could you do?
那你能做什么? You could create an attribute on mute's object and save the messages' object or ID.
您可以在静音对象上创建一个属性并保存消息的对象或 ID。
An example?一个例子?
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.