“before.channel”和“after.channel”在 on_voice_state_update() func 中被标记为 Nonetype

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

此功能必须在用户连接到特定通道(其id为1191737629281107978)时克隆语音通道,并在无人时删除。

@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()

它特别有效,它创建了一个新通道,但最终没有删除它,并且出现了 2 个错误:

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

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

我抬头一看,意图可能存在问题,但事实确实如此

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

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

以及可以在不和谐开发网站上打开的所有内容(特权网关意图也是如此)。 此后我多次将机器人重新连接到服务器。 我正在使用 discord.py 和这些书目:

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
python discord discord.py bots
1个回答
0
投票

您可以添加检查以确保

before.channel
after.channel
不是
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()
© www.soinside.com 2019 - 2024. All rights reserved.