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)
参考资料:
存在
和成员
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)