当前位置: 首页 > 知识库问答 >
问题:

如何访问用户写入命令不一致的语音通道。皮耶?

谭高峯
2023-03-14

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

这是我的密码:

 #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")

共有2个答案

苏德容
2023-03-14

这应该起作用:

# 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()
季凡
2023-03-14

有一种东西叫做Converters(您可以在这里阅读更多关于它们的信息)。您可以使用特殊的VoiceChannelConzer,它的工作原理是简单地在命令中键入参数

@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()
 类似资料:
  • 我在挑拨离间。v12上的js Bot,包括一个静音命令,用于静音您所在的整个语音频道。问题是当有人离开频道时,他们保持沉默。我正试图用一个简单的事件来解除此人的静音,但我不理解和和。我搜索了很多,但我只能找到一个适合加入风投的,而不是离开风投的。以下是我到目前为止得到的信息: 静音命令: 取消静音事件: 谢谢你花时间帮助我!:)

  • 我正在尝试创建一个机器人,当有人加入特定的语音频道(例如VC1)时,它会向特定的文本频道(例如vc-text)发送消息。 这是机器人。js代码: 一些ID: 文本通道ID是712677731023716452-发送通知到(我知道我需要添加通道消息编码) 当我加入VC1时,我会收到控制台消息“Left VC1”,当我离开/加入另一个VC1时,我也会收到相同的控制台消息。 我从https://www.

  • 如前所述。我正在尝试为我的Discord机器人创建一个临时静音命令。我遇到的问题是,它创建了一个静音的角色,但即使我更改了烫发,用户仍然可以编写消息。除此之外,我还收到了以下弃用警告: (节点:15956)弃用警告:集合#查找:改为传递函数

  • 我正在制作一个音乐机器人,当成员写入“~play”时,机器人会在文件夹中搜索一个随机文件(. mp3),并加入用户当前所在的语音通道。我希望我的机器人在所有用户离开语音通道时离开语音通道。

  • 我正在为我的discord服务器制作一个机器人,但我遇到了问题。我想要的是,当最后一个人离开一个语音频道(它是由机器人生成的)时,我想要删除该语音频道。 我也考虑过测试任何空的语音频道并删除它们,但我不知道如何做。

  • 问题内容: 在线上有许多教程可为Android应用添加语音识别。它们常常令人困惑,并且编码的发布者永远也不会提出问题。我需要一个带有完整编码的简单教程,以向我的应用添加语音识别。 问题答案: 如果要将语音识别添加到小组的Android应用中,这非常简单。 在整个教程中,您需要在粘贴代码时添加导入。 创建一个xml文件或使用现有的xml文件,并确保添加一个按钮和一个listview。 在Java类中