【发布时间】: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