Python discord机器人每24小时自动向每个频道发送一次挑战
我是制作discord机器人的新手,我想制作一个机器人,它可以为5种不同的语言(python、java、javascript、c#和html)发送编码挑战。我已经在我的测试discord中设置了通道,并且我已经给了bot管理员能够在通道中读写的权限,等等。我试图弄清楚如何让它在24小时计时器上运行,当计时器达到零时,它会将每种语言的代码挑战发送到相应的discord通道Python discord机器人每24小时自动向每个频道发送一次挑战,python,discord.py,Python,Discord.py,我是制作discord机器人的新手,我想制作一个机器人,它可以为5种不同的语言(python、java、javascript、c#和html)发送编码挑战。我已经在我的测试discord中设置了通道,并且我已经给了bot管理员能够在通道中读写的权限,等等。我试图弄清楚如何让它在24小时计时器上运行,当计时器达到零时,它会将每种语言的代码挑战发送到相应的discord通道 import random import discord from discord.ext import commands
import random
import discord
from discord.ext import commands
bot = commands.Bot(command_prefix='!')
TOKEN = ''
@bot.event
async def on_ready():
print(f'{bot.user.name} has connected to Discord!')
@bot.command()
async def hello(ctx, member: discord.member):
await ctx.send(f"Hello {member.name}")
@bot.command()
async def
bot.run(TOKEN)
到目前为止,我已经能够使机器人联机,并且我已经学会了如何给它自定义命令,但是我不知道从哪里开始我正在尝试做的事情,任何建议或提示都将不胜感激:)TYIA您可以使用discord.ext.tasks进行每24小时运行一次的循环。如果你有帮会对象,你可以在帮会的频道中使用
。频道:
,然后发送消息。您可以使用id获取帮会对象:bot.get\u guild(id)
。总的来说,它看起来是这样的:
import random
import discord
from discord.ext import commands, tasks # Import the tasks module
bot = commands.Bot(command_prefix='!')
TOKEN = ''
@bot.event
async def on_ready():
print(f'{bot.user.name} has connected to Discord!')
challenge.start() # Start the loop
@bot.command()
async def hello(ctx, member: discord.member):
await ctx.send(f"Hello {member.name}")
@tasks.loop(seconds=86400) # Loops every 24h
async def challenge():
guild = await bot.get_guild(id) # Gets the guild object by the id
for channel in guild.channels: # Runs a command in every channel
await channel.send(message) # Says the message
bot.run(TOKEN)
我没有测试这个,它可能不起作用。检查我放入的bot令牌,当我运行它时,它抛出了以下错误:guild=wait bot.get\u guild(id)TypeError:object NoneType不能用在'wait'表达式中抱歉响应太晚,但是,您必须将要向其发送消息的服务器的ID放入bot.get_guild(ID)中。只需将()中的id替换为服务器的id即可。