【问题标题】:get channel name and send a message over at that channel获取频道名称并在该频道发送消息
【发布时间】:2020-09-27 07:14:57
【问题描述】:

所以我在这里做一个小项目,而且我非常想拥有其中一个“请输入此服务器中频道的名称”功能。

差不多,机器人会询问频道名称,我输入例如“#changelog” - 然后它会询问它应该在该频道中写入什么等等。 所以需要获取频道ID(我猜),但我不希望用户写ID,而只写#server-name。然后每当我这样做时,机器人就会在该频道中写入。

这是我当前的代码!

class Changelog(commands.Cog):

    def __init__(self, client):
        self.client = client

    @commands.Cog.listener()
    async def on_ready(self):
        print('Changelog is loaded')

    @commands.command()
    async def clhook(self, ctx):
        await ctx.send('Write text-channel: ')
        text_channel = await self.client.wait_for("message", check=lambda message: message.author == ctx.author, timeout=300)
        clhook = self.client.get_channel(text_channel)


def setup(client):
    client.add_cog(Changelog(client))

编辑: 频道 ID 将“永远”保存,这意味着我不必重写消息应该去哪里的频道名称!

【问题讨论】:

    标签: discord.py discord.py-rewrite


    【解决方案1】:

    您可以在此示例中使用discord.utils.get()

    text_channel = await self.client.wait_for("message", check=lambda message: message.author == ctx.author, timeout=300)
    channel = discord.utils.get(ctx.guild.text_channels, name=text_channel)
    await channel.send('Bla Bla')
    

    因此,当您输入(prefix)clhook 时只输入频道名称,例如general,它会将Bla Bla 发送到名为general 的频道。

    还有另一种方法可以做到这一点,我认为它比第一个选项简单,这里是:

    @commands.command()
    async def clhook(self, ctx, channel: discord.TextChannel):
        await channel.send('Bla Bla')
    

    所以在这个命令中,用法被改变了。您可以使用它:(prefix)clhook #general(mention the channel)。我建议这个解决方案,我认为它更有用。

    【讨论】:

    • 好吧,让我们看看我有没有这个。第一个给了我一些错误,'NoneType' object has no attribute send,这是一个齿轮。 self 是否必须包含在某处?或者这个错误是什么意思?
    • 表示该频道不存在或可能与自己有关。您是否尝试过第二种解决方案?
    • 好吧,在第二个解决方案中。我还没有测试过 - 我将如何获得用户正在输入的频道?
    • 在第二种解决方案中,used 只需输入(prefix)clhook #channel-name 然后它就会向该频道发送消息。
    • 或者哦,所以每次我需要向该特定频道写消息时,我都需要运行!!clhook #text-channel?我希望它更像,你只写一次,它就会永远适用于那个频道。所以!!clhook #text-channel 将是“钩子”命令,之后我需要写的是!!changelog <text-goes-here>,它会一直写在那里。
    【解决方案2】:

    您可以使用message.channel_mentions。这将返回使用#channel-name 表示法提到的所有频道的list。这样,您只需使用channel.id 即可获取他们提到的频道的id

    但是,不要忘记检查用户是否确实标记了一个频道(您也可以将其放入您的check)。为了这个回复,我把它放在一个单独的函数中使它更具可读性,但如果你真的想的话,你可以把它放在你的 lambda 中。

    另外,请务必检查它是 Text Channel 而不是 Voice ChannelCategory Channel

    @commands.command()
    async def clhook(self, ctx):
    
        def check(self, message):
            author_ok = message.author == ctx.author  # Sent by the same author
            mentioned_channel = len(message.channel_mentions) == 1 and isinstance(message.channel_mentions[0], discord.TextChannel)
            return author_ok and mentioned_channel
    
        await ctx.send("Write text-channel: ")
        text_channel = await self.client.wait_for("message", check=check)
        chlhook = text_channel.channel_mentions[0]
    

    我在mentioned_channel 行设置了两个条件,因为如果第一个条件失败,第二个条件可能会导致 IndexError。或者,您也可以使用if-statement 尽快返回该地点以解决相同的问题。

    【讨论】:

    • 啊,我明白了,但是对于chlhook,那会是“存储”频道名称的地方吗?或者我如何向该频道发送类似“测试”的消息?
    • 不,它不存储name,但您将实际的channel 实例从列表中拉出,因此您可以通过clhook.send() 向其发送消息,或使用clhook.id 获取它的 ID。这样,您不必使用utils.get() 来查找通道对象,因为您已经拥有它。不过,您可能想重命名它,因为它可能会搞砸(它与您的函数同名)。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-21
    • 2021-03-24
    • 1970-01-01
    • 1970-01-01
    • 2020-04-07
    • 1970-01-01
    相关资源
    最近更新 更多