本文介绍了如何将消息发送到特定频道?不和谐/ Python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将消息发送到特定频道?
为什么会出现此错误?我的 ChannelID 是正确的

How do I send a message to a specific channel?Why am I getting this Error ? My ChannelID is right

代码:

from discord.ext import commands


client = commands.Bot(command_prefix='!')
channel = client.get_channel('693503765059338280')



@client.event
async def on_ready():
    print('Bot wurde gestartet: ' + client.user.name)
#wts
@client.command()
async def test(ctx,name_schuh,preis,festpreis):
    await channel.send(discord.Object(id='693503765059338280'),"Name:" + name_schuh +"\n Preis: " + preis +"\n Festpreis: " + festpreis)

错误:

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




推荐答案

发生错误的原因是因为 channel = client.get_channel()在连接机器人之前被调用,这意味着它将始终返回 None ,因为它看不到任何通道(未连接)。

The reason for the error is because channel = client.get_channel() is called before the bot is connected, meaning it will always return None as it cannot see any channels (not connected).

移动将此添加到命令函数中,以使其在调用命令时获得 channel 对象。

Move this to inside your command function to have it get the channel object as the command is called.

还要注意,从1.0版开始, 。这意味着您需要使用 client.get_channel(693503765059338280)而不是 client.get_channel('693503765059338280')。 / p>

Also note that since version 1.0, snowflakes are now int type instead of str type. This means you need to use client.get_channel(693503765059338280) instead of client.get_channel('693503765059338280').

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

这篇关于如何将消息发送到特定频道?不和谐/ Python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 07:58