当前位置: 首页 > 知识库问答 >
问题:

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

方韬
2023-03-14

我有下面的代码从消息生成嵌入,它现在可以正常工作。我想要的是,在创建嵌入后,bot应该要求用户提到一个频道,在用户提到一个频道后,bot应该将该嵌入发送到那里。我该怎么做?

@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个答案

鲜于河
2023-03-14

我使用了客户端。wait_for()coroutine等待来自用户的消息。然后我格式化了消息。content字符串,仅获取频道的ID。然后我用了不和谐。乌提尔斯。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}')
程正阳
2023-03-14

使用wait_for()和代码

@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)
 类似资料:
  • 我正在创建一个不和谐机器人,它每天在某个时间向公会发送消息。我使用为预定的消息,但不确定为机器人编写什么代码来将消息发送到公会的一般频道,如果他们没有一般频道,则发送到带有大多数活动或消息。 由于已被删除,我不确定该如何执行此操作。另外,我不想使用通道id将消息发送到特定的通道,因为我想将此bot公开,以便许多不同的协会可以使用。 它不一定要发送到“默认”频道或包含大多数消息的频道。这些正是我想到

  • 我目前正在使用Java和jda为discord制作一个机器人。我想让机器人向特定的通道发送消息。我该怎么做?

  • 前面两节里我们用到的输入和输出都是二维数组,但真实数据的维度经常更高。例如,彩色图像在高和宽2个维度外还有RGB(红、绿、蓝)3个颜色通道。假设彩色图像的高和宽分别是$h$和$w$(像素),那么它可以表示为一个$3\times h\times w$的多维数组。我们将大小为3的这一维称为通道(channel)维。本节我们将介绍含多个输入通道或多个输出通道的卷积核。 多输入通道 当输入数据含多个通道时

  • 我正在为我的Discord服务器创建一个bot。 我的服务器中有一个新的用户通道。当新用户加入时,我想向这个频道发送欢迎消息。但是,我不知道如何才能访问新用户的渠道。

  • 我曾经能够做到这一点,但我正在努力应对0.3的期货。 下面是我从WebSocket获得的一个sink和stream: 我创建了一个在异步tokio任务之间通信的无界通道: 这是我被卡住的部分。我生成了一个异步任务,它应该连接无界接收器和接收器;我的想法是通过< code>unbounded_sender发送消息: 对于<code>send_all</code>,错误消息显示: 而且 而且 查看文档

  • 因此,服务器和客户端都发生这种情况。我有来自通道活动方法的通道处理程序上下文,我正在尝试使用写入AndFlush(对象消息)方法向其写入对象,但似乎消息永远不会进入创建的管道。 下面是我的客户端处理程序的样子(我重写了包解码器和编码器中的一些方法来调试) 这是我如何写入ChannelHandlerContext通道变量 当我运行我的代码时,“从客户端写入服务器的数据”是打印机,但“在客户端上编码的