亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何在 discord.py 中一次運行多個函數?

如何在 discord.py 中一次運行多個函數?

一只斗牛犬 2023-03-16 17:54:59
在我的機器人中,我有一個函數,它每秒不斷地向字典中的值添加一次,以表示“支付員工”。我還有一個 @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()


查看完整回答
反對 回復 2023-03-16
  • 1 回答
  • 0 關注
  • 101 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號