我正在使我的机器人不和谐,我想这样做,当用户单击某个语音通道时,会为他创建一个新的语音通道,并在退出时将其删除。这是代码:
import discord
from discord.ext import commands
from discord.utils import get
import asyncio
TOKEN = 'xxxx'
bot = commands.Bot(command_prefix='!')
@bot.event
async def on_voice_state_update(member, before, after):
if after.channel != None:
if after.channel.id == 700246237244555338:
for guild in bot.guilds:
maincategory = discord.utils.get(
guild.categories, id=700246237244555336)
channel2 = guild.create_voice_channel(name=f'канал {member.display_name}', category=maincategory)
await channel2.set_permissions(member, connect=True, mute_members=True, manage_channels=True)
await member.move_to(channel2)
def check(x, y, z):
return len(channel2.members) == 0
await bot.wait_for('voice_state_update', check=check)
await channel2.delete()
# RUN
bot.run(TOKEN)
但是我有错误...
Ignoring exception in on_voice_state_update
Traceback (most recent call last):
File "C:\Users\asus\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\discord\client.py", line 333, in _run_event
await coro(*args, **kwargs)
File "jett.py", line 190, in on_voice_state_update
await member.move_to(channel2)
File "C:\Users\asus\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\discord\member.py", line 626, in move_to
await self.edit(voice_channel=channel, reason=reason)
File "C:\Users\asus\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\discord\member.py", line 592, in edit
payload['channel_id'] = vc and vc.id
AttributeError: 'coroutine' object has no attribute 'id'
C:\Users\asus\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\discord\client.py:340: RuntimeWarning: coroutine 'Guild.create_voice_channel' was never awaited
pass
RuntimeWarning: Enable tracemalloc to get the object allocation traceback
您能帮我解决这个问题还是给我发送临时语音的工作代码
答案 0 :(得分:0)
RuntimeWarning: Enable tracemalloc to get the object allocation traceback
通常意味着您忘记将await关键字添加到异步函数中。有问题的异步功能很可能是create_voice_channel
,因为文档说它是async function。
要解决此问题,您需要在函数调用之前添加await关键字,如下所示:
channel2 = await guild.create_voice_channel(name=f'канал {member.display_name}', category=maincategory)