亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Python discord bot 離開語音頻道

Python discord bot 離開語音頻道

當年話下 2022-10-06 15:51:31
我已經讓機器人加入我的語音服務器,如下所示 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 回答

?
收到一只叮咚

TA貢獻1821條經驗 獲得超5個贊

您可以通過兩種方式執行這兩個命令(加入和離開頻道命令),一種是使用 on_message,另一種是使用@bot.commands。最好使用 bot.command 而不是 on_message 作為命令,因為 bot.commands 具有更多功能,而且我認為它是為命令而構建的,它的速度很快。因此,我將使用 bot.command 重寫您的兩個命令,并在此處使用 on_message 以防您不想使用 bot.command。根據您的消息,我假設?是您的前綴


使用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")




查看完整回答
反對 回復 2022-10-06
?
守候你守候我

TA貢獻1802條經驗 獲得超10個贊

保存通道連接,以便稍后斷開連接。


voice = None

...

    if message.content.startswith("?join"):

        channel = message.author.voice.channel

        global voice = await channel.connect()

        await message.channel.send('bot joined')

    elif message.content.startswith("?join"):

        await self.voice.disconnect()

無論如何,嘗試使用discord.ext.commands擴展。它使所有涉及命令的事情變得更容易。我還建議使用 cogs ( example ),因為您可以擁有一個包含所有與語音相關的類,并且您不需要全局變量。


查看完整回答
反對 回復 2022-10-06
  • 2 回答
  • 0 關注
  • 124 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號