我想创建一个不和谐机器人,每天在特定的时间发送2条消息。下面的代码将使消息进入一个循环,例如每5秒发送一条消息。我如何设置每天发送消息的具体时间,例如,消息1在下午6点,消息2在上午10点。我在这里找到了这段代码,但是没有找到我想要的。
import discord
from discord.ext import commands, tasks
bot = commands.Bot("$")
channel_id = #Any ID
#Message 1
@tasks.loop(seconds=5)
async def called_once_a_day():
message_channel = bot.get_channel(channel id)
await message_channel.send("test 1")
@called_once_a_day.before_loop
async def before():
await bot.wait_until_ready()
print("Finished waiting")
#Message 2
@tasks.loop(seconds=10)
async def called_once_a_day2():
message_channel = bot.get_channel(channel id)
await message_channel.send("test 2")
@called_once_a_day2.before_loop
async def before():
await bot.wait_until_ready()
print("Finished waiting")
called_once_a_day.start()
called_once_a_day2.start()
bot.run('token')
你需要计算现在的时间,和你安排的时间,然后每隔10分钟或更少的时间,你检查计划的时间是否符合现在。
单击此链接查看python的日期时间库https://docs . python . org/3/library/DateTime . html # DateTime . DateTime . now
并查看此链接以查看如何使用@tasks的示例。循环(后台任务)https://github.com/Rapptz/discord.py/blob/master/examples/background_task.py
使用dateTime库获取现在的时间
now=datetime.datetime.now() #will store the time of when it is called
#if you want to put specific time:
remindMeAT =datetime.datetime(2021,5,4,17,24) #year,month,day,hour,min,sec
我创造了一个[不和谐机器人],它在特定的时间发送信息,直到那个时间到来,它将处于睡眠状态。我创建的代码很小,很容易理解< br >代码:
import discord
from datetime import datetime
client = discord.Client()
token = "" #enter your bot's token and it should be a string
channel_id = #enter your channel id and it should be a integer
def time_module():
print("time module in use")
while True:created
current_time = datetime.now().strftime("%H:%M")#hour %H min %M sec %S am:pm %p
if current_time == "00:00": # enter the time you wish
print("time module ended")
break
time_module()
@client.event
async def on_ready():
print("bot:user ready == {0.user}".format(client))
channel = client.get_channel(channel_id)
await channel.send("message")
client.run(token)
这里有一个简单的实现——每天,它都Hibernate到目标时间,然后发送你的消息
from discord.ext import commands
from datetime import datetime, time, timedelta
import asyncio
bot = commands.Bot(command_prefix="$")
WHEN = time(18, 0, 0) # 6:00 PM
channel_id = 1 # Put your channel id here
async def called_once_a_day(): # Fired every day
await bot.wait_until_ready() # Make sure your guild cache is ready so the channel can be found via get_channel
channel = bot.get_channel(channel_id) # Note: It's more efficient to do bot.get_guild(guild_id).get_channel(channel_id) as there's less looping involved, but just get_channel still works fine
await channel.send("your message here")
async def background_task():
now = datetime.utcnow()
if now.time() > WHEN: # Make sure loop doesn't start after {WHEN} as then it will send immediately the first time as negative seconds will make the sleep yield instantly
tomorrow = datetime.combine(now.date() + timedelta(days=1), time(0))
seconds = (tomorrow - now).total_seconds() # Seconds until tomorrow (midnight)
await asyncio.sleep(seconds) # Sleep until tomorrow and then the loop will start
while True:
now = datetime.utcnow() # You can do now() or a specific timezone if that matters, but I'll leave it with utcnow
target_time = datetime.combine(now.date(), WHEN) # 6:00 PM today (In UTC)
seconds_until_target = (target_time - now).total_seconds()
await asyncio.sleep(seconds_until_target) # Sleep until we hit the target time
await called_once_a_day() # Call the helper function that sends the message
tomorrow = datetime.combine(now.date() + timedelta(days=1), time(0))
seconds = (tomorrow - now).total_seconds() # Seconds until tomorrow (midnight)
await asyncio.sleep(seconds) # Sleep until tomorrow and then the loop will start a new iteration
if __name__ == "__main__":
bot.loop.create_task(background_task())
bot.run('token')
我在使用discord。py创建一个discord机器人,我需要每天在特定的时间执行某些操作。我看到了这个答案:如何在不和谐中循环。py重写?到目前为止,我一直在使用它。 当我在heroku免费计划上托管我的机器人时,问题就开始了。Heroku上的服务器每天至少重置一次,这会弄乱计时器,如该帖子所示。 我还看到了日程库。这个的问题是它似乎使用了一个无限循环。这不会阻止我在24小时内运行其他任何东西
我希望我的机器人每天在特定的时间发送一条消息,运行另一个机器人的命令。例如,我想让我的机器人写“s!t"每天凌晨2点在特定频道上,并删除机器人发送的消息。我该怎么做?
我只是想给加入服务器的新成员写一封欢迎信。我想在每次新成员加入时发送嵌入。但是,未发送嵌入。有人能帮我吗? 这是我的代码: 谢谢
我的代码是这个但是输出很糟糕 我的输出: 与不和有关![ 文件"C:\用户\Cinar\untitled0.py",第18行,client.run('Bot Token', bot=False) 在运行_cleanup_loop(循环)中,文件"D:\用户\Cinar\anaconda3\lib\site-包\discord\client.py",第714行。 文件“D:\Users\cinar\
问题内容: 我有这段代码,它每天早上7点运行一个通知,它获取当前日期,然后在到达设定的时间时运行该通知,我的问题是,如果时间已经超过了设定的运行时间,那么它将每天在用户当前时间不是我早上7点的时间,这是我的代码 如您所见,它每天早上7点运行。我不知道,即使时间是早上7点以后,通知在第二天仍将继续运行,将不胜感激 问题答案: 更新了@ Paulw11对Swift 3.0的回答,并包装在一个函数中:
如何要求机器人将消息发送到与机器人接收命令不同的另一个通道(特定通道)? 假设bot在通道中收到消息,如果操作完成,则bot发送给通道。 code: