from discord.ext import commands
intents = discord.Intents.default()
intents.typing = False
intents.presences = False
bot = commands.Bot(command_prefix='!', intents=intents)
tickets = {}
class Ticket:
def __init__(self, user):
self.user = user
self.channel = None
self.messages = []
self.is_closed = False
@bot.event
async def on_ready():
print(f'Logged in as {bot.user.name}')
@bot.command(name='closeticket', description='Close a support ticket')
@commands.has_permissions(administrator=True)
async def close_ticket(ctx, member: discord.Member):
if member.id in tickets:
ticket = tickets[member.id]
if not ticket.is_closed:
ticket.is_closed = True
if ticket.channel:
await ticket.channel.send("This ticket has been closed by the administration.")
await ticket.channel.delete()
del tickets[member.id]
await member.send("Your ticket has been closed.")
else:
await ctx.send("The specified user does not have an active ticket to close.")
else:
await ctx.send("The specified ticket is already closed.")
else:
await ctx.send("The specified user does not have an active ticket.")
@bot.event
async def on_message(message):
if isinstance(message.channel, discord.DMChannel) and not message.author.bot:
if message.author.id not in tickets:
guild = discord.utils.get(bot.guilds, id=SERVER_ID)
if guild:
member = guild.get_member(message.author.id)
if member:
new_ticket = Ticket(message.author)
overwrites = {
guild.default_role: discord.PermissionOverwrite(read_messages=False),
member: discord.PermissionOverwrite(read_messages=True)
}
new_ticket.channel = await guild.create_text_channel(f'ticket-{message.author.display_name}', overwrites=overwrites)
tickets[message.author.id] = new_ticket
await message.author.send("Your ticket has been opened automatically. A support team member will be with you shortly.")
else:
await message.author.send("Sorry, I couldn't find you on the server.")
else:
await message.author.send("Sorry, I couldn't find the server.")
else:
ticket = tickets[message.author.id]
if ticket.channel and not ticket.is_closed:
ticket.messages.append(message)
await ticket.channel.send(f'**{message.author.display_name}:** {message.content}')
else:
await message.author.send("Your ticket is not yet assigned to a support channel or has been closed.")
await bot.process_commands(message)
@bot.event
async def on_message_edit(before, after):
if isinstance(after.channel, discord.TextChannel) and after.channel in [t.channel for t in tickets.values()] and not after.author.bot:
ticket = next((t for t in tickets.values() if t.channel == after.channel), None)
if ticket:
user = ticket.user
user_dm = user.dm_channel or await user.create_dm()
await user_dm.send(f'**Support:** {after.content}')
bot.run('TOKEN')
I tried everything that came to my mind. Expected is the following result:
- The user sends a DM to the bot
- The bot creates a channel on the server and sends this message there
- The server admin sends a message on the bot channel
- The bot forwards the message as a DM back to the person who contacted it
- Actions of the !closeticket {user} command (it works)