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

如何使用discord.py制作断开用户语音频道连接的命令

使用discord.py实现断开指定用户语音连接的命令

前置要求

  • 你的机器人必须被授予*移动成员(Move Members)*权限
  • 机器人的最高角色权限等级,需要高于你要断开连接的目标用户的最高角色权限等级

核心实现逻辑

discord.py中没有专门的「断开语音连接」独立方法,你只需要给move_to方法传入None作为目标频道参数,即可直接断开用户的语音连接,而非移动到其他频道:
await member.move_to(None)

完整命令示例(前缀命令版)

import discord
from discord.ext import commands

# 初始化机器人,这里用!作为命令前缀,开启消息内容意图
intents = discord.Intents.default()
intents.message_content = True
bot = commands.Bot(command_prefix="!", intents=intents)

# 断开用户语音的命令,默认只有管理员可以调用
@bot.command(name="kickvc")
@commands.has_permissions(administrator=True)
async def kick_from_voice(ctx: commands.Context, member: discord.Member):
    # 先判断目标用户是否在语音频道中
    if not member.voice:
        await ctx.send(f"用户{member.mention}当前没有连接到任何语音频道")
        return
    try:
        # 传入None直接断开连接,无移动操作记录
        await member.move_to(None)
        await ctx.send(f"已断开{member.mention}的语音连接")
    except discord.Forbidden:
        await ctx.send("权限不足,无法操作该用户,请确认机器人权限高于目标用户且拥有移动成员权限")

# 运行机器人
bot.run("你的机器人TOKEN")

补充说明

  • 如果你使用的是discord.py v2.x的斜杠命令,只需要将命令装饰器替换为对应斜杠命令的装饰器即可,核心的await member.move_to(None)逻辑完全通用
  • 如果不需要管理员权限限制,删除@commands.has_permissions(administrator=True)装饰器即可,也可以自定义权限校验规则,比如只允许特定角色的用户调用

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.19 22:15:04