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

Python开发discord.py机器人遇参数错误:change_presence()传参过多

解决discord.py中change_presence()参数错误的问题

嘿,我之前也踩过这个坑!这个报错change_presence() takes 1 positional argument but 2 were given本质上是因为你调用方法的方式不符合discord.py(尤其是v2.x及以上版本)的要求,下面给你拆解原因和解决办法:

常见错误原因

  • 没使用关键字参数:change_presence()只把self(也就是你的client实例)当作位置参数,其他所有参数(比如活动、状态)都需要用关键字参数传递,直接传位置参数会被当成多余的第二个参数。
  • 忘了加await:新版本discord.py里change_presence是异步方法,必须在异步函数里用await调用,不然直接调用的话,会把生成的协程对象当成第二个参数传入,触发报错。
  • 旧版本写法残留:如果是从v1.x升级过来的,旧版里的game参数已经被activity取代,而且旧版可能不需要await,但新版必须同步调整。

正确的写法示例

示例1:设置普通游戏活动

import discord
from discord.ext import commands

intents = discord.Intents.default()
intents.message_content = True  # 根据你的需求开启对应的权限

client = commands.Bot(command_prefix='!', intents=intents)

@client.event
async def on_ready():
    # 用activity关键字指定活动,并且必须加await
    await client.change_presence(activity=discord.Game(name="自定义机器人开发"))
    print(f"已登录为 {client.user}")

client.run("你的机器人Token")

示例2:设置流媒体活动(比如Twitch)

@client.event
async def on_ready():
    stream_activity = discord.Streaming(
        name="我的直播",
        url="https://twitch.tv/你的直播间地址"
    )
    # 同时指定状态和活动,都用关键字参数
    await client.change_presence(activity=stream_activity, status=discord.Status.online)
    print("机器人上线啦!")

快速排查步骤

  1. 检查调用change_presence的代码是否在异步函数里(比如on_ready、@commands.command()装饰的命令函数)。
  2. 给change_presence前面加上await关键字。
  3. 确保所有传入的参数都用参数名=值的形式,比如activity=...,不要直接传对象。

按照这个调整后,你的报错应该就能解决啦!

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.19 10:25:24