0
votes

I am making a discord ModMail bot and I have made it so that a channel is opened when somebody dms the bot but what is happening is that every single time somebody DMs the bot, it opens another channel. For example, if i had a conversation where I send 10 messages to the bot; 10 different channels would be opened. Does anybody know how I can make it send to the existing one? I have tried using checks but I am not sure how I would implement them. Here is my code:

async def on_message(message):
    if message.author.id == client.user.id:
        return

    if message.author != message.author.bot:
      await message.add_reaction('✅')
      if not message.guild:
        guild = client.get_guild(guild)
        c = None
          
        for channel in guild.text_channels:  # check every channel
           if channel.name == "❗ticket-{message.author.id}":
            c = channel  # channel found, stop looking
            break

    if c is None:  # if no channel was found
      c = await guild.create_text_channel('❗ticket-{message.author.id}')
      
      embed = Embed(color=discord.Color.orange())
      embed.add_field(name="**????ModMail Support????**",
                            value=f"User Mention: {message.author.mention}\nUsername: **{message.author}**\nUser-ID: **{message.author.id}**\n\n__**Content**__\n{message.content}")
           
          
      await c.send(embed=embed)

            

Many thanks in advance! :)

1
that code doesn't make any senseŁukasz Kwieciński

1 Answers

0
votes

You could try checking if a channel with that name already exists in said guild. You can get a list of a guild's channels with Guild.text_channels.

c = None

for channel in guild.text_channels:  # check every channel
    if channel.name == "❗ticket-{message.author.id}":
        c = channel  # channel found, stop looking
        break

if c is None:  # if no channel was found
    c = ..  # create the channel

Keep in mind that not all characters can be in channel names (like spaces), there can be no capital letters, and you can have duplicates, so you probably want to create a channel with the user's id instead, and add their display name or @mention to the channel description to know their name.