我目前拥有:
music.find_channel()
url = str(sounds[random.randint(0, 3)])
voice_channel = ctx.author.voice.channel
FFMPEG_OPTIONS = {'before_options': '-reconnect 1 -reconnect_streamed 1 -reconnect_delay_max 5', 'options': '-vn'}
YDL_OPTIONS = {'format':"bestaudio"}
with youtube_dl.YoutubeDL(YDL_OPTIONS) as ydl:
info = ydl.extract_info(url, download=False)
url2 = info['formats'][0]['url']
source = await discord.FFmpegOpusAudio.from_probe(url2, **FFMPEG_OPTIONS)
print(ctx.author.voice.channel)
print(type(voice_channel))
if ctx.voice_client is None:
await voice_channel.connect()
else:
await ctx.voice_client.move_to(voice_channel)
ctx.voice_client.stop()
vc = ctx.voice_client
vc.play(source)
但是我试图删除命令方面并让机器人每小时运行一次查找频道命令,该命令遍历公会中的每个频道,并加入它看到的第一个有 2 个或更多人的频道。我似乎无法找到有关如何在不使用 ctx 类的情况下正确列出公会中的多个语音频道的文档。
答案 0 :(得分:0)
你需要做两件事:
要从公会获取频道,您可以使用 bot.guild.voice_channels
。要创建任务,您需要在装饰器 @tasks.loop()
下创建命令,然后使用 start()
启动它。
import discord
from discord.ext import commands, tasks #import tasks as well
@tasks.loop(hours=1)
async def joinvc():
voice_channels = bot.get_guild(guild_id).voice_channels #replace guild_id with your guild's id.
##then here goes the code that you want to loop every 1 hour.
joinvc.start() ##start the task.