Python 无法向discord频道discord.py发送消息

Python 无法向discord频道discord.py发送消息,python,discord,discord.py,Python,Discord,Discord.py,我想用这个简单的后台任务发送一条消息。但是,在我的python shell上没有错误,但是消息不会发送 import discord import asyncio client = discord.Client() async def my_background_task(): await client.wait_until_ready() counter = 0 channel = discord.Object(id='791003444726988850')

我想用这个简单的后台任务发送一条消息。但是,在我的python shell上没有错误,但是消息不会发送

import discord
import asyncio

client = discord.Client()

async def my_background_task():
    await client.wait_until_ready()
    counter = 0
    channel = discord.Object(id='791003444726988850')
    while not client.is_closed:
        counter += 1
        await client.send_message(channel, counter)
        await asyncio.sleep(60) # task runs every 60 seconds

@client.event
async def on_ready():
    print('Logged in as')
    print(client.user.name)
    print(client.user.id)
    print('------')

client.loop.create_task(my_background_task())
client.run('MY BOT TOKEN')
我看到您正在使用代码,您是否正在使用登录令牌来运行bot,就像在
客户端中一样。运行('token')

首先,在while循环条件中
已关闭后添加
()
而不是客户端。已关闭():
否则,while循环将始终返回一个对象而不是bool,并且不满足该条件

然后将:
channel=discord.Object(id='791003444726988850')
更改为:
channel=client.get_channel(791003444726988850)

最后更改:
等待客户端。发送消息(频道、计数器)
到:
等待频道。发送(计数器)

完整代码:

import discord
import asyncio

client = discord.Client()

async def my_background_task():
   await client.wait_until_ready()
   counter = 0
   channel = client.get_channel(791003444726988850)
   while not client.is_closed():
       counter += 1
       await channel.send(counter)
       await asyncio.sleep(60) # task runs every 60 seconds

@client.event
async def on_ready():
   print('Logged in as')
   print(client.user.name)
   print(client.user.id)
   print('------')

client.loop.create_task(my_background_task())
client.run('MY BOT TOKEN')

您好,是的,我正在使用bot的令牌。请使用您正在使用的整个代码片段更新您的问题,这样可以更容易地捕获任何错误。为您更新了帖子。您使用的是相同的代码,因此代码本身不成问题。您是否在服务器上正确设置了bot权限?机器人有一个管理员角色,所以我猜它在所有聊天中都有发言的权限?我现在收到了错误:
AttributeError:'NoneType'对象没有属性'send'
我更新了帖子,请立即重试。此外,请确保通道id正确,因为如果未找到具有该id的通道,它可能会返回该错误。