問題描述
我試圖在機器人狙擊"的地方發出命令.最后刪除的消息.這是我當前的代碼:
Im trying to make a command where the bot "snipes" the last deleted message. this is my current code:
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 snip.")
部分可以正常工作,但其余部分無法正常工作.有人可以幫忙嗎?
the await message.channel.send("Theres nothing to snipe.")
part works perfectly fine, but the rest wont work. Can anyone help?
推薦答案
你的 on_message_delete()
函數只是不工作.
Well your on_message_delete()
function is just not working.
我會將您的變量縮短為 smc
(snipe_message_content) 和 sma
(snipe_message_author).
I'll shorten your variables as smc
(snipe_message_content) and sma
(snipe_message_author).
首先,你的變量 sma
和 smc
的類型是 None
,但是方法 remove
和 append
是 list
類型的一部分,所以你必須聲明列表
First of all, your variables sma
and smc
are of the type None
, but the methods remove
and append
are part of the type list
, so you'd have to declare lists
smc = []
sma = []
為了讓他們工作.
不過,無論如何您都不必這樣做.只需給您當前的變量一個新值:
Still, you wouldn't have to do this anyway. Just give your current variables a new value:
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"
寫成帶引號.
Also, you should not convert 60 to a string. time.sleep
and asyncio.sleep
both need an integer
in order to work. (And by the way, if you wanted 60 to be a string, just write "60"
with quotation marks.
另外,請注意以下情況:如果一條消息被刪除,但在新消息被刪除 50 秒后,sma
和 smc
將分配給新消息.但是 10 秒后,之前消息執行的函數會將 sma
和 smc
的值設置為 None.
Also, be careful of the following case: If a message gets deleted, but 50 seconds after a new message gets deleted, sma
and smc
would be assigned to the new message. But 10 seconds later, the function executed by the message before would set he value of sma
and smc
to None.
因此,在 await asyncio.sleep(60)
之后檢查您的消息是否仍然與之前相同:
Therefore, after await asyncio.sleep(60)
check wether your message is still the same as before:
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
這篇關于Discord.py 狙擊命令的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!