You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何让Discord机器人选中新建文本频道并发送指定消息(含工单场景)

Hey there! Let's tackle your two Discord bot questions one by one.

问题1:选中新建频道并发送消息

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.

问题2:优化工单机器人的频道消息发送

Your existing code is already 90% there—just a few tweaks will get you the functionality you want:

  1. Store the created channel as a variable (so you can send messages to it)
  2. Use the mention property of roles/users to properly tag them
  3. Fix the add_roles syntax (your original code was missing the role collection to search through)
  4. Streamline the ticketNumber handling (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_channel to store the created channel, allowing direct message sending
  • Leveraged support_role.mention and ctx.author.mention to ensure proper notifications for the support team and ticket requester
  • Simplified ticketNumber handling by working directly with integers
  • Corrected the add_roles syntax to properly search the server's role list for the target ID

内容的提问来源于stack exchange,提问作者Pepper

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.14 07:09:46