将机器人解除静音当加入AFK频道

huangapple go评论61阅读模式
英文:

Get bot to unmute self when joining an afk channel

问题

I am working on a discord bot for my private server that plays an mp3 file when someone joins the afk channel. The afk channel automatically sets the user to muted when joining, this includes the bot. I am having trouble getting the bot to unmute after joining the channel. The bot has admin permissions so it should be able to unmute itself.

@client.event
async def on_voice_state_update(member, before, after):
    if before.channel and after.channel:
        channel = after.channel
        randomSong = random.choice(os.listdir("file"))
        if channel.id == #id: 
            voice = await channel.connect()
            source = FFmpegPCMAudio(f"{randomSong}")
            voice.play(source)
            print(randomSong)

After reading the documentation, connect() can take a self_mute parameter that I set to False, but the bot is still muted when it joins the voice channel. I also tried to look up the solution and found this, but it didn't really help me.

@client.event
async def on_voice_state_update(member, before, after):
    if before.channel and after.channel:
        channel = after.channel
        randomSong = random.choice(os.listdir("file"))
        if channel.id == #id: 
            voice = await channel.connect(**self_mute=False**)
            source = FFmpegPCMAudio(f"{randomSong}")
            voice.play(source)
            print(randomSong)
英文:

I am working on a discord bot for my private server that plays an mp3 file when someone joins the afk channel. The afk channel automatically sets the user to muted when joining, this includes the bot. I am having trouble getting the bot to unmute after joining the channel. The bot has admin permissions so it should be able to unmute its self.

@client.event
async def on_voice_state_update(member, before, after):
    if before.channel and after.channel:
       channel = after.channel
       randomSong= random.choice(os.listdir("file"))
       if channel.id == #id: 
        voice = await channel.connect()
        source = FFmpegPCMAudio(f"{randomSong}")
        voice.play(source )
        print(randomSong)

After reading the documentation, connect() can take a self_mute parameter that I set the False but the bot is still muted when it joins the voice channel. I also tried to look up the solution and found this but it didn't really help me.

@client.event
async def on_voice_state_update(member, before, after):
    if before.channel and after.channel:
       channel = after.channel
       randomSong= random.choice(os.listdir("file"))
       if channel.id == #id: 
        voice = await channel.connect(**self_mute=False**)
        source = FFmpegPCMAudio(f"{randomSong}")
        voice.play(source )
        print(randomSong)

答案1

得分: 1

我应该从一开始就说,这不是最佳方法,但这是我做的方式:

guild = client.get_guild(server_id)
bot_user = guild.me
await bot_user.edit(mute=False)

self_mute 参数只会改变客户端静音。如果你加入一个AFK频道,你将被服务器静音。你需要获取服务器和机器人的ID,并将它们放在这里。希望这有所帮助!

英文:

I should say from the start this is not the best way, but this is how I did it:

guild = client.get_guild(server_id)
bot_user = guild.get_member(bot_id)
await bot_user.edit(mute=False)

self_mute argument only changes the client sides mute. If you join an afk channel, you are server muted instead. You need to get both the server's and the bot's IDs and put them there. Hope this helps!

EDIT:

Instead of bot_user = guild.get_member(bot_id) you can use bot_user = guild.me

huangapple
  • 本文由 发表于 2023年2月10日 02:37:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/75403030.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定