Search code examples
pythondiscorddiscord.pybots

"before.channel" and "after.channel" are marked as Nonetype in on_voice_state_update() func


This function have to make a cloned voice channel, when user connect to the special one (it's id is 1191737629281107978), and delete, when there is noone.

@client.event
async def on_voice_state_update(member, before, after):
    possible_channel_name = f'new {member}`s channel'
    if after.channel.id == 1191737629281107978:
        temp_channel = await after.channel.clone(name=possible_channel_name)
        await member.move_to(temp_channel)
    if before.channel.name == possible_channel_name and len(before.channel.members) == 0:
        await before.channel.delete()

It particulary works, it makes a new channel, but doesn't delete it in the end, and goes with 2 errors:

line 58, in on_voice_state_update
    if after.channel.id == 1191737629281107978:
       ^^^^^^^^^^^^^^^^
AttributeError: 'NoneType' object has no attribute 'id'

and

line 61, in on_voice_state_update
    if before.channel.name == possible_channel_name and len(before.channel.members) == 0:
       ^^^^^^^^^^^^^^^^^^^

I looked up, that there can be problems with intents, but it has

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

client = commands.Bot(intents=intents, command_prefix="/")

and everything that can be turned on discord development site (Privileged Gateway Intents too). Also after this I recconected bot to the server several times. I'm using discord.py and these bibliotecks:

import discord
from discord.ext import commands
from datetime import datetime
from PIL import Image, ImageDraw
import asyncio
import configure
import sqlite3
import requests
import io

Solution

  • You can add a check to make sure that before.channel and after.channel not None :

    @client.event
    async def on_voice_state_update(member, before, after):
        possible_channel_name = f'new {member}`s channel'
        if after.channel and after.channel.id == 1191737629281107978:
            temp_channel = await after.channel.clone(name=possible_channel_name)
            await member.move_to(temp_channel)
        if before.channel and before.channel.name == possible_channel_name and len(before.channel.members) == 0:
            await before.channel.delete()