简体   繁体   English

我如何从另一个函数获取消息 ID

[英]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.

相关问题 如何通过 ID 获取消息? - How can I get a message by the ID? 如何从机器人使用不和谐发送的消息中获取 ID - How can I get the id from message a bot sent using discord 我怎样才能从一个 function 中的另一个 function 中的 Django 中获取价值,views() - How can I get value from one function in another function in Django, views() 为什么我无法使用 ctx.channel.fetch_message(message.id) 从频道获取消息? - Why can't I get a message from a channel using ctx.channel.fetch_message(message.id)? 如何从@client.command function 获取一个值并将其实现到另一个 - How can I get one value from a @client.command function and implement it into another 如何从 another.py 文件中获取 function 和 class 中的变量 - How can I get a variable that is in a function and a class from another .py file 如何从ctypes中的.dll接收回调函数消息? - How can I receive Callback function message from .dll in ctypes? 如何在另一个函数中使用函数的结果? - How can I use the result from a function in another function? 如何从whatsapp cloud api中的消息ID获取消息对象? - how to get message object from message id in whatsapp cloud api? 如何从另一个 function 停止 function? - How can I stop a function from another function?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM