【问题标题】:Sending a looped message in discord.py在 discord.py 中发送循环消息
【发布时间】:2021-06-29 14:22:27
【问题描述】:

我想让它在你执行命令 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

【问题讨论】:

  • 797915093954199565 可能不是频道。

标签: discord.py


【解决方案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')

【讨论】:

    【解决方案2】:

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

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

    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')
    

    【讨论】:

    • 有没有办法让机器人在命令所在的同一频道中执行提醒循环,以便它可以在 dms 和所需的任何频道中工作?
    • 确定@Zeeshan 检查我最近的答案
    猜你喜欢
    • 2020-10-01
    • 2021-04-04
    • 2019-04-14
    • 2021-07-08
    • 2021-10-15
    • 1970-01-01
    • 1970-01-01
    • 2020-10-15
    • 2020-11-10
    相关资源
    最近更新 更多