在Python中,如何让discord bot在特定时间发送3条不同的消息,只发送一次,而不与我的主代码冲突?

2024-10-02 04:25:18 发布

您现在位置:Python中文网/ 问答频道 /正文

问题是我想将“发送消息”命令集成到一个现有代码中,该代码与主代码冲突

我尝试过“循环”消息,例如:等待5分钟,然后每100分钟发送一次“你的消息”,但它进入循环,不加载我的主代码

切换订单结果:

主代码前面的“循环消息”代码: 当我在我的discord频道上收到我想要的消息后,我按ctrl^C中止,直到我的bot开始加载,但加载后最终由于中止而再次停止$bb命令从未工作,因为主代码未加载

主代码后的“循环消息”代码: 我的机器人自动加载,工作正常,但从不加载“循环消息”。当我按住ctrl^C键时,我收到“从未等待过协同程序”错误。我从未收到消息,因为“循环消息”未加载

我需要什么? 我需要在没有冲突的情况下同时运行这两个命令,机器人回答我的问题,并在19:30时发送自动消息,之后机器人仍然可以回答我的问题

我希望bot发送多条延迟消息,例如19.30发送的msg1和19.33发送的msg2,在指定时间仅发送一次,同时不中断其应答“$bb message”命令的能力

以下是我的代码,其中没有“循环消息”代码:

import discord,os

from neuralintents import GenericAssistant

chatbot = GenericAssistant('intents.json')
chatbot.train_model()
chatbot.save_model()

print("Bot is active. . .")

client = discord.Client()

@client.event
async def on_message(message):
    if message.author == client.user:
        return

    if message.content.startswith("$bb"):
        response = chatbot.request(message.content[4:])
        await message.channel.send(response)

client.run("TOKEN")

Tags: 代码import命令client消息messagemodelif
1条回答
网友
1楼 · 发布于 2024-10-02 04:25:18

尝试使用线程,下面是一个示例

import time
import threading
# Only the module 'threading' is needed (the module 'time' is for example purposes)

# function with the loop inside of it
def startloop():
  while True:
    print("loop")
    time.sleep(1)

# Start the function with no arguments
threading.Thread(target=startloop).start()

# Start a thread with arguments
# threading.Thread(target=startloop, args=('arg1', 'arg2')).start()

# make sure the main thread is still running
print("Main thread is still running!")

相关问题 更多 >

    热门问题