簡體   English   中英

Discord.py 文本通道檢查

[英]Discord.py text channel check

@client.command(aliases=["logchannel, setlog"])
@commands.has_permissions(manage_messages=True)
async def log(ctx, *args: discord.TextChannel):
    with open("configs/channels.json", "r") as f:
        channels = json.load(f)
    channel = channels.get(str(ctx.guild.id), ctx.channel.id)
    if len(args) == 0:
        await ctx.send("Which channel should I set the logs? :thinking:")
    elif args[0] != discord.TextChannel:
        await ctx.send("That is not a valid channel!")
    elif args[0] == discord.TextChannel:
        with open("configs/channels.json", "w") as f:
            json.dump(channels, f, indent=4)
        embed = discord.Embed(title="Log channel set! :white_check_mark:",
                              description=f"**{channel}** has been set as logs channel!",
                              color=0x2f3136)
        await ctx.send(embed=embed)

因此,我的這部分代碼是用於使用 JSON 設置日志通道。 json 部件工作正常,因為我在更多命令中使用了它。 但它看不到是否有一個有效的頻道。 據我所知,它應該是discord.TextChannel但它不是。 if len(args) == 0部分有效,但其他部分無效。 我怎樣才能使這項工作? 我應該用什么代替discord.TextChannel

更精確的方法是使用TextChannelConverter ,因為它在提供 ID/名稱/提及時返回通道,而不僅僅是名稱

from discord.ext import commands

async def log(ctx, *, args=None):
    if not args:
        await ctx.send("Please provide the channel to set the logs")
        return
    try:
        channel = await commands.TextChannelConverter().convert(ctx, args)
    except:
        return await ctx.send("Channel Not Found")
    #channel is not a TextChannel object, save its ID or send or whatever you want to do

Return 只是結束 function 您可以使用它而不是 if 語句。 我不知道使用的 JSON 文件是什么,希望對您有所幫助

async def log(ctx, *, args=None):
    if not args:
        await ctx.send("Which channel should I set the logs? :thinking:")
        return

    for channel in ctx.guild.channels:
        if channel.name == args:
            await ctx.send('Found your channel')
            # channel is now an object, you can do what you want here
            await channel.send('This is the channel wanted')
            return

    await ctx.send("Can't find your 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