如何让您的 Discord 机器人在其上一条消息中添加一串文本?

问题描述 投票:0回答:1

我需要我的 Discord 机器人编辑他们自己之前的消息,添加一串文本。但我不明白如何实现这一点。我正在使用discord.py。

我的 Discord 机器人用于托管大型战略视频游戏的预订。机器人通过消息将 .txt 文件的内容发送到频道,其中包含可玩国家的列表。然后,用户可以执行“!保留(x 国家)”,机器人将继续更新其列表,将该用户的姓名添加到列表中,位于他们选择的(x 国家)旁边。

但是,我不明白如何让机器人进行编辑,然后将文本字符串添加到它之前发送的消息中。我查看了discord.py 文档并浏览了stackoverflow,但没有找到任何解决此问题的内容。此外,我需要对机器人进行编程,将用户的名字放在列表中用户选择的“国家”之后。

例如:

机器人消息:

可玩国家列表
美国
意大利
...

upon command '!reserve USA'

机器人消息(已编辑):

可玩国家列表
美国@用户
意大利
...

关于代码;我设置了一个函数来获取机器人原始消息的消息 ID。只是之后的过程让我无法理解。

这是代码:

import discord
from discord import app_commands
from discord.ext import commands
from discord.utils import get

intents = discord.Intents.all()
client = commands.Bot(command_prefix='!', intents = intents)
tree = app_commands.CommandTree

@client.event
async def on_ready():
    print("The bot is now ready")
    try:
        synced = await tree.sync(guild=discord.Object(id=GUILD_ID))
        print(f"Synced {len(synced)} command(s)")
    except Exception as e:
        print(e)

@client.command()
async def host(ctx):
    with open('host-template.txt', 'r') as fp:
        message = await ctx.send(fp.read())
    with open('message.txt', 'w') as f:
        f.write(str(message.id))

@client.command()
async def reserve_USA(ctx):
    channel = client.get_channel(CHANNEL_ID)
    with open("message.txt", "r") as f:
        message_id = int(f.read())
    message = await channel.fetch_message(message_id)
    await message.edit(content="CONTENT") #This basic message.edit function is not feasible.



client.run(BOTTOKEN)

这是“host-template.txt”的内容:

Available Nations

USA
Japan
UK
USSR
Germany
France
Italy
python python-3.x discord discord.py
1个回答
0
投票

我认为您最好使用嵌入来实现此目的。嵌入具有可以轻松操作的字段。对于每个国家/地区,您可以添加一个字段,然后在该字段下添加一个人。此外,它看起来也会更吸引眼球。

首先,您必须从

Available Nations
中删除
host-template.txt
并使其仅包含国家/地区。然后,您可以 .
readlines()
,这将返回每行的列表。

with open("host-template.txt") as f:
    countries = f.readlines() # countries will now be ['USA', 'Japan', ...] 

您需要将国家/地区保持在不同的位置才能使其发挥作用。

然后,您可以使用

discord.Embed
构建一个嵌入,然后为每个国家/地区添加一个新字段。例如:

country_embed = discord.Embed(title="Available Nations")
for country in countries:
   country_embed.add_field(name=country, value="N/A") # we can edit the N/A later with our list of members

.send()
有一个
embed
kwarg,为了发送嵌入,我们将在那里传递我们的嵌入实例。

await ctx.send(embed=country_embed)

然后,当您编辑它时,您可以获取消息并访问

message.embeds
,这是一个列表。由于消息中只有一个嵌入,因此我们通过索引 0 来获取该嵌入。

message = await channel.fetch_message(message_id)
country_embed = message.embeds[0]

现在,我们得到了需要编辑的字段。

Message.embeds
是一个列表,因此我们将使用索引来获取我们想要的字段。这些字段将按照国家/地区在
host-template.txt
中的顺序创建,因此如果我们想要访问第二个
Japan
,我们将从中减去 1,因为索引从 0 而不是 1 开始。因此, 2 - 1 = 1.

japan_field = country_embed.fields[1]
# Now, we will remove the field and add it again using `Embed.insert_field_at`.
e.remove_field(1)
# We need to check if the value is "N/A" (no users) or if there are users
new_value = ""
if japan_field.value == "N/A": # There is no user so we can replace it
    new_value = "<your_new_value>"
else: # There are users logged, so we add to it
    new_value = f"{japan_field.value}\n" + "<your_new_value>"
# Now, we insert the field at the same index
country_embed.insert_field_at(1, name=japan_field.name, value=new_value)
# And now, edit the message with the new embed
await message.edit(embed=country_embed)

这可能很难理解,如果您有任何疑问,可以在下面评论。

参考资料:

© www.soinside.com 2019 - 2024. All rights reserved.