【问题标题】:Discord bot won't mute all peopleDiscord 机器人不会让所有人静音
【发布时间】:2020-12-05 03:14:46
【问题描述】:

它只会在不应该的时候静音我和它自己,bot 具有最高的角色并且在加入的语音频道中也有权限。有什么想法吗?

@bot.command() 
async def mute(ctx):
        voice_client = ctx.guild.voice_client #
        if not voice_client:  
            return
        channel = voice_client.channel 
        people = channel.members 
        for person in people: 
            if person == client.user: 
                continue
            await person.edit(mute=True, reason="{} told me to mute everyone in this channel".format(ctx.author))

编辑完整代码:

import discord
from discord.ext import commands
import os
from discord.utils import get


client = discord.Client()

DISCORD_TOKEN = os.getenv("TokenGoesHere")


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 disconnect(ctx):
    channel = ctx.message.author.voice.channel
    await channel.disconnect()



@bot.command() 
@commands.has_permissions(administrator=True)
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.

@bot.command() 
@commands.has_permissions(administrator=True)
async def unmute(ctx):
        voice_client = ctx.guild.voice_client
        if not voice_client:  
            return
        channel = voice_client.channel 
        people = channel.members 
        for person in people: 
            if person == client.user: 
                continue
            await person.edit(mute=False, reason="{} told me to mute everyone in this channel".format(ctx.author))



bot.run("TokenGoesHere")

希望这会有所帮助,Bot 有时只会静音自己或仅静音自己和其他用户,但特别是某个用户... 它只会在不应该的时候使我和它自己静音,机器人具有最高的角色,并且在加入的语音频道中也有权限。有什么想法吗?

【问题讨论】:

  • 您是否在代码中定义了意图?
  • 哦不..我该怎么做?
  • 我添加了完整代码

标签: python discord discord.py


【解决方案1】:

您必须定义使用某些事件的意图,例如on_messageguild.memberschannel.members 等函数。

# Here is your imports
import discord

intents = discord.Intents().all()
bot = commands.Bot(command_prefix='', intents=intents)
# Rest of your code

此外,您必须从Discord Developer Portal 激活意图。

    • 转到您的机器人应用程序。
    • 转到 Bot -> Privileged Gateway Intents
    • 激活Presence IntentServer Members Intent

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-09-22
    • 2019-07-28
    • 1970-01-01
    • 1970-01-01
    • 2020-12-19
    • 2021-05-27
    • 2021-04-09
    • 2018-05-18
    相关资源
    最近更新 更多