我想这样做,一旦你执行命令 h!start,它就会每 10 秒向你发送一条消息,并在用户键入 h!stop 时停止。但是,discord bot 永远不会在 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
答案 0 :(得分:1)
是的,只需获取发送命令的通道的 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')
答案 1 :(得分:0)
在您的错误中,它包括“频道未定义”
因为它的通道被定义为“本地”变量,这意味着它只能在同一代码块或命令中使用。您可以通过添加一个全局变量来简单地解决这个问题,一个全局变量使一个变量在整个代码中都可以访问,它可以像这样使用:
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')