首页 > 解决方案 > 不和谐蟒蛇多次狙击

问题描述

我有一个不和谐的蟒蛇狙击机器人,但他们只狙击一次。我希望它能够多次狙击。我想用 deque 来做,但我不知道怎么做,因为我是 Python 新手。

import discord
from discord.ext import commands
    
bot.sniped_messages = {}


@bot.event
async def on_message_delete(message):
    bot.sniped_messages[message.guild.id] = (message.content, message.author, message.channel.name, message.created_at)

@bot.command()
async def snipe(ctx):
    try:
        contents, author, channel_name, time = bot.sniped_messages[ctx.guild.id]
        
    except:
        await ctx.channel.send("er is geen verwijderde bericht Dombo!")
        return

    embed = discord.Embed(description=contents, color=discord.Color.red(), timestamp=time)
    embed.set_author(name=f"{author.name}#{author.discriminator}", icon_url=author.avatar_url)
    embed.set_footer(text=f"Deleted in : #{channel_name}")

    await ctx.channel.send(embed=embed)

bot.run(TOKEN)

标签: pythondiscord.py

解决方案


使用列表保存所有已删除的消息:

import discord
from discord.ext import commands


bot = commands.Bot(prefix="-")
sniped_messages = {}


@bot.event
async def on_message_delete(message):
    history = sniped_messages.get(message.guild.id, [])
    history.insert(0, message)
    sniped_messages[message.guild.id] = history

@bot.command()
async def snipe(ctx, position):
    try:
        message = sniped_message[ctx.guild.id][position-1]
        
    except (KeyError, IndexError):
        await ctx.channel.send("er is geen verwijderde bericht Dombo!")
        return

    embed = discord.Embed(
        description=message.content,
        color=discord.Color.red(),
        timestamp=message.created_at
    )
    embed.set_author(name=f"{message.author}", icon_url=message.author.avatar_url)
    embed.set_footer(text=f"Deleted in : {message.channel}")

    await ctx.channel.send(embed=embed)

bot.run(TOKEN)

但我建议您#logs在公会中创建一个频道,您的机器人将发送所有已删除的消息:

import discord
from discord.ext import commands


MY_GUILD_ID = 623204625251827724
LOGS_CHANNEL_ID = 626136590305198113
bot = commands.Bot(prefix="-")


@bot.event
async def on_message_delete(message):
    if message.guild.id != MY_GUILD_ID:
        return
    embed = discord.Embed(
        description=message.content,
        color=discord.Color.red(),
        timestamp=message.created_at
    )
    embed.set_author(name=f"{message.author}", icon_url=message.author.avatar_url)
    embed.set_footer(text=f"Deleted in : {message.channel}")
    logs = bot.get_channel(LOGS_CHANNEL_ID)
    await logs.send(embed=embed)

bot.run(TOKEN)

推荐阅读