Python discord.py如何检查文本频道

Python discord.py如何检查文本频道,python,discord,discord.py,Python,Discord,Discord.py,我想确保他们只能将文本频道保存到json文件中。我到处找,但找不到解决这个问题的办法。有人能帮忙解决这个问题吗 @commands.command() async def goodbye(self, ctx, channel=None): try: channel = int(channel) except Exception as e: return await ctx.send('Please enter

我想确保他们只能将文本频道保存到json文件中。我到处找,但找不到解决这个问题的办法。有人能帮忙解决这个问题吗

@commands.command()
    async def goodbye(self, ctx, channel=None):
        try:
            channel = int(channel)
        except Exception as e:
            return await ctx.send('Please enter a channel id.')

        if len(str(channel)) != 18:
            await ctx.send('Invalid id.')

        elif self.client.get_channel(id=int(channel)):
            with open('./data/goodbye.json', 'r') as f:
                welcome = json.load(f)

            welcome[str(ctx.guild.id)] = f"{channel}"


            with open('./data/goodbye.json', 'w') as f:
                json.dump(welcome, f, indent=4)

            await ctx.send(f'Goodbye channel set: {channel}')

        else:
            await ctx.send('Channel not found!')
我找到了这样的解决方案,但我真的不知道如何实施:
discord.TextChannel

这是否回答了您的问题

如果存在(频道,discord.TextChannel):
打印(“文本频道”)
或:

如果类型(ctx.channel)=discord.TextChannel:
打印(“文本频道”)

这是否回答了您的问题

如果存在(频道,discord.TextChannel):
打印(“文本频道”)
或:

如果类型(ctx.channel)=discord.TextChannel:
打印(“文本频道”)
我解决了这个问题

    @commands.command()
    async def test(self, ctx, channel=None):

        channel2 = self.client.get_channel(id=int(channel))

        if type(channel2) != discord.channel.TextChannel:
            await ctx.send('Please do not enter an audio channel')

        else:
            await ctx.send('Perfect.')
我解决了这个问题

    @commands.command()
    async def test(self, ctx, channel=None):

        channel2 = self.client.get_channel(id=int(channel))

        if type(channel2) != discord.channel.TextChannel:
            await ctx.send('Please do not enter an audio channel')

        else:
            await ctx.send('Perfect.')

是的,我想到了这样一个解决方案,但它不想工作。我用我的机器人试过了,它工作了,这很奇怪。我用不同的方式编辑了这篇文章。第二个选项的问题是,无论我输入什么值,我总是得到这个值:
print(“text channel”)
第一个选项就像我没有写入脚本一样。它总是转到下一个if。是的,我想到了这样一个解决方案,但它不想工作。我用我的bot尝试了它,它工作了,这很奇怪。我用不同的方式编辑了帖子。第二个选项的问题是,无论我输入什么值,我总是得到这个值:
print(“文本频道”)
第一个选项就像我没有写入脚本一样。它总是转到下一个if。可能尝试使用
channel:discord.textconnel
,而不仅仅是
channel=None
?然后可以立即获得文本通道对象。如果它不是一个文本频道,它会引发一个可以处理的错误。好吧,我明白了,但是如果这个解决方案对我来说是最好的。但是我不得不忘记这个解决方案:“channel=in(channel)”可能尝试使用
channel:discord.textconnel
而不是
channel=None
?然后可以立即获得文本通道对象。如果它不是一个文本频道,它会引发一个可以处理的错误。好吧,我明白了,但是如果这个解决方案对我来说是最好的。但是我不得不忘记这个解决方案:“channel=in(channel)”