Drip Narix
Drip Narix

Reputation: 1

Channel does not receive dms from User to Bot

I created a ticket system, where a user needs to dm the bot with "ticket". A ticket_channel will be created on the server and the function of all this is, that all the messages, that the user is sending to the bot, the bot sends these messages to the channel in my server. And reversed: The problem here is that the command "ticket" is working (a new text channel has been created) if the user sends a message, the channel in the server recieves nothing Can someone help me?

import discord
import asyncio

intents = discord.Intents.all()
client = discord.Client(intents = intents)
ticket_users = {}
@client.event
async def on_message(message):
    if message.channel.type == discord.ChannelType.private and message.content.startswith("ticket"):
        user = message.author
        if user.id in ticket_users:
            await user.send("Du hast bereits ein Ticket erstellt. Bitte warte auf eine Antwort oder schließe das Ticket mit close.")
            return
        else:
            ticket_users[user.id] = True
        guild = client.get_guild(ID) # Replace GUILD_ID with the ID of the server where you want to create the channel
        channel = await guild.create_text_channel(f"ticket-{user.name}")
        await channel.send(f"{user.mention} hat ein Ticket erstellt.")
        await user.send("Dein Ticket wurde erstellt. Ein Teammitglied wird sich in Kürze bei dir melden.")

        def check(m):
            return m.author == user and m.channel == channel

        while True:
                msg = await client.wait_for('message', check=check)
                if msg.attachments:
                    await user.send(msg.content, file=msg.attachments[0])
                else:
                    await user.send(msg.content)
                if msg.content.startswith == "close":
                    del ticket_users[user.id]
                    await channel.send("Ticket wurde geschlossen.")
                    await user.send("Dein Ticket wurde geschlossen.")
                    await channel.delete()
                    break
client.run("My Token")

Upvotes: 0

Views: 70

Answers (1)

ESloman
ESloman

Reputation: 2102

Your check function is wrong - you're checking for messages sent in the newly created channel and not the direct message channel.

def check(m):
    return m.author == user and m.channel == message.channel.id

Personally, though, I wouldn't be using a while True loop in an on_message function. I would do something like the below.

@client.event
async def on_message(message: discord.Message):
    if message.channel.type == discord.ChannelType.private:
        user = message.author
        if message.content.startswith("ticket"):
            if user.id in ticket_users:
                await user.send("Du hast bereits ein Ticket erstellt. Bitte warte auf eine Antwort oder schließe das Ticket mit close.")
                return
            else:
                ticket_users[user.id] = {"open": True}
            guild = client.get_guild(ID)
            channel = await guild.create_text_channel(f"ticket-{user.name}")
            await channel.send(f"{user.mention} hat ein Ticket erstellt.")
            await user.send("Dein Ticket wurde erstellt. Ein Teammitglied wird sich in Kürze bei dir melden.")
            ticket_users[user.id]["channel_id"] = channel.id
        else:
          # doesn't start with 'ticket'
          guild = client.get_guild(ID)
          channel_id = ticket_users[user.id]["channel_id"]
          channel = guild.get_channel(channel_id)

          attachments = None
          if msg.attachments:
              attachments = msg.attachments[0]
          await user.send(msg.content, file=attachments)
          if msg.content.startswith == "close":
              del ticket_users[user.id]
              await channel.send("Ticket wurde geschlossen.")
              await user.send("Dein Ticket wurde geschlossen.")
              await channel.delete()

I would do something like this; this still retains the original functionality I think.

Upvotes: 1

Related Questions