Discord bot在Python的帮助下使用语音聊天来静音人们的声音

2024-09-21 05:55:49 发布

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

我正在尝试制作一个Discord机器人,它使用语音聊天使参与者的声音静音

为此,我使用Python

这是我的代码,但它没有按预期工作

import discord 
from discord.ext import commands 

client = commands.Bot(command_prefix=" !") 

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

@client.command() 
async def mute(ctx):
     voice_client = ctx.guild.voice_client
     if not voice_client:
         return
     channel = voice_client.channel
     await voice_client.voice_state(ctx.guild.id, channel.id, self_mute=True)
 
client.run(My Token)

我的想法是:

我将输入命令:!muteall\

机器人将使语音聊天中的所有参与者静音

我将输入命令:!unmuteall\

机器人将取消语音聊天中所有参与者的静音


Tags: importclientasyncdefchannel机器人静音语音
1条回答
网友
1楼 · 发布于 2024-09-21 05:55:49

在我们讨论问题的关键之前,请在前缀上写一个简短的词:
您的命令前缀是 !,前面有一个空格。我不知道这是否是有意的,但如果是,在我的测试中,我发现使用它是不可能的。不一致带开头的空格,所以我所有的消息 !test都显示为!test

修复此问题后,尝试使用!mute命令会产生错误:
'VoiceClient' object has no attribute 'voice_state' 事实上,我在文档中找不到类似的内容。我花了很多时间寻找,但我可能已经得到了你想要的

client = commands.Bot(command_prefix="!") 

@client.command() 
async def mute(ctx):
        voice_client = ctx.guild.voice_client #get bot's current voice connection in this guild
        if not voice_client:  #if no connection...
            return  #probably should add some kind of message for the users to know why nothing is happening here, like ctx.send("I'm not in any voice channel...")
        channel = voice_client.channel #get the voice channel of the voice connection
        people = channel.members #get the members in the channel
        for person in people: #loop over those members
            if person == client.user: #if the person being checked is the bot...
                continue #skip this iteration, to avoid muting the bot
            await person.edit(mute=True, reason="{} told me to mute everyone in this channel".format(ctx.author))
            #edit the person to be muted, with a reason that shows in the audit log who invoked this command. Line is awaited because it involves sending commands ("mute this user") to the server then waiting for a response.

您的机器人需要权限才能禁用用户

相关问题 更多 >

    热门问题