不协调Py将消息发送到特定通道

2024-05-19 00:04:20 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在开发我的discord机器人。 我在下命令要把信息发送到一个特定的房间,除非我做不到。。。这是我的密码:

@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"

提前感谢您的回复

我的机器人是法语的


Tags: nameclientsendtrueformatmessagechannel机器人
2条回答

通过执行以下操作,您可以将消息作为字符串或多个单词显示:

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

它将元组的元素连接起来,生成一个由空格分隔的字符串。它也适用于列表。 希望这有帮助

这是一段代码的一部分,可以让你开始!我最近提出:

@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替换为“👋│抵达“!我还没有测试出来,但这是我最好的猜测,它将工作!当然,这是一个例子,当有人加入,但你可以使用它的任何命令

相关问题 更多 >

    热门问题