簡體   English   中英

如何讓 discord.py bot 隨機發送消息

[英]How to make discord.py bot send messages at random times

我正在嘗試讓我的 discord 機器人隨機向頻道發送消息,這是我到目前為止的代碼:

@client.event
async def on_message(message):

    if (random.randint(0,500) == 42) and (message.author != client.user):
        await message.channel.send("bruh")
    
    elif (random.randint(0,500) == 69) and (message.author != client.user):
        await message.channel.send("good plan")

    elif (random.randint(0,500) == 99) and (message.author != client.user):
        await message.channel.send("awwww hellll nahhhhhhh")

    elif (random.randint(0,500) == 76) and (message.author != client.user):
        await message.channel.send("I think we should see other people...")

    elif (random.randint(0,500) == 25) and (message.author != client.user):
        await message.channel.send("no.")
    
    elif (random.randint(0,500) == 56) and (message.author != client.user):
        await message.channel.send("bad idea")
    
    else:
        return

每次我用這段代碼運行機器人時,我的命令都不起作用。 我應該怎么辦?

如果您有 on_message 事件,則必須在事件結束時添加await client.process_commands(message) ,如下所示:

@client.event
async def on_message(message):

    if (random.randint(0,500) == 42) and (message.author != client.user):
        await message.channel.send("bruh")
    
    elif (random.randint(0,500) == 69) and (message.author != client.user):
        await message.channel.send("good plan")

    elif (random.randint(0,500) == 99) and (message.author != client.user):
        await message.channel.send("awwww hellll nahhhhhhh")

    elif (random.randint(0,500) == 76) and (message.author != client.user):
        await message.channel.send("I think we should see other people...")

    elif (random.randint(0,500) == 25) and (message.author != client.user):
        await message.channel.send("no.")
    
    elif (random.randint(0,500) == 56) and (message.author != client.user):
        await message.channel.send("bad idea")
    
    else:
        return

    await client.process_commands(message)

或者你可以像這樣使用@client.listen()裝飾器:

@client.listen()
async def on_message(message):

    if (random.randint(0,500) == 42) and (message.author != client.user):
        await message.channel.send("bruh")
    
    elif (random.randint(0,500) == 69) and (message.author != client.user):
        await message.channel.send("good plan")

    elif (random.randint(0,500) == 99) and (message.author != client.user):
        await message.channel.send("awwww hellll nahhhhhhh")

    elif (random.randint(0,500) == 76) and (message.author != client.user):
        await message.channel.send("I think we should see other people...")

    elif (random.randint(0,500) == 25) and (message.author != client.user):
        await message.channel.send("no.")
    
    elif (random.randint(0,500) == 56) and (message.author != client.user):
        await message.channel.send("bad idea")
    
    else:
        return

如果您使用 listen() 事件,則不必處理命令

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM