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

Discord机器人报错:handle_response()参数不匹配问题求助

Discord机器人报错:handle_response() takes 0 positional arguments but 1 was given

问题情况

制作Discord机器人时,因权限问题安装了旧版本discord==1.7.3和discord.py==1.7.3,机器人成功上线后终端出现如下报错:

handle_response() takes 0 positional arguments but 1 was given

原代码如下:

responses模块代码

import random
from discord import Message


def handle_response() -> str:
    p_message = Message.lower()
    
    if p_message == 'hey':
        return 'hello there'
    
    if p_message == 'd20':
        return str(random.randint(1, 20))
    
    if p_message == '!help':
        return "'what now?'"

机器人主代码

from http import client
import discord
import responses


TOKEN = 'redacted' 
async def send_message(message, user_message, is_private):
    try:
        response = responses.handle_response(user_message)
        await message.author.send(response) if is_private else await  message.channel.send(response)
    except Exception as e:    
        print(e)
        
def run_discord_bot():
    TOKEN = 'redacted'
    client = discord.Client()

    @client.event
    async def on_ready():
     print(f'{client.user} has awakend!') 
     
     
     @client.event
     async def on_message(message):
         if message.author == client.user:
             return
         username = str(message.author) 
         user_message = str(message.content)
         channel = str(message.channel)
         
         print(f"{username} said: '{user_message}' ({channel})")
         if user_message[0] == '^':
             user_message = user_message[1:]
             await send_message(message, user_message, is_private=True)
         else:    
             await send_message(message, user_message, is_private=False)
             
    client.run(TOKEN)             

错误原因

  1. 参数不匹配:主代码调用responses.handle_response(user_message)时传入了1个参数,但handle_response()函数定义时未声明接收任何参数,导致参数数量不匹配报错。
  2. 类方法误用:responses模块中直接调用Message.lower(),这是对Message类调用实例方法,正确做法应该对传入的消息字符串调用lower()。
  3. 事件嵌套错误:主代码中on_message事件函数错误嵌套在on_ready函数内部,导致Discord客户端无法注册该事件,机器人将无法响应任何用户消息。

修复方案

1. 修改responses模块

调整函数定义,添加参数接收用户消息,并修正lower()调用方式,同时添加默认回复避免无返回值报错:

import random

def handle_response(user_message: str) -> str:
    p_message = user_message.lower()
    
    if p_message == 'hey':
        return 'hello there'
    
    if p_message == 'd20':
        return str(random.randint(1, 20))
    
    if p_message == '!help':
        return "'what now?'"
    
    # 添加默认回复,防止无匹配时返回None引发错误
    return "我不太理解你的意思哦!"

2. 修正机器人主代码

将on_message事件函数移至on_ready同级,修复嵌套问题,同时优化私有消息判断逻辑:

import discord
import responses

TOKEN = 'redacted' 

async def send_message(message, user_message, is_private):
    try:
        response = responses.handle_response(user_message)
        await message.author.send(response) if is_private else await message.channel.send(response)
    except Exception as e:    
        print(e)
        
def run_discord_bot():
    client = discord.Client()

    @client.event
    async def on_ready():
        print(f'{client.user} has awakend!') 

    # 事件必须与on_ready同级,不能嵌套
    @client.event
    async def on_message(message):
        if message.author == client.user:
            return
        username = str(message.author) 
        user_message = str(message.content)
        channel = str(message.channel)
        
        print(f"{username} said: '{user_message}' ({channel})")
        # 使用startswith更安全,避免空字符串引发索引错误
        if user_message.startswith('^'):
            user_message = user_message[1:]
            await send_message(message, user_message, is_private=True)
        else:    
            await send_message(message, user_message, is_private=False)
            
    client.run(TOKEN)             

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.18 11:42:52