Discord bot使语音频道中的所有人静音不工作(Python)

2024-09-30 04:27:25 发布

您现在位置:Python中文网/ 问答频道 /正文

因此,我想为我的discord服务器创建一个bot,以便它加入我当前所在的一个语音频道,并根据命令对所有成员进行静音和取消静音,如vcmutevcunmute,但缺少一些内容,并且没有任何工作。这是我写的代码

import discord
from discord.ext import commands
import os


client = discord.Client()

DISCORD_TOKEN = os.getenv("myToken")


bot = commands.Bot(command_prefix="$")

@client.event
async def on_ready():
    print('BOT ACTIVATED')

@bot.command()
async def join(ctx):
    channel = ctx.message.author.voice.channel
    await channel.connect()

@bot.command()
async def vcmute(ctx):
    vc = ctx.author.voice.channel
    for member in vc.members:
        await member.edit(mute=True)

@bot.command()
async def vcunmute(ctx):
    vc = ctx.author.voice.channel
    for member in vc.members:
        await member.edit(mute=False)




bot.run("myToken")

Tags: importasyncdefbotchannel静音awaitcommand
1条回答
网友
1楼 · 发布于 2024-09-30 04:27:25

代码对我来说是有效的,bot可能缺少管理员权限,这会阻止它对成员进行静音和取消静音

您还可以添加^{}装饰器,让它检查管理员权限,例如您的代码:

@bot.command()
@commands.has_permissions(administrator=True)
async def join(ctx):
    channel = ctx.message.author.voice.channel
    await channel.connect()

@bot.command()
@commands.has_permissions(administrator=True)
async def vcmute(ctx):
    vc = ctx.author.voice.channel
    for member in vc.members:
        await member.edit(mute=True)

@bot.command()
@commands.has_permissions(administrator=True)
async def vcunmute(ctx):
    vc = ctx.author.voice.channel
    for member in vc.members:
        await member.edit(mute=False)

相关问题 更多 >

    热门问题