Discord.py 狙击命令
Posted
技术标签:
【中文标题】Discord.py 狙击命令【英文标题】:Discord.py Snipe command 【发布时间】:2021-01-30 15:26:20 【问题描述】:我试图发出一个命令,让机器人“狙击”最后一条删除的消息。这是我当前的代码:
snipe_message_content = None
snipe_message_author = None
@client.event
async def on_message_delete(message):
snipe_message_author.remove(None)
snipe_message_content.remove(None)
snipe_message_content.append(message.content)
snipe_message_author.append(message.author.id)
await asyncio.sleep(str(60))
snipe_message_author.remove(message.author.id)
snipe_message_content.remove(message.content)
@client.command()
async def snipe(message):
if snipe_message_content==None:
await message.channel.send("Theres nothing to snipe.")
else:
embed = discord.Embed(description=f"snipe_message_content")
embed.set_footer(text=f"Asked by message.author.name#message.author.discriminator", icon_url=message.author.avatar_url)
embed.set_author(name= f"<@snipe_message_author>")
await message.channel.send(embed=embed)
return
await message.channel.send("Theres nothing to snipe.")
部分工作得很好,但其余部分不起作用。有人可以帮忙吗?
【问题讨论】:
【参考方案1】:您的命令现在可能有效,但有一个问题。如果我在服务器中删除了一条消息,而您在服务器中运行该命令,您可能会看到该消息。
您应该做的是制作snipe_message_author
和snipe_message_content
变量字典。
活动应该是这样的:
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
await sleep(60)
del snipe_message_author[message.channel.id]
del snipe_message_content[message.channel.id]
@client.command(name = 'snipe')
async def snipe(ctx):
channel = ctx.channel
try: #This piece of code is run if the bot finds anything in the dictionary
em = discord.Embed(name = f"Last deleted message in #channel.name", description = snipe_message_content[channel.id])
em.set_footer(text = f"This message was sent by snipe_message_author[channel.id]")
await ctx.send(embed = em)
except KeyError: #This piece of code is run if the bot doesn't find anything in the dictionary
await ctx.send(f"There are no recently deleted messages in #channel.name")
#If the bot sends the embed, but it's empty, it simply means that the deleted message was either a media file or another embed.
总而言之,这是为您解决的问题:
不显示来自其他服务器的已删除邮件 不显示来自其他频道的已删除消息 一台服务器中的已删除邮件不会替换另一台服务器中已删除的邮件希望这有帮助:)
【讨论】:
【参考方案2】:您的on_message_delete()
功能无法正常工作。
我会将您的变量缩短为 smc
(snipe_message_content) 和 sma
(snipe_message_author)。
首先,您的变量sma
和smc
属于None
类型,但方法remove
和append
属于list
类型,因此您必须声明列表
smc = []
sma = []
为了让他们工作。
不过,无论如何您都不必这样做。只需给您当前的变量一个新值:
snipe_message_content = None
snipe_message_author = None
@client.event
async def on_message_delete(message):
global snipe_message_content
global snipe_message_author
# Variables outside a function have to be declared as global in order to be changed
snipe_message_content = message.content
snipe_message_author = message.author.id
await asyncio.sleep(60)
snipe_message_author = None
snipe_message_content = None
此外,您不应将 60 转换为字符串。 time.sleep
和 asyncio.sleep
都需要 integer
才能工作。 (顺便说一句,如果您希望 60 成为字符串,只需将 "60"
写成带引号即可。
另外,请注意以下情况:如果一条消息被删除,但在新消息被删除 50 秒后,sma
和 smc
将被分配给新消息。但是 10 秒后,之前消息执行的函数会将 sma
和 smc
的值设置为 None。
因此,在await asyncio.sleep(60)
之后检查您的消息是否仍然与之前相同:
snipe_message_content = None
snipe_message_author = None
snipe_message_id = None
@client.event
async def on_message_delete(message):
global snipe_message_content
global snipe_message_author
global snipe_message_id
snipe_message_content = message.content
snipe_message_author = message.author.id
snipe_message_id = message.id
await asyncio.sleep(60)
if message.id == snipe_message_id:
snipe_message_author = None
snipe_message_content = None
snipe_message_id = None
【讨论】:
谢谢 sm 成功了!感谢您向我解释一切,而不仅仅是给我代码。它帮助我更多地理解这一点。以上是关于Discord.py 狙击命令的主要内容,如果未能解决你的问题,请参考以下文章