在我的機器人中,我有一個函數,它每秒不斷地向字典中的值添加一次,以表示“支付員工”。我還有一個 @client.command 函數,它在發送命令時顯示字典的鍵和值。雖然“支付”功能(始終)處于打開狀態,但查找功能不起作用,on_message 功能起作用。我該怎么做才能使機器人并行檢測@client.command 函數?import discordfrom discord.ext import commandsfrom discord.ext import tasksimport asyncioclient = commands.Bot(command_prefix='/')payroll = []data = {}# /start will start operations and manage payroll for everyone on the server# will make it [email protected] def on_ready(): global data server = client.get_guild(670006903815929889) # starts balance for everyone on the server at the time of the command and adds them to the payroll for person in server.members: person = str(person) payroll.append(person) data[person] = int(0) print(data) await asyncio.sleep(1) # pays everyone at a steady BASE rate while True: for person in payroll: data[person] += 10 print(data) await asyncio.sleep(60)# on [email protected] def on_message(ctx): global data balance = int(data[str(ctx.author)]) data[str(ctx.author)] -= 5 if balance <= 25 and ctx.author.id != 734119994761150585: await ctx.channel.send('Be careful {author}! Your balance is ${balance}.') if balance <= 0 and ctx.author.id != 734119994761150585: # delete message await ctx.channel.send('Oops! {author}, you have run out of money. As a consequence, you will be deducted $15.') data[str(ctx.author)] -= 15 if message.startswith("/dm"): await message.author.send('Hello World!')# look-up [email protected]()async def dm(message): await message.author.send('stats go here')client.run("token")
1 回答

蝴蝶不菲
TA貢獻1810條經驗 獲得超4個贊
命令功能將不起作用,因為您沒有處理命令。為此,請在消息函數中添加小行:
@client.event()
async def on_message(message):
#YOUR CODE HERE
await client.process_commands(message)
對于持續運行函數或一次運行多個:您可以在函數中添加將持續運行或在給定時間差運行的任務。深入的文檔可以在 discord.py 文檔中找到。
代碼片段:
from discord.ext import tasks
@tasks.loop(seconds = 5)
async def test_function():
#YOUR CODE
pass
@client.event
async def on_ready():
#TASK RUN
test_function.start()
添加回答
舉報
0/150
提交
取消