Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/240.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何每分钟使用discord.py在discord中发送消息_Python_Discord_Discord.py_Discord.py Rewrite_Python 3.9 - Fatal编程技术网

Python 如何每分钟使用discord.py在discord中发送消息

Python 如何每分钟使用discord.py在discord中发送消息,python,discord,discord.py,discord.py-rewrite,python-3.9,Python,Discord,Discord.py,Discord.py Rewrite,Python 3.9,我从中得到了这段代码,上面说它应该可以工作,但我不断得到以下错误: import discord from discord.ext import tasks client = discord.Client() channel = client.get_channel(CHANNEL-ID) @tasks.loop(minutes=1) async def test(): await channel.send("test") @client.event async

我从中得到了这段代码,上面说它应该可以工作,但我不断得到以下错误:

import discord
from discord.ext import tasks

client = discord.Client()
channel = client.get_channel(CHANNEL-ID)

@tasks.loop(minutes=1)
async def test():
    await channel.send("test")

@client.event
async def on_ready():
    test.start()


TOKEN = "MYTOKEN"


client.run(TOKEN)

我能做些什么来修复此问题?

您得到的是
,因为客户端的缓存尚未加载

如果以后打算添加命令,我还建议使用
commands.Bot(…)
,而不是
discord.Client()
commands.Bot()
继承了
Client()
,所以现在可以做的任何事情,都可以使用
Bot()
来完成

要解决此问题,您可以在
on\u ready
事件中让用户:

Unhandled exception in internal background task 'test'.
Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/discord/ext/tasks/__init__.py", line 101, in _loop
    await self.coro(*args, **kwargs)
  File "/Users/me/Coding/Py/Gmail/bot.py", line 9, in test
    await channel.send("test")
AttributeError: 'NoneType' object has no attribute 'send'
如果您仍然获得
None
,则需要启用特权意图。这可以通过以下代码完成,并在应用程序页面中启用以下按钮:

@tasks.loop(minutes=1)
async def test(channel):
    await channel.send("test")

@bot.event
async def on_ready()
    channel = bot.get_channel(ID_HERE)
    test.start(channel=channel)


参考资料:

您定义意图了吗?@Nurqm没有,我忘了您必须这么做,谢谢您的帮助意图不应该是问题,如果没有提供,它将假定为默认意图。确保传递的是一个整数,而不是字符串,并在on_ready或循环本身中执行此操作。是
存在
成员
Client.get\u channel()
在中。我在这里为ID\u添加什么?@thegroup主控您要发送消息的用户的ID我认为他想在一个频道中发送消息,因此它将是
bot.get\u channel(channel\u ID)
。每分钟向用户发送一条消息都是垃圾邮件。@Sujit谢谢,刚刚解决了这个问题。不知道我从哪里得到的用户哈哈。
intents = discord.Intents().all()

bot = commands.Bot(command_prefix=..., intents=intents)