简体   繁体   English

如何使用 discord 机器人从旧消息/命令中获取内容?

[英]How do I get the content from an old message/command with my discord bot?

I am currently trying to edit an embed with a command.我目前正在尝试使用命令编辑嵌入。 The edited embed has a clear structure.编辑后的嵌入结构清晰。 It is supposed to be an evaluation of the suggestion.它应该是对建议的评估。 For this I need the author and his suggestion from the previous command.为此,我需要作者和他从上一个命令中获得的建议。 Is it possible to take the content of the old embed and transfer it to the edited embed if you have two commands?如果您有两个命令,是否可以获取旧嵌入的内容并将其传输到编辑过的嵌入? In addition, it would be good to still count and insert the reactions at the message.此外,最好仍然计算并在消息中插入反应。 Here are my working approaches, except for the missing parts:这是我的工作方法,除了缺少的部分:

    #@commands.cooldown(1, 100, BucketType.user)
    @commands.command(usage="<text>")
    async def suggest(self, ctx, *, text: str = None):
        """This is the command for suggestions."""
        if text is None:
            await ctx.send("**You need to insert a text.**")
            return self.suggest.reset_cooldown(ctx)

        channel1 = self.bot.get_channel(812284187427864616)
        if channel1 == ctx.channel:

            channel = self.bot.get_channel(812283430707920906)
                e = discord.Embed(color=discord.Colour.green())
                e.description = f "**__submitter:__**\n {ctx.author}"
                e.add_field(name="__Suggestion:__", value=f"{text}")
                e.set_thumbnail(url=ctx.message.author.avatar_url)
                e.timestamp = datetime.utcnow()
                e.set_footer(text=f "UID: {ctx.author.id}")
                feedback = await channel.send(embed=e)
                await feedback.add_reaction("✅")
                await feedback.add_reaction("❌")
                await ctx.message.add_reaction("✅")

The approve command which actually edits the old embed and should insert "Suggestion", "Submitter" and count the reactions.实际编辑旧嵌入的批准命令应插入“建议”、“提交者”并计算反应。

    @commands.command()
    async def approve(self, ctx, msg_id: int = None, channel: discord.TextChannel = None):
        if not msg_id:
            channel = self.bot.get_channel(812283430707920906) # the message's channel
            msg_id = 998877665544332211 # the message's id
        elif not channel:
            channel = ctx.channel
        msg = await channel.fetch_message(msg_id)
        embed = discord.Embed()
        embed.title = "Suggestion accepted"
        embed.description = "*Here are all the important information*"
        embed.add_field(name="Results", value="✅: MISSING COUNT / ❌: MISSING COUNT")
        embed.add_field(name="Suggestion", value=f"MISSING SUGGESTION TEXT")
        embed.add_field(name="Submitter:", value=f"MISSING SUBMITTER")
        embed.add_field(name="Approved by:", value=f"{ctx.author.mention}")
        await msg.edit(embed=embed)
        await msg.clear_reactions()

To count the reactions I would use something like:为了计算反应,我会使用类似的东西:

total_count = 0
for r in message.reactions:
    total_count += r.count

EDIT: This is the embed right now with showing Suggestion two times in different ways.编辑:这是现在以不同方式显示Suggestion两次的嵌入。

    @commands.command()
    async def approve(self, ctx, channel: discord.TextChannel, msgID: int):
        try:
            msg = await channel.fetch_message(msgID)
            embed = msg.embeds[0]
            submitter = embed.description[embed.description.find('\n'):]
            suggestion = embed.fields[0].value

            embed.title = "Suggestion accepted"
            embed.description = "*Here are all the important information*"
            embed.add_field(name="Results", value="✅: Test/ ❌: Test", inline=False)
            embed.add_field(name="Suggestion", value=f"{suggestion}", inline=False)
            embed.add_field(name="Submitter", value=f"{submitter}", inline=False)
            embed.add_field(name="Approved by", value=f"{ctx.author.mention}", inline=False)
            await msg.edit(embed=embed, reference=msgID)
            await msg.clear_reactions()
        except:
            pass

msg = await channel.fetch_message(msg_id) You can use the fetched message to get details about it. msg = await channel.fetch_message(msg_id)您可以使用获取的消息来获取有关它的详细信息。

try:
   msg = await channel.fetch_message(msg_id)
   embed = msg.embeds[0] 
   submitter = embed.description[embed.description.find('/n'):] # get submitter name
   suggestion = embed.fields[0].value
   # other stuff
except:
   pass
   # possible exceptions message not found, list index out of range(if wrong msg id with no embeds passed as arg)

References: fetch_message embeds message参考: fetch_message 嵌入消息

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用Discord机器人,如何从另一个机器人获取嵌入消息的字符串 - Using a Discord bot, how do I get a string of an embed message from another bot 如何修复我的 Discord Bot 中的 datetime 命令? - How do I fix my datetime command in my Discord Bot? 如何让我的机器人从用户运行命令的位置获取通道 ID? (不和谐.py) - How do I make my bot get the channel Id from where the user ran the command from? (Discord.py) 如何让我的机器人仅响应对特定消息的反应? | discord.py - How do I get my bot to respond only to reactions on a specific message? | discord.py 如何让 Discord Bot 说出消息的作者 - How do I get Discord Bot to Say Message's Author 在我的discord机器人键入特定命令后,如何获得所有用户输入的字符串? - How do I get all user input as a string with my discord bot after they type a certain command? 如何让我的 discord 机器人对我的消息做出反应 - How do I get my discord bot to react to my messages 我如何制作使我的机器人离开服务器的命令(discord.py) - How do i make a command that make my bot leave from the server (discord.py) 如何做到这一点,以便我的 discord 机器人只接受启动命令的人的输入 - How do I make it so my discord bot only take inputs from the person that started the command 如何使用 Discord 机器人从 channel.history 获取消息内容? - How do I get message contents from channel.history using a Discord bot?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM