2017-06-05 99 views
-1

我試圖讓用戶激活test,它會在聊天室中顯示一個測試菜單,並且如果同一用戶發送任何消息的反應,它會刪除測試菜單:如何在不同功能之間共享對象

import discord, asyncio 

client = discord.Client() 

@client.event 
async def on_message(message): 
    if message.content.lower().startswith('test'): 
     menu = await client.send_message(message.channel, "test menu") 

@client.event 
async def on_reaction_add(reaction, user): 
    if user == message.author: 
     await client.delete_message(menu) 

client.run('ClientToken') 

但是,代碼不是讀取的對象等來自其它功能message.authormenu

回答

1

您可以創建一個字典來保存消息和作者的值。

test = {'message': None, 'author': None} 

@client.event 
async def on_message(message): 
    if message.content.lower().startswith('test'): 
     menu = await client.send_message(message.channel, "test menu") 
     test['message'] = menu 
     test['author'] = message.author 

@client.event 
async def on_reaction_add(reaction, user): 
    if user == test['author']: 
     await client.delete_message(test['message']) 
+0

看來,測試應該是* global *,對嗎? – rth