簡體   English   中英

discord.py如何查看文本通道

[英]discord.py how to check text channel

我想確保他們只能將文本通道保存到 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

這回答了你的問題了嗎?

if isinstance(channel, discord.TextChannel):
    print("text channel")

或者:

if type(ctx.channel) == discord.TextChannel:
    print("text channel")

我解決了這個問題。

    @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.')

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM