在 Python 中,如何让 discord bot 在特定时间发送 3 条不同的消息,只发送一次,而不与我的主代码冲突?
In Python, how to make discord bot send 3 different message at the specific time, only once, without conflicting my main code?
问题是我想将 'send message' 命令集成到现有代码中,然后与主代码发生冲突。
我已经尝试 'loop' 消息,例如:等待 5 分钟,然后每 100 分钟发送 'Your message.' 但它进入循环并且不加载我的主代码。
切换顺序结果:
'loop-message' 主代码之前的代码:
在我的不和谐频道收到我想要的消息后,我按 ctrl^C 中止,只有在那之后我的机器人开始加载但加载后最终由于中止再次停止。 $bb 命令从不工作,因为主代码没有加载。
'loop-message' 主代码后的代码:
我的机器人自动加载,运行完美但从未加载 'loop-message'。当我按 ctrl^C 时,我收到 'coroutine was never awaited.' 错误。我从来没有收到消息,因为 'loop-message' 没有加载。
我需要什么?
我需要同时 运行 这两个命令而不会发生冲突,机器人回答我的问题并在 19.30 发送自动消息,然后再次,机器人可以回答我的问题。
我希望机器人发送多条延迟消息,例如 msg1 发送于 19.30 msg2 发送于 19.33,仅在指定时间发送一次同时不中断其回答“$bb message”命令的能力。
这是我的代码,其中没有 'loop-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")
尝试使用线程,示例如下
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!")
问题是我想将 'send message' 命令集成到现有代码中,然后与主代码发生冲突。
我已经尝试 'loop' 消息,例如:等待 5 分钟,然后每 100 分钟发送 'Your message.' 但它进入循环并且不加载我的主代码。
切换顺序结果:
'loop-message' 主代码之前的代码: 在我的不和谐频道收到我想要的消息后,我按 ctrl^C 中止,只有在那之后我的机器人开始加载但加载后最终由于中止再次停止。 $bb 命令从不工作,因为主代码没有加载。
'loop-message' 主代码后的代码: 我的机器人自动加载,运行完美但从未加载 'loop-message'。当我按 ctrl^C 时,我收到 'coroutine was never awaited.' 错误。我从来没有收到消息,因为 'loop-message' 没有加载。
我需要什么? 我需要同时 运行 这两个命令而不会发生冲突,机器人回答我的问题并在 19.30 发送自动消息,然后再次,机器人可以回答我的问题。
我希望机器人发送多条延迟消息,例如 msg1 发送于 19.30 msg2 发送于 19.33,仅在指定时间发送一次同时不中断其回答“$bb message”命令的能力。
这是我的代码,其中没有 'loop-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")
尝试使用线程,示例如下
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!")