Discord.py using on_voice_state_update

I am current trying to make a Discord bot that joins VC when a certain person joins and then leaves VC when they leave.

The bot should ONLY be in VC when the specific user is in VC, and then leave instantly when said user disconnects.

The problem I am having is trying to figure out how to make the bot detect the user leaving and leave.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>@bot.event
async def on_voice_state_update(member, before, after):
targetID = bot.get_user(USERID)
if after.channel is not None and member.id == targetID.id:
await member.voice.channel.connect()
</code>
<code>@bot.event async def on_voice_state_update(member, before, after): targetID = bot.get_user(USERID) if after.channel is not None and member.id == targetID.id: await member.voice.channel.connect() </code>
@bot.event

async def on_voice_state_update(member, before, after):
    targetID = bot.get_user(USERID)

    if after.channel is not None and member.id == targetID.id:
        await member.voice.channel.connect()

This is what I use to make the bot join the VC and there seem to be no issues, but no matter what I try I cannot figure out how to make the bot leave once the said user leaves VC.

Any help is appreciated, thank you.

Any time i’ve been able to make the bot leave, it leaves the instant is joins, I know the reason for this but I just cant figure out how to prevent it, or do it another way to make the bot only leave when the user leaves.

New contributor

RS F is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

1

  1. Your bot.get_user(USERID) returned a NoneType, which would of course not have an ID to compare with the member.id. So, I used await with bot.fetch_user. But if it works on your end, then that’s that.
  2. Because getting the id of the user didn’t work for me, I stuck to comparing the user objects.
  3. The actual answer to your question is as simple as using a list to store the VC the user joins, and then exiting from the VC when they leave. The reason why you need a list is because there isn’t a way (at least that I know of) to find the last VC a user was in. Having a list also means that you could potentially make the code watch for multiple people in different VCs.

Here’s my code:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>
VCs = {} #VC list
@bot.event
async def on_voice_state_update(member, before, after):
target = await bot.fetch_user(USERID)
if after.channel is not None and member == target:
VCs[member.id] = member.voice.channel
await member.voice.channel.connect()
elif after.channel is None and member == target:
for i in bot.voice_clients: #Check through all VCs bot is currently in
if i.channel == VCs[member.id]: #If VC is the one the user was in
VCs.pop(member.id) #Remove the VC from the list
await i.disconnect(force=False) #Disconnect from the VC
</code>
<code> VCs = {} #VC list @bot.event async def on_voice_state_update(member, before, after): target = await bot.fetch_user(USERID) if after.channel is not None and member == target: VCs[member.id] = member.voice.channel await member.voice.channel.connect() elif after.channel is None and member == target: for i in bot.voice_clients: #Check through all VCs bot is currently in if i.channel == VCs[member.id]: #If VC is the one the user was in VCs.pop(member.id) #Remove the VC from the list await i.disconnect(force=False) #Disconnect from the VC </code>

VCs = {} #VC list

@bot.event
async def on_voice_state_update(member, before, after):
    target = await bot.fetch_user(USERID)

    if after.channel is not None and member == target:
        VCs[member.id] = member.voice.channel
        await member.voice.channel.connect()

    elif after.channel is None and member == target:
        for i in bot.voice_clients: #Check through all VCs bot is currently in
            if i.channel == VCs[member.id]: #If VC is the one the user was in
                VCs.pop(member.id) #Remove the VC from the list
                await i.disconnect(force=False) #Disconnect from the VC

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật