提问者:小点点

在没有用户输入的情况下迭代公会中的不和谐语音通道列表


我目前有:


 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类的情况下正确列出公会中的多个语音频道。


共1个答案

匿名用户

你需要做两件事:

  1. 从公会获取所有VC频道。
  2. 创建一个任务以每小时运行一次命令。

要从公会获取频道,您可以使用bot. guid.voice_channels。要创建任务,您需要在装饰器@task.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.