【问题标题】:Discord bot to mute the voice of people Using Voice Chat with the help of PythonDiscord bot 在 Python 的帮助下使用语音聊天来静音人们的声音
【发布时间】:2021-01-10 08:32:31
【问题描述】:

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

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

【问题讨论】:

  • 你能显示你的整个代码文件,减去令牌吗?你有 @client.command() 上面的装饰器 async def mute 吗? main_ws 应该在 voice_client.main_ws.voice_state() 中是什么?我在 discord.py 文档中找不到它
  • 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)

标签: python discord


【解决方案1】:

在我们进入问题的症结之前,在你的前缀上写一个简短的词:
你的命令前缀是 !,前面有一个空格。我不知道这是否是故意的,但如果是,在我的测试中我发现使用它是不可能的。 Discord 去除开头的空白,因此我的所有消息 !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.

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

【讨论】:

    猜你喜欢
    • 2021-10-06
    • 2019-07-31
    • 2019-06-29
    • 2017-11-20
    • 1970-01-01
    • 2020-09-12
    • 2022-12-11
    • 2021-01-12
    • 1970-01-01
    相关资源
    最近更新 更多