Discord机器人可以在Python的帮助下使用语音聊天使人们的声音静音 [英] Discord bot to mute the voice of people Using Voice Chat with the help of Python

查看:239
本文介绍了Discord机器人可以在Python的帮助下使用语音聊天使人们的声音静音的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to make a Discord bot that mutes the voice of the participant using voice chat.

为此,我正在使用Python。

For this, I am using Python.

这是我的代码,但是无法正常工作。

This is my code, but it is not working as expected.

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 \

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

And the bot will mute all the participants in the voice chat

命令我将输入:!unm uteall \

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

And the bot will unmute all the participants in the voice chat.

推荐答案

在开始讨论问题之前,请在您的前缀上输入一个简短单词:

您的命令前缀为预先留一个空格。我不知道这是否是故意的,但如果是故意的,在我的测试中,我发现使用它是不可能的。不和谐去除开头的空格,所以我所有的消息!test 都以!test 的形式出现。

Before we get to the crux of the question, a short word on your prefix:
Your command prefix is ! with a space beforehand. I do not know if this was intentional or not, but if it was, in my testing I found using this to be impossible. Discord strips beginning whitespace so all my messages !test come out as !test.

修复此问题后,尝试使用!mute 命令会产生错误:

'VoiceClient'对象具有没有属性'voice_state'
的确,我无法在文档中找到类似的内容。我花了很多时间搜索,但我可能有找到了您想要的东西。

After fixing this, attempting to use the !mute command produces an error:
'VoiceClient' object has no attribute 'voice_state' Indeed, I have been unable to find anything like this in the docs. It took me a lot of searching, but I may have found what you wanted.

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.

您的机器人将需要使用户静音的权限。

Your bot will need permissions to mute users.

这篇关于Discord机器人可以在Python的帮助下使用语音聊天使人们的声音静音的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆