如何制作始终保留在频道底部的“粘性消息”。 (Discord.py)

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

所以我一直在用 python 开发我的不和谐机器人,我发现了另一个可以执行粘性消息的机器人。这基本上意味着嵌入或消息将始终保留在底部,而第一个嵌入或消息将被延迟。

sticky embed

我确实写了这段代码:

@bot.event
async def on_message(message):
    if message.channel.id == 1206246422324715580:
        if message.author == bot.user:
            return
        else:
            msg = await message.channel.fetch_message(1208866913426473021)
            new_embed = msg.embeds[0].copy()
            await message.channel.send(embed=new_embed)
            # await new_embed.delete()
    else:
        return

但这并没有延迟旧消息。我正在考虑一些关于数据库和 json 的事情。

我很高兴每一个回复:-)

正如我所说,我想让它看起来像我发送的 gif 中的样子。

python json database discord discord.py
1个回答
0
投票

这个问题很简单。它需要几个步骤。

  1. 将消息 ID、内容和频道 ID 保存到数据库(可以是 JSON,但 SQL 是最好的方法)
  2. 每次发送新消息时,删除旧消息(使用数据库获取旧消息)并将新消息发送到频道。
  3. 发送新消息后,在数据库中保存并更新该新消息 ID。

当然,一旦用户决定不再需要粘性消息,您需要创建其他系统来删除数据库中的某些行。但是,我不会在这里讨论这一点(至少现在),因为它超出了您问题的上下文。

第一步:创建数据库

为此,我将使用 sqllite3 for Python

# --- SQL startup ---
con = sqlite3.connect("sticky_messages.db")  # connect to the database
cur = con.cursor()  # create the cursor
try:
    cur.execute("CREATE TABLE sticky(guild_id, channel_id, message_id, message)")  # attempt to create the table
    print("Created new sticky table for sticky_messages")
except sqlite3.OperationalError:
    print("Found existing table for sticky")  # if the table already exists

上面的内容非常简单。我们只是在设置 sqqlite3 数据库。

对于 Pycord

创建创建斜杠命令

@bot.slash_command()
async def set_up_sticky_messages(ctx, msg_content: str, channel: discord.TextChannel):
    ref = cur.execute(f"""SELECT * FROM sticky WHERE channel_id=?""", (channel.id,))
    ref_fetch = ref.fetchall()

    if ref_fetch:
        return await ctx.respond("You already have a sticky message setup")

    msg = await channel.send(msg_content)
    cur.execute(f"""INSERT INTO sticky 
    VALUES (?, ?, ?, ?);""", (ctx.guild.id, channel.id, msg.id, msg_content))
    con.commit()  # save to db
    await ctx.respond("Created sticky message")

这是如何运作的:

  1. 我们首先检查数据库中是否已经存在粘性消息条目。如果有,我们会返回错误消息。
  2. 然后我们将消息发送到用户提供的频道中(如果您想在此处使用嵌入,这非常简单)
  3. 我们将相关信息(如上所述)保存到数据库中以供稍后使用

输出:

消息事件

消息事件用于获取和删除过去的消息以及发送新的置顶消息。

@bot.event
async def on_message(msg):
    ref = cur.execute(f"""SELECT * FROM sticky WHERE channel_id=?""", (msg.channel.id,))
    ref_fetch = ref.fetchall()

    if not ref_fetch:  # No record found in database
        return

    channel_id = ref_fetch[0][1]
    message_id = ref_fetch[0][2]
    message_content = ref_fetch[0][3]
    channel = await bot.fetch_channel(channel_id)
    message = await channel.fetch_message(message_id)

    await message.delete()  # delete old message
    new_msg = await channel.send(message_content)  # send new message

    cur.execute("UPDATE sticky SET message_id = ? WHERE channel_id= ?", (new_msg.id, channel_id))
    con.commit()  # update db

这是如何运作的

  1. 我们首先检查消息是否在数据库中。如果不是,我们什么也不返回。
  2. 如果它在数据库中,我们删除旧消息并发送新消息。我们使用新的消息 ID 更新数据库的 ID。

对于 Discord.py

它在数据库方面应该非常相似。您唯一需要更改的是处理斜杠命令的方式。

最终输出

之前:

之后:

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