Discord机器人可以在Python的帮助下使用语音聊天使人们的声音静音

时间:2020-09-24 05:02:21

标签: python discord

我正在尝试创建一个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 \

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

1 个答案:

答案 0 :(得分:1)

在问题关键之前,请在您的前缀上输入一个简短的单词:
您的命令前缀为 !,并带有空格。我不知道这是否是故意的,但如果是故意的,在我的测试中,我发现使用它是不可能的。不和谐地去除了开头的空格,所以我所有的消息 !test都以!test的形式出现。

解决此问题后,尝试使用!mute命令会产生错误:
'VoiceClient' object has no attribute 'voice_state' 确实,我无法在文档中找到类似的内容。我花了很多时间进行搜索,但是我可能found有您想要的东西。

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.

您的漫游器将需要使用户静音的权限。