How to generate a random ID for a user and save it in a file. This ID should be always accessable in Discord.py?

I came up with the idea of creating a unique ID system for my server with a ticket system. This is how it works: When a user joins the server, the bot automaticlly generates and ID and it stores this ID in a file with the user ID from discord. If the user creates a ticket with the Bot, the bot adds the ID that it generated for the user into a message. Also, there should be a command, that allowes me to call the user and his ID.

This is the code, I wrote to generate the random ID

random = ''.join([random.choice(string.ascii_letters + string.digits) for n in range(8)])

Now I want to access it in the ticket command like this:

@client.command(name='ticket-new') async def create_channel(ctx, channel_name='unnamed_channel', *, reason=None):     member = ctx.author     guild = ctx.guild     existing_channel = discord.utils.get(guild.channels, name=channel_name)          reason_length = len(reason)     missing_length = 50 - reason_length          if reason_length < 50:         await ctx.send(f'Hey, your ticket should be at least 50 characters long to relay it to the moderators. {missing_length} missing characters.')         return                     else:         if existing_channel:             await ctx.send(f'There is already a ticket with the name {channel_name}. Please selcet another name.')         if not existing_channel:             channel = await guild.create_text_channel(channel_name)              await channel.set_permissions(guild.default_role, read_messages=False)             await channel.set_permissions(ctx.author, read_messages=True)             embed = discord.Embed(title=f"Ticket: {channel_name}", description=f"**Description**: {reason}\n**ID**: {random}")             await channel.send(embed=embed)             await member.send("Ticket: `" + channel_name + '` erstellt!')                          print(f'Creating a new ticket channel: {channel_name}, by {member.name}, at {ctx.message.created_at}, on: {guild.name}, ID: {random}') 

Well … my plan now is it to store random for user in a file. Also … maybe how to access the information via command.

Add Comment
0 Answer(s)

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.