Python不和谐机器人离开语音通道

时间:2020-04-11 04:45:09

标签: python discord.py

我已经像下面这样使机器人加入了我的语音服务器

 if message.content.startswith("?join"):
    channel = message.author.voice.channel
    await channel.connect()
    await message.channel.send('bot joined')

但是我不能让机器人离开频道。我该如何编码才能做到这一点? 和之间有什么区别

@bot.event
 async def on_message(message):
 if message.content.startswith('~'):

@bot.command()
async def ~(ctx):

2 个答案:

答案 0 :(得分:1)

您可以通过两种方式执行这两个命令(join和leave通道命令),一种是使用on_message,另一种是使用@ bot.commands。最好使用bot.command代替on_message作为命令,因为bot.commands具有更多功能,而且我认为毕竟是为命令构建的,它很快。因此,如果您不想使用bot.command,我将使用bot.command重写您的两个命令,并在其中使用on_message。 根据您的消息,我假设?是您的前缀

使用on_message

@bot.event
async def on_message(message):
    if (message.content.startswith('?join')):
        if (message.author.voice): # If the person is in a channel
            channel = message.author.voice.channel
            await channel.connect()
            await message.channel.send('Bot joined')
        else: #But is (s)he isn't in a voice channel
            await message.channel.send("You must be in a voice channel first so I can join it.")

    elif message.content.startswith('?~'): # Saying ?~ will make bot leave channel
        if (message.guild.voice_client): # If the bot is in a voice channel 
            await message.guild.voice_client.disconnect() # Leave the channel
            await message.channel.send('Bot left')
        else: # But if it isn't
            await message.channel.send("I'm not in a voice channel, use the join command to make me join")
    await bot.process_commands(message) # Always put this at the bottom of on_message to make commands work properly

使用bot.command

@bot.command()
async def join(ctx):
    if (ctx.author.voice): # If the person is in a channel
        channel = ctx.author.voice.channel
        await channel.connect()
        await ctx.send('Bot joined')
    else: #But is (s)he isn't in a voice channel
        await ctx.send("You must be in a voice channel first so I can join it.")

@bot.command(name = ["~"])
async def leave(ctx): # Note: ?leave won't work, only ?~ will work unless you change  `name = ["~"]` to `aliases = ["~"]` so both can work.
    if (ctx.voice_client): # If the bot is in a voice channel 
        await ctx.guild.voice_client.disconnect() # Leave the channel
        await ctx.send('Bot left')
    else: # But if it isn't
        await ctx.send("I'm not in a voice channel, use the join command to make me join")

答案 1 :(得分:1)

保存频道连接,以便以后断开连接。

discord.ext.commands

无论如何,请尝试使用let mainNavLinks = document.querySelectorAll(".glossaryContainer ul li a"); let mainSections = document.querySelectorAll("main section"); window.addEventListener("scroll", event => { let fromTop = window.scrollY; mainNavLinks.forEach(link => { let section = document.querySelector(link.hash); if (section.offsetTop <= fromTop && section.offsetTop + section.offsetHeight > fromTop) { link.classList.add("current"); } else { link.classList.remove("current"); } }); });扩展名。它使涉及命令的所有事情变得更加容易。我还建议您使用齿轮(example),因为您可以拥有一个与语音相关的所有类,并且不需要全局变量。