【问题标题】:How to access which voice channel user writing command is in discord.py?如何访问discord.py中的哪个语音通道用户写入命令?
【发布时间】:2021-05-10 19:37:22
【问题描述】:

我开始用 discord.py 制作一个不和谐的音乐机器人。我已经学会了如何在用户编写命令时将机器人连接到语音通道:dj/play。但是,我希望机器人加入编写命令的用户所在的语音频道。有没有办法做到这一点?

这是我的代码:

 #imports
import discord
from discord.ext import commands
from random import randint
import aiohttp
from youtube_dl import YoutubeDL
import os

# dotenv.load_dotenv()

#setup
audio_downloder = YoutubeDL({'format':'bestaudio'})
client = commands.Bot(command_prefix=commands.when_mentioned_or("dj/"))

#commands
class MyCog(commands.Cog):
    def __init__(self, bot):
        self.bot = bot

    @commands.command()
    async def flip(self, ctx):
        num = randint(0,2)
        if num == 0:
            await ctx.send("It's heads")
        elif num == 1:
            await ctx.send("No")
        else:
            await ctx.send("It's tails")
    
    @commands.command()
    async def play(self, ctx, *args):
        voice_channel = discord.utils.get(ctx.guild.voice_channels, name="Dev Train")
        voice = discord.utils.get(client.voice_clients, guild=ctx.guild)
        await voice_channel.connect()


async def on_message(message):
    if message.author == client.user:
        return

    print(f"{message.guild} - {message.channel} - {message.author} <{message.author.id}>: {message.content}")



client.add_listener(on_message)

client.add_cog(MyCog(client))
client.run("TOKEN")

【问题讨论】:

  • 感谢从我的帖子中删除令牌的人!我是新手,完全忘记了你不应该在网上发帖
  • 不用担心,我建议您重新生成令牌,因为您仍然可以在 revisions 选项卡中看到令牌

标签: python async-await discord discord.py


【解决方案1】:

有一个东西叫做Converters(你可以阅读更多关于它们的信息here)。您可以使用特殊的VoiceChannelConverter,它只需在命令中键入参数即可工作

@commands.command()
async def play(self, ctx, voice_channel: discord.VoiceChannel):
    print(type(voice_channel)) # -> discord.channel.VoiceChannel
    await voice_channel.connect()

调用

{prefix}play #channel        | channel mention
{prefix}play 716389123897123 | channel ID
{prefix}play general         | channel name

获取当前用户语音频道

@command.command()
async def play(self, ctx):
    if ctx.author.voice is None:
        return await ctx.send("You are not in a voice channel")

    voice_channel = ctx.author.voice.channel
    await voice_channel.connect()

【讨论】:

  • 好的,如果有帮助,请务必接受答案
【解决方案2】:

这应该可行:

# join command
@commands.command(description="joins a voice channel")
    async def join(self, ctx):
        if ctx.author.voice is None or ctx.author.voice.channel is None:
            return await ctx.send('You need to be in a voice channel to use this command!')

        voice_channel = ctx.author.voice.channel
        if ctx.voice_client is None:
            vc = await voice_channel.connect()
        else:
            await ctx.voice_client.move_to(voice_channel)
            vc = ctx.voice_client

# leave command
@commands.command(description="stops and disconnects the bot from voice")
    async def leave(self, ctx):
        await ctx.voice_client.disconnect()

【讨论】:

  • 谢谢,我会试一试
  • 太棒了!如果对你有用,请接受我的回答!
  • 断开机器人的命令是什么?
  • 我已经编辑了答案并添加了离开命令。
猜你喜欢
  • 2021-04-15
  • 2020-06-15
  • 1970-01-01
  • 2021-11-13
  • 2021-08-26
  • 2018-04-04
  • 2021-03-13
  • 2021-01-31
  • 2020-12-06
相关资源
最近更新 更多