Sp0t
Sp0t

Reputation: 39

Calling for a channel with discord.utils.get returns "None"

So basically what I want to do is create a channel with the name Ticket-(name and tag of the person) and then call it for a ctx.author.send so the person can go into the channel from their DMs, this is the code:

async def _1(ctx):

    guild = client.get_guild(742566911778357330)
    supportroom = ('bot-commands')
    name = 'Tickets'
    rolename = 'Staff'
    category = discord.utils.get(guild.categories, id=744151796439711825)
    role = discord.utils.get(guild.roles, name=rolename)
    overwrites = {
guild.default_role: discord.PermissionOverwrite(read_messages= False),
    guild.me: discord.PermissionOverwrite(read_messages=True),
 ctx.author: discord.PermissionOverwrite(read_messages=True),
 role: discord.PermissionOverwrite(read_messages=True)
    }

    if str(ctx.channel) in supportroom:
        {
        }
    else:
        await guild.create_text_channel('Ticket %s' % ctx.author, overwrites=overwrites, category=category)
        await ctx.author.send('**Your ticket has been created.**')

the problem is from this line forward

        print(ctx.message.author)
        nameChannelTicket = discord.utils.get(guild.channels, guild__name=guild, name=(('Ticket-%s') % (ctx.author)))
        print(nameChannelTicket)
        await ctx.author.send('Your ticket is %s' % nameChannelTicket)

Any help would be greatly appreciated as I have been working on it for more than 4 hours :)

https://prnt.sc/u02ezq That is what gets sent from the discord perspective ^

Upvotes: 1

Views: 1389

Answers (3)

Billy
Billy

Reputation: 1207

try first defining the guild, then using get for finding the channel with given name.

    print(ctx.message.author)
    guild = discord.utils.get(client.guilds, id = put_id_here)
    nameChannelTicket = discord.utils.get(guild.channels, name=(f'Ticket-{ctx.author}'))
    print(nameChannelTicket)
    await ctx.author.send('Your ticket is {0}'.format(nameChannelTicket))

Upvotes: 0

Sp0t
Sp0t

Reputation: 39

Thank you @StarbuckBarista! Very helpful, however when I wanted to click on the name to go directly to the channel it wouldn't let me as it just displays as a normal text, instead what I did was this:

        nameChannelTicket = await guild.create_text_channel('Ticket %s' % ctx.author, overwrites=overwrites, category=category)
        await ctx.author.send('**Your ticket has been created.**')
        print(ctx.message.author)
        print(nameChannelTicket)
        await ctx.author.send('Your ticket is <#%s>' % nameChannelTicket.id )

This way it would get the channel id and call it later, which allowed me to get inside the channel by one click, without having to call the guild, again very helpful your answer @StarbuckBarista! Very appreciated.

Upvotes: 0

StarbuckBarista
StarbuckBarista

Reputation: 1318

Rather than going through all of the trouble of trying to find the channel. Why don't we define it?

channel = await guild.create_text_channel('Ticket %s' % ctx.author, overwrites = overwrites, category = category)

Now that we have defined it, we can call it!

await ctx.author.send('Your ticket is %s' % channel.name)

Now you can totally get rid of defining nameChannelTicket. Have a nice day!

Upvotes: 1

Related Questions