如何修改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. Foradd 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
相关产品推荐
相关产品推荐

