简体   繁体   English

Discord Py 将消息发送到特定通道

[英]Discord Py send message to specific channel

I'm working on my discord bot.我正在研究我的 discord 机器人。 I'm making an order to send a message to a specific room except I can't make it... Here's my code:我正在下订单向特定房间发送消息,但我无法做到......这是我的代码:

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

and here's the error I'm getting in the terminal.这是我在终端中遇到的错误。

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

Thank you in advance for your response.预先感谢您的回复。

PS: My bot is in french PS:我的机器人是法语的

You can have a message as string more then one word, by doing:您可以通过执行以下操作将消息作为字符串超过一个单词:

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

The colon and discord.Channel signifies you need to enter an #channel format on discord screen Also to have a multi letter message, do this:冒号和 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)

the star sign makes it a tuple, So if you do星号使它成为一个元组,所以如果你这样做

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

So Hello, and, bye are parts of a tuple, like this: ('Hello','and','bye') so when you do所以 Hello, and, bye 是元组的一部分,像这样: ('Hello','and','bye')所以当你这样做的时候

" ".join(msg)

it joins the elements of tuple to give you a string, separeted by spaces.它连接元组的元素给你一个字符串,用空格分隔。 It works for lists too.它也适用于列表。 Hope this helps希望这可以帮助

Here is part of a code that can get you started: I have recently made this:这是可以帮助您入门的代码的一部分:我最近做了这个:

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

You can than change name to id and put your channel id instead of "│arrivals"!您可以将name更改为id并输入您的频道 ID 而不是“│arrivals”! I have not test this out but this my best guess that it will work!我没有对此进行测试,但这是我最好的猜测,它会起作用! Of course this is an example for when someone joins but you can use this for any command!当然,这是某人加入时的示例,但您可以将其用于任何命令!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM