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

如何修改Discord Python机器人add命令:无需括号直接输入数值

Fixing the Discord Bot Command to Use add 500 Instead of add (500)

Hey there! It's easy to tweak your code so you can trigger the command with a simple add 500 instead of needing parentheses. Let's walk through the changes:

The Issue with Your Current Code

Your existing code relies on hunting for parentheses to pull out the number, which restricts how users can input the command. Instead, we can split the message content directly into the command and its parameter—no brackets required.

The Updated Code

Here's the modified version that works seamlessly with add 500:

if message.content.lower().startswith('add'):
    # Split the message into parts using spaces as separators
    message_parts = message.content.split()
    
    # Make sure the user provided a number after the command
    if len(message_parts) >= 2:
        # Extract the second part (the number)
        text_out = message_parts[1]
        # Optional: Convert the text to an integer if you need to use it numerically
        # num = int(text_out)
        # Add your logic here (e.g., saving the number, replying to the user)
    else:
        # Handle cases where the user only typed "add" without a number
        await message.channel.send("Oops! Please include a number after 'add' — try something like `add 500`")

How This Works

  • split() breaks the message into a list of strings, splitting on spaces. For add 500, this gives us ['add', '500'].
  • We check if there are at least 2 parts to avoid errors if the user forgets to include the number.
  • message_parts[1] grabs the second element in the list, which is your desired number.

This approach also handles extra spaces gracefully — so add 500 (with multiple spaces) will work just as well as add 500.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.27 07:28:28