1
votes
@bot.event
async def on_message(message):
    if message.author == bot.user:
        return
    if isinstance(message.channel, DMChannel):
        guild = bot.get_guild(guildID) 
        channels = await guild.fetch_channels()
        channel = discord.utils.get(channels, name=message.author.id)
        message1 = message.author
        embedVar = discord.Embed(title= f'{message.author} has sent a new message', description = message.content, 
        color= 0xff0000)
        category = discord.utils.get(guild.categories, id = category.id) 
        channel = await guild.create_text_channel(message.author.id, category=category, id = message.author.id)
        embedMod = discord.Embed(title = 'Information for the mods', description = '!close- Closes the text channel'
        '\n!DM- To DM the user anonymously', color = 0x00FF00)
        
        if channel not in channels:
        await guild.create_text_channel(message.author.id, category = category)
        await channel.send(embed=embedMod)
        if channel in channels:
        pass    
        await channel.send(embed=embedVar)
        
    
    await bot.process_commands(message)

I am making a bot where the user dms the bot for help and the bot creates a text channel in the guild under a specific category. The problem is every time a message is DMed to the bot, the bot creates a text channel for every message even if it is the same user. How do I make sure that the bot doesnt create a text channel with the same name if its already made and the other messages of the same user are sent to the channel..

1
you need to save the user_id and "his" channel_id to a db, or json file. Then you can check if the user already has a ticketGuddi
How would I do that ? Can you help me? @GuddiSaahil Gupta
loop through the catagory.channels and check if channel.topic == str(message.author.id)Guddi
It didnt work, its still crating new channels. Any other idea ?Saahil Gupta
can you edit your post with your current code? :DGuddi

1 Answers

0
votes

I've if understood your code correctly the name of the channel is the user id of the user who dm'ed the bot? If so, you could just check if a channel with that name already exists and redirect your message there. And if not, create the channel

@bot.event
async def on_message(message):
    if message.author == bot.user:
        return
    if isinstance(message.channel, DMChannel):
        guild = bot.get_guild(guildID) 
        channels = await guild.fetch_channels()
        channel = discord.utils.get(channels, name=message.author.id)
        message1 = message.author
        embedVar = discord.Embed(title= f'{message.author} has sent a new message', description = message.content, 
        color= 0xff0000)
        category = discord.utils.get(guild.categories, id = category.id)
        embedMod = discord.Embed(title = 'Information for the mods', description = '!close- Closes the text channel'
        '\n!DM- To DM the user anonymously', color = 0x00FF00)
        
        if channel == None: #Channel doesn't exist yet. So create it
            channel = await guild.create_text_channel(message.author.id, category=category, id = message.author.id)
        
        await channel.send(embed=embedMod)
        await channel.send(embed=embedVar)
    
    await bot.process_commands(message)