所以我一直在用 python 开发我的不和谐机器人,我发现了另一个可以执行粘性消息的机器人。这基本上意味着嵌入或消息将始终保留在底部,而第一个嵌入或消息将被延迟。
我确实写了这段代码:
@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 中的样子。
这个问题很简单。它需要几个步骤。
当然,一旦用户决定不再需要粘性消息,您需要创建其他系统来删除数据库中的某些行。但是,我不会在这里讨论这一点(至少现在),因为它超出了您问题的上下文。
为此,我将使用 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 数据库。
@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")
消息事件用于获取和删除过去的消息以及发送新的置顶消息。
@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
它在数据库方面应该非常相似。您唯一需要更改的是处理斜杠命令的方式。