discord.py嵌入消息未更新

时间:2020-10-05 10:14:53

标签: discord.py embed

对不起,我的英语^^

我正在用python做一个不和谐的机器人(我是python的初学者),并且正在一步一步地做。我在这里获得了很大的帮助,如果可能的话,我再次想要一点帮助:)

我不知道如何正确执行操作,但是我尝试使用@用户单击表情符号来编辑嵌入内容。如果有人单击“坦克笑脸”,则会编辑“坦克”字段中的嵌入内容,我希望可以填充5个字段(1个战车,1个治疗,3 dps)。

告诉我是否有可能,如果我尝试使用discord.edit正确吗?

谢谢:)

# bot.py
import os
from discord.ext import commands
from discord.ext.commands import Bot
import discord
from discord import Embed, Emoji
from dotenv import load_dotenv
import random
import asyncio

load_dotenv()
TOKEN = os.getenv('DISCORD_TOKEN')
GUILD = os.getenv('DISCORD_GUILD')

# On définit le préfixe
bot = commands.Bot(command_prefix='!')

# On print les infos après le lancement du bot et
# on affiche un message d'activité pour le bot.
@bot.event
async def on_ready():
    for guild in bot.guilds:
        if guild.name == GUILD:
            break

    print(
        f'{bot.user} is connected to the following guild:\n'
        f'{guild.name}(id: {guild.id})'
    )
    await bot.change_presence(activity = discord.Activity(
                          type = discord.ActivityType.watching,
                          name = 'Sylàn être mauvais'))

# Commande mm+ dans un embed
@bot.command()
async def mm(ctx, arg, arg2, arg3, help="permet de créer une invit mm+ avec !mm clé niveau heure. Exemple: !mm Boralus +14 21h00"):
      embed = discord.Embed(title="Inscription pour {} en {} vers {}" .format(arg, arg2, arg3), description="Composition du groupe") #,color=Hex code
      embed.add_field(name="Tank", value="<:tank:761252435720667157>tank\n", inline = False)
      embed.add_field(name="Heal", value="<:heal:761252937548169246>heal\n", inline = False)
      embed.add_field(name="Dps1", value="<:dps:761252937066217512>dps1\n", inline = False)
      embed.add_field(name="Dps2", value="<:dps:761252937066217512>dps2\n", inline = False)
      embed.add_field(name="Dps3", value="<:dps:761252937066217512>dps3\n", inline = False)

      sent = await ctx.send(embed=embed)
      emojis=['<:tank:761252435720667157>', '<:heal:761252937548169246>', '<:dps:761252937066217512>']
      for emoji in emojis:
            await sent.add_reaction(emoji)
             
@bot.event
async def on_raw_reaction_add(payload):
    channel = bot.get_channel(payload.channel_id)
    message = await channel.fetch_message(payload.message_id)
    if message.author != payload.member:
        return

    reaction = discord.utils.get(message.reactions, emoji=payload.emoji.name)
    embed  = message.embeds[0]
    tank = bot.get_emoji(761252435720667157)
    heal = bot.get_emoji(761252937548169246)
    dps = bot.get_emoji(761252937066217512)

    if reaction == 'tank':
        embed.set_field_at(1, name='Tank', value='Modified value')
    elif reaction == 'heal':
        embed.set_field_at(2, name='Heal', value='Modified value')
    elif reaction == 'dps':
        embed.set_field_at(3, name='Dps', value='Modified value')    
    await message.edit(embed=embed)


#Message d'erreur si mauvaise utilisation
@mm.error
async def mm_error(ctx, error):
    if isinstance(error, commands.MissingRequiredArgument):
                await ctx.send("Merci d'utiliser le bot avec !mm clé niveau heure. Exemple: !mm Boralus +14 21h00")
# EOF

bot.run(TOKEN)

1 个答案:

答案 0 :(得分:0)

使用Message.edit()方法使您处在正确的轨道上。
由于您只需要一些代码,因此下面是您要执行的操作的一个简单示例:

@bot.command()
async def send_embed(self, ctx):
    embed = (Embed(name='A nice embed')
             .add_field(name='Field 1', value='Value 1')
             .add_field(name='Field 2', value='Value 2'))
    emojis = ['1️⃣', '2️⃣']
    message = await ctx.send(embed=embed)
    for emoji in emojis:
        await message.add_reaction(emoji)

@bot.event
async def on_raw_reaction_add(self, payload):
    if payload.member.bot:
        return
    channel = bot.get_channel(payload.channel_id)
    message = await channel.fetch_message(payload.message_id)
    embed, emojis  = message.embeds[0], ['1️⃣', '2️⃣']
    index = emojis.index(payload.emoji.name)

    embed.set_field_at(index, name=embed.fields[index].name, value=payload.member.mention, inline=False)
    await message.edit(embed=embed)

on_raw_reaction_add的优点是您不再需要while循环,但是,如果您对任何消息添加响应,则会触发该循环,因此您需要存储该消息id并进行比较。您决定自己喜欢什么:)

参考文献: