【问题标题】:Discord.py snipe command is sniping messages from other channelsDiscord.py 狙击命令正在从其他渠道狙击消息
【发布时间】:2021-10-19 15:25:43
【问题描述】:

我最近为我的不和谐机器人做了一个狙击命令,但我遇到了一个问题。例如,当我想要截取一般消息时,狙击命令会从其他渠道截取消息。这是我的代码:

@bot.event
async def on_message_delete(message):
    if message.attachments:
        bob = message.attachments[0]
        bot.sniped_messages[message.guild.id] = (bob.proxy_url, message.content, message.author, message.channel.name, message.created_at)
    else:
        bot.sniped_messages[message.guild.id] = (message.content,message.author, message.channel.name, message.created_at)

@bot.command(aliases=['s'])
async def snipe(ctx):
    try:
        bob_proxy_url, contents,author, channel_name, time = bot.sniped_messages[ctx.guild.id]
    except:
        contents,author, channel_name, time = bot.sniped_messages[ctx.guild.id]
    try:
        embed = discord.Embed(description=contents , color=discord.Color.purple(), timestamp=time)
        embed.set_image(url=bob_proxy_url)
        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)
    except:
        embed = discord.Embed(description=contents , color=discord.Color.purple(), 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)

【问题讨论】:

  • 然后将channel 设为必需参数并从那里狙击它?

标签: discord discord.py


【解决方案1】:
snipe_message_author = {}
snipe_message_content = {}

@client.event
async def on_message_delete(message):
     snipe_message_author[message.channel.id] = message.author
     snipe_message_content[message.channel.id] = message.content

@client.command(name = 'snipe')
async def snipe(ctx):
    channel = ctx.channel
    try:
        em = discord.Embed(description = f"`{snipe_message_content[channel.id]}`\nMessage sent by {snipe_message_author[channel.id]}!", color = 0x00c230)
        em.set_author(name = f"Last deleted message in #{channel.name}")
        em.set_thumbnail(url="https://cdn.discordapp.com/avatars/352793093105254402/8a2018de21ad29973696bfbf92fc31cd.png?size=4096")
        em.set_footer(text = f"Snipe requested by {ctx.message.author}")
        await ctx.send(embed = em)
    except:
     embed = discord.Embed(colour = 0x00c230)
     embed.set_author(name=f"There are no deleted messages in #{ctx.channel}!")
     embed.set_thumbnail(url="https://cdn.discordapp.com/avatars/352793093105254402/8a2018de21ad29973696bfbf92fc31cd.png?size=4096")
     embed.set_footer(text=f"Snipe requested by {ctx.message.author}")
     await ctx.channel.send(embed=embed)

这就是我使用的,它工作正常。 由于我将 [message.channel.id] 添加到变量中,因此它仅检查该频道中的狙击手。 This is how it looks like in Discord

【讨论】:

    猜你喜欢
    • 2021-01-30
    • 2021-07-30
    • 2021-10-26
    • 2021-02-27
    • 2020-11-23
    • 2021-04-21
    • 2019-03-01
    • 1970-01-01
    • 2021-07-31
    相关资源
    最近更新 更多