如何向特定频道发送消息?不和谐/Python

如何向特定频道发送消息?不和谐/Python,python,discord,discord.py,Python,Discord,Discord.py,如何向特定频道发送消息? 为什么我会犯这个错误?我的ChannelID是正确的 代码: 错误: raise CommandInvokeError(exc) from exc discord.ext.commands.errors.CommandInvokeError: Command raised an exception: AttributeError: 'NoneType' object has no attribute 'send' 客户端和频道超出范围。您可以使用global关键

如何向特定频道发送消息? 为什么我会犯这个错误?我的
ChannelID
是正确的

代码:

错误:

raise CommandInvokeError(exc) from exc
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: AttributeError: 'NoneType' object has no attribute 'send'




客户端
频道
超出范围。您可以使用
global
关键字进行恶意攻击:

来自discord.ext导入命令
client=commands.Bot(命令前缀='!')
通道=客户端。获取通道(693503765059338280)
@客户端事件
_ready()上的异步定义:
全球客户
打印('Bot wurde gestaret:'+client.user.name)
#wts
@client.command()
异步def测试(ctx、名称、preis、festpreis):
全球客户
全球频道
等待channel.send(discord.Object(id=693503765059338280),“Name:”+Name_schuh+“\n Preis:“+Preis+”\n Festpreis:”+Festpreis)

但是更好的选择是使用一个处理程序类来保存实例。

出现错误的原因是因为在连接bot之前调用了
channel=client.get\u channel()
,这意味着它将始终返回
None
,因为它看不到任何通道(未连接)

将其移动到命令函数内部,使其在调用命令时获得
通道
对象

还请注意,自版本1.0以来。这意味着您需要使用
client.get_频道(69350376509338280)
而不是
client.get_频道('69350376509338280')


不行
channel=client.get\u channel()
正在返回上述代码中的
None
,但仍将返回bot连接之前调用的
None
。它看不到任何频道,因此
get\u频道
将始终返回
None
raise CommandInvokeError(exc) from exc
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: AttributeError: 'NoneType' object has no attribute 'send'



from discord.ext import commands


client = commands.Bot(command_prefix='!')


@client.event
async def on_ready():
    print('Bot wurde gestartet: ' + client.user.name)

@client.command()
async def test(ctx,name_schuh,preis,festpreis):
    channel = client.get_channel(693503765059338280)
    await channel.send("Name:" + name_schuh +"\n Preis: " + preis +"\n Festpreis: " + festpreis)

client.run('token')