此功能必须在用户连接到特定通道(其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
您可以添加检查以确保
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()