【问题标题】:How to set channel object using the channel id in discord.py?如何使用 discord.py 中的频道 ID 设置频道对象?
【发布时间】:2018-12-08 15:32:45
【问题描述】:
大家好,我正在尝试创建一个函数来检查我的不和谐服务器中的几个频道是否已满,但我不知道如何指定它们。
我的最佳想法是使用该想法,但无法将其设置为通道对象,那么我该如何做到这一点,或者还有其他好的想法吗?
def full(channel):
while True:
if len(channel.voice_members) == channel.user_limit:
print("Wooo There is a full channel.")
asyncio.sleep(10)
但我不知道如何设置通道参数。
提前感谢您的帮助。
【问题讨论】:
标签:
python
python-3.x
discord
discord.py
【解决方案1】:
您可以通过 ID 或名称获取通道对象。
使用discord.utils.get() 按名称返回频道(例如语音频道):
channel = discord.utils.get(server.channels, name="Channel_name_here", type="ChannelType.voice")
如果您有频道的 ID,也可以使用discord.get_channel(id)。
例如,如果您有一个要检查的频道名称列表:
channel_names = ['channel1', 'channel2', 'channel3']
for ch in channel_names:
channel = discord.utils.get(server.channels, name=ch, type="ChannelType.voice")
full(channel)
在documentation中查看更多信息:
discord.get_channel()
discord.utils.get()
【解决方案2】:
如果你希望这是一个命令,你也可以利用converters:
@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))