如何获取提到的频道的输入并在该频道中发送消息?

时间:2020-08-26 18:59:11

标签: python discord discord.py

我有以下代码从消息中生成嵌入,现在我可以正常工作了:创建嵌入后,机器人应要求用户提及一个频道,而在用户提及频道后,机器人应发送该嵌入那里。我该怎么办?

@bot.command()
async def embed(ctx):
    await ctx.send("Enter title for embed:")
    e = await get_input_of_type(str, ctx)
    await ctx.send("Enter the content for embed:")
    c = await get_input_of_type(str, ctx)
    embed = discord.Embed(
        title = e,
        description = c,
        color = 0x03f8fc,
        timestamp= ctx.message.created_at
    )
    embed.set_thumbnail(url = ctx.guild.icon_url)
    await ctx.channel.send(embed=embed)
    

2 个答案:

答案 0 :(得分:0)

我使用Client.wait_for()协程等待用户的消息。然后,我格式化了message.content字符串以获取通道的ID。然后,我使用discord.utils.get方法仅使用ID来获取频道。

# at the top of the file
from discord.utils import get


    @client.command()
    async def embed(ctx):

        # embed stuff here
        
        def check(msg):
            # make sure the author of the message we're waiting for is the same user that invoked the command
            return ctx.author == msg.author
        
        await ctx.send("mention a channel to send to")
        msg = await client.wait_for('message', timeout=60.0, check=check)
        

        msg = msg.content.split("#")[1].split('>')[0]
"""
over here we are spliting the '#' from the message content first and index just the ID with the '>' 
(if the message is just a channel mention it shoud be something like this:  <#000000000000000000>) 
then we split the '>' from the string and index just the ID part and now we have the ID of the channel
"""
        channel = get(ctx.guild.channels, id=int(msg))

        await channel.send(f'{words}')

答案 1 :(得分:0)

使用wait_for()和TextChannelConverter

@bot.command()
async def embed(ctx):
    def check(m):
        return m.author == ctx.author and m.channel == ctx.channel
    await ctx.send("Enter title for embed:")
    e = await get_input_of_type(str, ctx)
    await ctx.send("Enter the content for embed:")
    c = await get_input_of_type(str, ctx)
    embed = discord.Embed(
        title = e,
        description = c,
        color = 0x03f8fc,
        timestamp= ctx.message.created_at
    )
    embed.set_thumbnail(url = ctx.guild.icon_url)
    channel = await bot.wait_for("message", check=check)
    channel = await commands.TextChannelConverter().convert(ctx, channel.content)
    await channel.send(embed=embed)