Emir Sürmen
Emir Sürmen

Reputation: 950

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)

So this part of my code is made for setting a log channel using JSON. The json part works fine as i have used it in several more commands. But it doesnt see if there was a valid channel given or not. As i know it should be discord.TextChannel but it's not. The if len(args) == 0 part works but others don't. How can i make this work? What should i put instead of discord.TextChannel?

Upvotes: 0

Views: 4798

Answers (3)

IKanekiI12
IKanekiI12

Reputation: 5

I think it can work based on that.

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

Upvotes: 0

Just for fun
Just for fun

Reputation: 4225

A more precise way to do this will be using TextChannelConverter as it returns channel when provided ID/Name/Mention not only Name

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

Upvotes: 1

Abdulaziz
Abdulaziz

Reputation: 3426

Return just ends the function you can use it instead of if statements. I didn't know what is the JSON file used, I hope this will help

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")

Upvotes: 0

Related Questions