如何让Discord机器人选中新建文本频道并发送指定消息(含工单场景)
Hey there! Let's tackle your two Discord bot questions one by one.
Actually, it's simpler than you might think—when you call the create_text_channel() method, it returns the newly created channel object directly. You just need to store this object in a variable, then use that variable to call the send() method to post messages in the new channel.
Here's a quick example:
# Assuming you already have the guild object and necessary permissions/overwrites set up new_channel = await guild.create_text_channel("my-new-channel") # Send a message directly using the returned channel object await new_channel.send("Hey there! This is the brand new channel.")
The key takeaway is you don't need to "select" the channel afterward. Reusing the object returned by the creation operation is the most efficient approach. If you need to interact with this channel later, you can either keep the variable or look it up via discord.utils.get() using its name/ID, but storing the object immediately is the best practice.
Your existing code is already 90% there—just a few tweaks will get you the functionality you want:
- Store the created channel as a variable (so you can send messages to it)
- Use the
mentionproperty of roles/users to properly tag them - Fix the
add_rolessyntax (your original code was missing the role collection to search through) - Streamline the
ticketNumberhandling (no need to convert between int and string repeatedly)
Here's the revised complete code:
# Initialize the ticket number at the top of your script (e.g., start at 1) ticketNumber = 1 @client.command(pass_context=True) async def ticket(ctx, *, reason): global ticketNumber # Fetch the Support role support_role = discord.utils.get(ctx.guild.roles, name="Support") if not support_role: await ctx.send("Oops! Couldn't find the Support role. Make sure it exists.") return # Set up channel permission overwrites overwrites = { ctx.guild.default_role: discord.PermissionOverwrite(read_messages=False), ctx.guild.me: discord.PermissionOverwrite(read_messages=True, send_messages=True), support_role: discord.PermissionOverwrite(read_messages=True, send_messages=True), ctx.author: discord.PermissionOverwrite(read_messages=True, send_messages=True) } # Fetch the Tickets category category = discord.utils.get(ctx.guild.categories, name="Tickets") if not category: await ctx.send("Error: Couldn't locate the Tickets category. Please create it first.") return # Create the ticket channel and save the object new_ticket_channel = await ctx.guild.create_text_channel( f'Ticket-{ticketNumber}', category=category, overwrites=overwrites ) # Send the required notification in the new ticket channel await new_ticket_channel.send( f"{support_role.mention}, {ctx.author.mention} 发起了一个关于**{reason}**的工单。" ) # Increment the ticket number for the next request ticketNumber += 1 # Send confirmation to the user await ctx.author.send("Your ticket has been created! A support team member will reach out as soon as possible. Thank you.") await ctx.send(f"工单已创建:{new_ticket_channel.mention}") # Fix the add_roles logic (replace with your actual role ID) target_role = discord.utils.get(ctx.guild.roles, id=1234567890) # Swap with your role's ID if target_role: await ctx.author.add_roles(target_role) else: await ctx.send("Oops! Couldn't find the target role to assign.")
Key improvements explained:
- Added checks for missing roles/categories to prevent crashes and give clear error messages
- Used
new_ticket_channelto store the created channel, allowing direct message sending - Leveraged
support_role.mentionandctx.author.mentionto ensure proper notifications for the support team and ticket requester - Simplified
ticketNumberhandling by working directly with integers - Corrected the
add_rolessyntax to properly search the server's role list for the target ID
内容的提问来源于stack exchange,提问作者Pepper

