【问题标题】:Discord Py send message to specific channelDiscord Py 向特定频道发送消息
【发布时间】:2020-10-01 06:47:16
【问题描述】:

我正在开发我的不和谐机器人。 我正在下订单向特定房间发送消息,但我无法做到......这是我的代码:

@client.command(name='say', help='Dire un message à votre place')
@commands.has_permissions(send_messages=True, manage_messages=True)
async def say(ctx, message, channelid):
    await client.wait_until_ready()
    channel = client.get_channel(channelid)
    embed = discord.Embed(title="Message", color=discord.Color.dark_red())
    embed.add_field(name="Nouveau message de {}".format(ctx.message.author), value="{}".format(message))
    embed.set_footer(text="{}".format(ctx.message.author))
    await channel.send(embed=embed)

这是我在终端中遇到的错误。

Command raised an exception: AttributeError: 'NoneType' object has no attribute 'send"

提前感谢您的回复。

PS : 我的机器人是法语的

【问题讨论】:

  • 看起来client.get_channel(channelid) 找不到需要的频道,所以它返回None 值。你确定channelid 是正确的吗?
  • 我在 discord 上发送的消息是 c?say message 720347475361267843
  • 请! ;)

标签: python discord.py


【解决方案1】:

您可以通过以下方式将消息作为字符串超过一个单词:

@client.command(name='say', help='Dire un message à votre place')
@commands.has_permissions(send_messages=True, manage_messages=True)
async def say(ctx, channel:discord.TextChannel, message):
    #Embed making
    embed = discord.Embed(title="Message", color=discord.Color.dark_red())
    embed.add_field(name="Nouveau message de {}".format(ctx.message.author), value="{}".format(message))
    embed.set_footer(text="{}".format(ctx.message.author))

    await channel.send(embed=e)

冒号和 discord.Channel 表示您需要在 discord 屏幕上输入 #channel 格式 也有一个多字母消息,这样做:

async def say(ctx, channel:discord.TextChannel, *msg):
    message = " ".join(msg)
    #Embed making
    embed = discord.Embed(title="Message", color=discord.Color.dark_red())
    embed.add_field(name="Nouveau message de {}".format(ctx.message.author), value="{}".format(message))
    embed.set_footer(text="{}".format(ctx.message.author))

    await channel.send(embed=e)

星号使它成为一个元组,所以如果你这样做

c?say #text-channel-send Hello and Bye

你好,再见是元组的一部分,像这样:('Hello','and','bye') 所以当你这样做时

" ".join(msg)

它连接元组的元素给你一个字符串,用空格分隔。它也适用于列表。 希望这会有所帮助

【讨论】:

  • 我有这个错误:Traceback (most recent call last): File "main.py", line 74, in <module> async def say(ctx, channel:discord.Channel, message): AttributeError: module 'discord' has no attribute 'Channel'
【解决方案2】:

这是可以帮助您入门的部分代码!我最近做了这个:

@client.event
async def on_member_join(member):
    channel = discord.utils.get(member.guild.channels, name="?│arrivals")
    await channel.send(f"Welcome to Sleep's Gaming Cult {member.mention}! You are the ### member")

您可以将name 更改为id 并输入您的频道ID 而不是“?│arrivals”!我没有对此进行测试,但这是我最好的猜测,它会起作用!当然,这是某人加入时的示例,但您可以将其用于任何命令!

【讨论】:

  • 我有这个错误Command raised an exception: NameError: name 'member' is not defined
猜你喜欢
  • 2020-04-07
  • 2021-03-24
  • 2021-10-28
  • 2019-03-01
  • 2023-03-27
  • 2021-01-10
  • 1970-01-01
  • 2020-07-06
  • 2020-07-02
相关资源
最近更新 更多