首页 > 解决方案 > 如何获取用户消息的频道 ID?

问题描述

我正在编写一个 Discord Bot,我想知道如何从任何用户的消息中获取频道 ID。

我实际上想制作一个文本机命令。当用户处于 AFK 或离线状态时,它会向用户留下一条消息。当他们返回并在服务器中的任何通道中发送消息时,机器人会立即将消息发送给他们到同一通道。

@client.command()
async def txt(ctx, receiver: discord.Member, *, message):
    await ctx.message.delete()
    def check(message: discord.Message):
        return message.author == receiver
    newembed = discord.Embed (title='On-Message Scheduled', description=f'Message will be delivered to the receiver as soon as he messages. \n Message: `{message}` \n to **{receiver}**')
    channel = await ctx.message.author.create_dm()
    x = await channel.send(embed=newembed)
    msg = await client.wait_for('message', check = check)
    await ctx.send(f'TEXT MACHINE \n **{ctx.message.author}:** `{message}`\n for {receiver.mention}')
    await x.add_reaction('✅')

这在某种程度上确实有效。但问题是,如果我这样做,假设>txt @User#1234 this is a message通道 1 上的命令和通道 2 上的用户消息,机器人将消息发送到通道 1,但我希望机器人将消息发送到用户消息的通道。

帮助将不胜感激,谢谢。

标签: python-3.xdiscorddiscord.py-rewrite

解决方案


很简单,就是不要使用ctx.send,而是使用msg.channel.send。所以从 end 开始的第二行将如下所示:

await msg.channel.send(f'TEXT MACHINE \n **{ctx.message.author}:** `{message}`\n for {receiver.mention}')

推荐阅读