我是Python的初学者,最近我开始为我和一些朋友制作一个不和谐的bot。这个想法是输入!startq并让该bot加入频道,播放本地存储在mp3文件中的mp3文件。 bot.py所在的文件夹。
import discord, chalk
from discord.ext import commands
import time
import asyncio
bot = commands.Bot(command_prefix = "!")
@bot.event
async def on_ready():
print("Bot is ready!")
@bot.command()
async def q5(ctx):
await ctx.send("@here QUEUE STARTING IN 5 MINUTES")
@bot.command()
async def q3(ctx):
await ctx.send("@here QUEUE STARTING IN 3 MINUTES")
@bot.command()
async def q1(ctx):
await ctx.send("@here QUEUE STARTING IN 1 MINUTES")
@bot.command()
async def ping(ctx):
ping_ = bot.latency
ping = round(ping_ * 1000)
await ctx.send(f"my ping is {ping}ms")
@bot.command()
async def startq(ctx):
voicechannel = discord.utils.get(ctx.guild.channels, name='queue')
vc = await voicechannel.connect()
vc.play(discord.FFmpegPCMAudio("countdown.mp3"), after=lambda e: print('done', e))
bot.run('TOKEN')
到目前为止,我的漫游器可以很好地加入频道,但实际上并没有播放mp3。我曾在“非官方Discord API Discord”和其他一些编程Discords中问过无数人,但我还没有得到答案。
答案 0 :(得分:2)
我对我的discord机器人做了类似的事情,这是您可以参考的一些示例代码。如果您要播放mp3文件,请确保安装ffmpeg,设置机器人https://github.com/adaptlearning/adapt_authoring/wiki/Installing-FFmpeg
时,请按照此处的说明进行操作。@client.command(
name='vuvuzela',
description='Plays an awful vuvuzela in the voice channel',
pass_context=True,
)
async def vuvuzela(context):
# grab the user who sent the command
user=context.message.author
voice_channel=user.voice.voice_channel
channel=None
# only play music if user is in a voice channel
if voice_channel!= None:
# grab user's voice channel
channel=voice_channel.name
await client.say('User is in channel: '+ channel)
# create StreamPlayer
vc= await client.join_voice_channel(voice_channel)
player = vc.create_ffmpeg_player('vuvuzela.mp3', after=lambda: print('done'))
player.start()
while not player.is_done():
await asyncio.sleep(1)
# disconnect after the player has finished
player.stop()
await vc.disconnect()
else:
await client.say('User is not in a channel.')
答案 1 :(得分:0)
这是我将用于bot播放mp3文件的重写版本的处理方式。您还需要加载作品,这很容易并且具有FFMPEG。
OPUS_LIBS = ['libopus-0.x86.dll', 'libopus-0.x64.dll', 'libopus-0.dll', 'libopus.so.0', 'libopus.0.dylib']
def load_opus_lib(opus_libs=OPUS_LIBS):
if opus.is_loaded():
return True
for opus_lib in opus_libs:
try:
opus.load_opus(opus_lib)
return
except OSError:
pass
raise RuntimeError('Could not load an opus lib. Tried %s' % (', '.join(opus_libs)))
@bot.command(aliases=['paly', 'queue', 'que'])
async def play(ctx):
guild = ctx.guild
voice_client: discord.VoiceClient = discord.utils.get(bot.voice_clients, guild=guild)
audio_source = discord.FFmpegPCMAudio('vuvuzela.mp3')
if not voice_client.is_playing():
voice_client.play(audio_source, after=None)
答案 2 :(得分:0)
我会做这样的事情:
voice.play(discord.FFmpegPCMAudio(executable="C:/path/ffmpeg.exe", source="C:/songpath"))