gpt4 book ai didi

python - Discord Python Bot 在使用后台任务时破坏我的命令

转载 作者:行者123 更新时间:2023-12-04 07:37:34 26 4
gpt4 key购买 nike

我正在使用 python 开发一个不和谐的机器人,它每天在非常特定的时间在服务器中发送预定的消息以及其他功能。执行预定消息后,bot 命令不再起作用 - 因此用户不能使用任何命令,但预定消息有效。
我设置了机器人,因此它会在 12:00,19:00,21:00,22:00,23:00 UTC 发送一条消息。唯一的问题是现在诸如“;help”之类的命令不起作用。

import discord
import datetime
import asyncio
from discord.ext import commands
from discord.utils import get

client = commands.Bot(command_prefix = ';',help_command=None) #prefix

race_times_utc = [12,19,21,22,23]
time = datetime.datetime.now

async def timer():

await client.wait_until_ready()

msg_sent = False

while not client.is_closed():
if any(time().hour == race for race in race_times_utc) and time().minute == 0:
if not msg_sent:
channel = client.get_channel(838626115326636022)
racer_role = get(channel.guild.roles, name = 'Racers')

embed = discord.Embed(
title = 'Race Time',
description = 'Scheduled reminder for everyone that race starts soon.',
colour = discord.Colour.blue()
)

await channel.send(f'{racer_role.mention}',embed=embed)
msg_sent = True
else:
msg_sent = False

await asyncio.sleep(1)

@client.event
async def on_ready():
await client.change_presence(activity=discord.Activity(type=discord.ActivityType.watching, name=' ;help'))
client.loop.create_task(timer())
print('Bot is alive')

#Functions such as this no longer working
@client.command()
async def help(ctx):
...

最佳答案

您应该使用 discord.ext.tasks因为您的代码的一部分似乎被阻塞了。

import discord
import datetime
import asyncio
from discord.ext import tasks, commands
from discord.utils import get

client = commands.Bot(command_prefix = ';',help_command=None) #prefix

race_times_utc = [12,19,21,22,23]
time = datetime.datetime.now

@tasks.loop(seconds=1.0) # replaces the sleep
async def timer():
msg_sent = False
if any(time().hour == race for race in race_times_utc) and time().minute == 0:
if not msg_sent:
channel = client.get_channel(838626115326636022)
racer_role = get(channel.guild.roles, name = 'Racers')

embed = discord.Embed(
title = 'Race Time',
description = 'Scheduled reminder for everyone that race starts soon.',
colour = discord.Colour.blue()
)

await channel.send(f'{racer_role.mention}',embed=embed)
msg_sent = True
else:
msg_sent = False

@client.event
async def on_ready():
await client.change_presence(activity=discord.Activity(type=discord.ActivityType.watching, name=' ;help'))
print('Bot is alive')
timer.start()

关于python - Discord Python Bot 在使用后台任务时破坏我的命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67660919/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com