当某人加入我的服务器中的任何语音频道时,我怎样才能给他们一个特定的角色?

问题描述 投票:0回答:0

我是编程新手,尤其是不和谐的机器人,所以我不太确定自己在做什么。

这是我的代码:

import discord

from discord.ext import commands
from discord import Member, Guild

intents = discord.Intents.all()
intents.members = True

client = commands.Bot(command_prefix =('/', '!'),intents=intents)

@client.event
async def on_ready():
    print("Bot is ready!")
    print("----------------------------------------")

voice_role_id = 123456789 # replace with the ID of the role you want to assign

@client.event
async def on_voice_state_update(member, before, after):
    # Check if the member already has the voice role
    if discord.utils.get(member.roles, id=voice_role_id) is not None:
        return

    # Check if the member joined a voice channel
    if before.channel is None and after.channel is not None:
        voice_role = discord.utils.get(member.guild.roles, id=voice_role_id)
        await member.add_roles(voice_role)

    # Check if the member left a voice channel
    if before.channel is not None and after.channel is None:
        voice_role = discord.utils.get(member.guild.roles, id=voice_role_id)
        await member.remove_roles(voice_role) 

我做错了什么?

discord discord.py bots roles
© www.soinside.com 2019 - 2024. All rights reserved.