Python 如何使用discord.py中的通道id设置通道对象?
大家好,我正在尝试制作一个函数来检查MyDiscord服务器中的几个频道是否已满,但我不知道如何指定它们。 我最好的想法是使用这个想法,但无法将其设置为通道对象,那么我如何才能做到这一点,或者还有其他好的想法吗Python 如何使用discord.py中的通道id设置通道对象?,python,python-3.x,discord,discord.py,Python,Python 3.x,Discord,Discord.py,大家好,我正在尝试制作一个函数来检查MyDiscord服务器中的几个频道是否已满,但我不知道如何指定它们。 我最好的想法是使用这个想法,但无法将其设置为通道对象,那么我如何才能做到这一点,或者还有其他好的想法吗 def full(channel): while True: if len(channel.voice_members) == channel.user_limit: print("Wooo There is a full channel.")
def full(channel):
while True:
if len(channel.voice_members) == channel.user_limit:
print("Wooo There is a full channel.")
asyncio.sleep(10)
但我不知道如何设置通道参数。
提前感谢您的帮助。您可以通过ID或名称获取通道对象 使用
discord.utils.get()
或者您可以使用discord.get_channel(id)
(如果您有频道的id)
例如,如果您有要检查的频道名称列表:
channel_names = ['channel1', 'channel2', 'channel3']
for ch in channel_names:
channel = discord.utils.get(server.channels, name=ch, type="ChannelType.voice")
full(channel)
更多信息,请参见:
如果希望将其作为命令,还可以利用:
channel_names = ['channel1', 'channel2', 'channel3']
for ch in channel_names:
channel = discord.utils.get(server.channels, name=ch, type="ChannelType.voice")
full(channel)
@bot.command(pass_context=True)
async def full(ctx, channel: discord.Channel):
if len(channel.voice_members) == channel.user_limit:
await bot.say("{} is full".format(channel.name))
else:
await bot.say("{} is not full".format(channel.name))