在discord.py中发送循环消息

在discord.py中发送循环消息,discord.py,discord.py-rewrite,Discord.py,Discord.py Rewrite,我想让它一旦你做了命令h!开始时,它每10秒向您发送一条消息,当用户键入h!停止但是,discord bot从不在discord聊天中发送消息。我刚开始学习如何制作discord机器人,如果这是基本的,请原谅。错误在代码下面 import discord import random from discord.ext import commands, tasks from itertools import cycle client = commands.Bot(command_prefix =

我想让它一旦你做了命令h!开始时,它每10秒向您发送一条消息,当用户键入h!停止但是,discord bot从不在discord聊天中发送消息。我刚开始学习如何制作discord机器人,如果这是基本的,请原谅。错误在代码下面

import discord
import random
from discord.ext import commands, tasks
from itertools import cycle


client = commands.Bot(command_prefix = 'h!')

@client.event
async def on_ready():
    await client.change_presence(status=discord.Status.idle, activity=discord.Game('Work In Progress'))
    print('Bot is ready')

@tasks.loop(seconds=10)
async def reminder():
    channel = client.get_channel(797915093954199565)
    await channel.send('It has been 10 seconds')

@client.command()
async def start():
    reminder.start()
    print('Reminder Started')

@client.command()
async def stop():
    reminder.cancel()
    print('Reminder stopped')
错误:

Traceback (most recent call last):
  File "C:\Python39\lib\site-packages\discord\client.py", line 343, in _run_event
    await coro(*args, **kwargs)
  File "C:\Python39\lib\site-packages\discord\ext\commands\bot.py", line 942, in on_message
    await self.process_commands(message)
  File "C:\Python39\lib\site-packages\discord\ext\commands\bot.py", line 939, in process_commands
    await self.invoke(ctx)
  File "C:\Python39\lib\site-packages\discord\ext\commands\bot.py", line 902, in invoke
    await ctx.command.invoke(ctx)
  File "C:\Python39\lib\site-packages\discord\ext\commands\core.py", line 856, in invoke
    await self.prepare(ctx)
  File "C:\Python39\lib\site-packages\discord\ext\commands\core.py", line 790, in prepare
    await self._parse_arguments(ctx)
  File "C:\Python39\lib\site-packages\discord\ext\commands\core.py", line 693, in _parse_arguments
    raise discord.ClientException(fmt.format(self))
discord.errors.ClientException: Callback for start command is missing "ctx" parameter.
Task exception was never retrieved
future: <Task finished name='Task-13' coro=<Loop._loop() done, defined at C:\Python39\lib\site-packages\discord\ext\tasks\__init__.py:88> exception=NameError("name 'channel' is not defined")>
Traceback (most recent call last):
  File "C:\Python39\lib\site-packages\discord\ext\tasks\__init__.py", line 125, in _loop
    raise exc
  File "C:\Python39\lib\site-packages\discord\ext\tasks\__init__.py", line 101, in _loop
    await self.coro(*args, **kwargs)
  File "C:\Users\zedga\source\repos\Hydrate\Hydrate\Hydrate.py", line 16, in reminder
    await channel.send('It has been 10 seconds')
NameError: name 'channel' is not defined
Unhandled exception in internal background task 'reminder'.
Traceback (most recent call last):
  File "C:\Python39\lib\site-packages\discord\ext\tasks\__init__.py", line 101, in _loop
    await self.coro(*args, **kwargs)
  File "C:\Users\zedga\source\repos\Hydrate\Hydrate\Hydrate.py", line 16, in reminder
    await channel.send('It has been 10 seconds')
NameError: name 'channel' is not defined
回溯(最近一次呼叫最后一次):
文件“C:\Python39\lib\site packages\discord\client.py”,第343行,在运行事件中
等待coro(*args,**kwargs)
文件“C:\Python39\lib\site packages\discord\ext\commands\bot.py”,第942行,在on_消息中
等待self.process_命令(消息)
文件“C:\Python39\lib\site packages\discord\ext\commands\bot.py”,第939行,进程内命令
等待自我调用(ctx)
文件“C:\Python39\lib\site packages\discord\ext\commands\bot.py”,第902行,在invoke中
等待ctx.command.invoke(ctx)
文件“C:\Python39\lib\site packages\discord\ext\commands\core.py”,第856行,在invoke中
等待自我准备(ctx)
文件“C:\Python39\lib\site packages\discord\ext\commands\core.py”,第790行,在prepare中
等待self.\u解析\u参数(ctx)
文件“C:\Python39\lib\site packages\discord\ext\commands\core.py”,第693行,在_parse_参数中
引起不一致。客户例外(fmt.格式(自我))
discord.errors.ClientException:启动命令的回调缺少“ctx”参数。
从未检索到任务异常
未来:
回溯(最近一次呼叫最后一次):
文件“C:\Python39\lib\site packages\discord\ext\tasks\\uuuu init\uuuu.py”,第125行,在循环中
加薪
文件“C:\Python39\lib\site packages\discord\ext\tasks\\uuuu init\uuuu.py”,第101行,在循环中
等待自我。coro(*args,**kwargs)
文件“C:\Users\zedga\source\repos\Hydrate\Hydrate\Hydrate.py”,第16行,在提醒中
等待channel.send('已经10秒了')
NameError:未定义名称“通道”
内部后台任务“提醒”中未处理的异常。
回溯(最近一次呼叫最后一次):
文件“C:\Python39\lib\site packages\discord\ext\tasks\\uuuu init\uuuu.py”,第101行,在循环中
等待自我。coro(*args,**kwargs)
文件“C:\Users\zedga\source\repos\Hydrate\Hydrate\Hydrate.py”,第16行,在提醒中
等待channel.send('已经10秒了')
NameError:未定义名称“通道”

在您的错误中,包括“未定义频道”

这是因为它的通道被定义为“局部”变量,这意味着它只能在同一代码块或命令中使用。您可以通过添加一个全局变量来解决这个问题,全局变量使整个代码都可以访问,它可以这样使用:

global channel
channel = client.get_channel(797915093954199565)
您还缺少“ctx”装饰器,您的任务应该位于触发它的命令下方

@client.command()
async def start(ctx):
    reminder.start()
    print('Reminder Started')


@tasks.loop(seconds=10)
async def reminder():
    global channel
    channel = client.get_channel(797915093954199565)
    await channel.send('It has been 10 seconds')


@client.command()
async def stop(ctx):
    reminder.cancel()
    print('Reminder stopped')

是的,当然,只需获取发送命令的通道id,并对其进行全局处理,以便在任务中使用

@client.command()
async def start(ctx):
    global start_channel
    start_channel = ctx.channel.id
    reminder.start()
    print('Reminder Started')


@tasks.loop(seconds=4)
async def reminder():
    channel = client.get_channel(int(start_channel))
    await channel.send('It has been 10 seconds')


@client.command()
async def stop(ctx):
    reminder.cancel()
    print('Reminder stopped')

797915093954199565
可能不是频道。有没有办法让机器人在命令所在的同一频道中执行提醒循环,以便它可以在dms和任何需要的频道中工作?当然@Zeeshan检查我最近的回答