Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x Discord.py:访问发送邮件的人的计算机上的文件。从服务器上任何用户的本地驱动器播放歌曲_Python 3.x_Ffmpeg_Discord.py - Fatal编程技术网

Python 3.x Discord.py:访问发送邮件的人的计算机上的文件。从服务器上任何用户的本地驱动器播放歌曲

Python 3.x Discord.py:访问发送邮件的人的计算机上的文件。从服务器上任何用户的本地驱动器播放歌曲,python-3.x,ffmpeg,discord.py,Python 3.x,Ffmpeg,Discord.py,我有一个机器人可以从我的电脑播放音乐。有没有办法从发送信息的人的计算机播放歌曲?我的想法是使用message.author以某种方式访问此人的会话并进入他们的驱动器。这是我的机器人。它可以加入语音频道,从本地文件路径创建播放列表,使用停止/暂停/播放/下一个/上一个控件启动播放列表或单个文件: import discord import os.path import logging import asyncio from os import path global ready global

我有一个机器人可以从我的电脑播放音乐。有没有办法从发送信息的人的计算机播放歌曲?我的想法是使用
message.author
以某种方式访问此人的会话并进入他们的驱动器。这是我的机器人。它可以加入语音频道,从本地文件路径创建播放列表,使用停止/暂停/播放/下一个/上一个控件启动播放列表或单个文件:

import discord
import os.path
import logging
import asyncio
from os import path

global ready 
global vc
global source
global songQueue
global songIndex
global commandList
global stopPlaylist

ready = False
stopPlaylist = False
songQueue = []
songIndex = 0

logger = logging.getLogger('discord')
logger.setLevel(logging.DEBUG)
handler = logging.FileHandler(filename='D:\DnD\DiscordBot\discord.log', encoding='utf-8', mode='w')
handler.setFormatter(logging.Formatter('%(asctime)s:%(levelname)s:%(name)s: %(message)s'))
logger.addHandler(handler)

client = discord.Client()
commands = [
    '!connect\nConnect to a voice channel by channel id. Use !channels to find the desired id.\nExample Command: !connect 827202170973323305\n\n',
    '!channels\nLists all voice channels and their connection id.\n\n',
    '!add\nAdd a file path to the playlist.\nExample Command: !add D:\\DnD\\DiscordBot\\mySong.mp3\n\n',
    '!delete\nDeletes the last song added to the playlist.\n\n',
    '!view\nDisplays the current playlist, in order.\n\n',
    '!playlist\nStarts the playlist from the beginning, or optionally add a number as the start position.\nExample Command: !playlist 3\n\n',
    '!playSong\nPlays a specified file.\nExample Command: !playSong D:\\DnD\\DiscordBot\\mySong.mp3\n\n',
    '!next\nPlays next song in playlist.\n\n',
    '!prev\nPlays previous song in playlist.\n\n',
    '!stop\nStops all music song. Playlist will restart from the beginning.\n\n',
    '!pause\nPauses the current song. Restart with !resumeSong.\n\n',
    '!resume\nResumes the current song.\n\n'
    '!status\nLets you know if the bot thinks it is playing music.'
    ]
commandList=''
for command in commands:
    commandList+=command

@client.event
async def on_ready():
    print('We have logged in as {0.user}'.format(client))

@client.event
async def on_message(message):
    global ready 
    global vc
    global source
    global songQueue
    global songIndex
    global commandList
    global stopPlaylist

    if message.author == client.user:
        return
#!help
    if message.content.startswith('!help'):
        await message.channel.send('{0}'.format(commandList))
        return
#!connect
    if message.content.startswith('!connect'):  
        if ready:
            await message.channel.send('Bot [{0}] is already connected to a voice channel.'.format(client.user))
            return
        channel = int(message.content[9:])
        vc = await client.get_channel(channel).connect()
        ready = True
        await message.channel.send('Bot [{0}] is connecting to voice.'.format(client.user))
        return
#!channels
    if message.content.startswith('!channels'):
        channelList = ''
        for channel in client.get_all_channels():
            if channel.type == discord.ChannelType.voice:
                channelList += 'name: ' + channel.name + '\n'
                channelList += 'id: ' + str(channel.id) + '\n\n'
        await message.channel.send('{0}'.format(channelList))
        return
#!add
    if message.content.startswith('!add'):
        song = message.content[5:]
        if not path.exists(song):
            await message.channel.send('Song not found or invalid path specified.\nSpecified Path: {0}\nExample command: !addSong C:\\Users\\Public\\Music\\mySong.mp3'.format(song))
            return
        songQueue.append(song)
        await message.channel.send('Song added: {0}\nCurrent playist length: {1} song(s)'.format(song,len(songQueue)))
        return
#!delete
    if message.content.startswith('!delete'):
        if len(songQueue) == 0:
            await message.channel.send('Playlist is empty. Use !addSong, !viewList, and !playList to manage playlists.')
            return
        await message.channel.send('Removed song: {0}'.format(songQueue.pop()))
        return
#!view
    if message.content.startswith('!view'):
        if len(songQueue) == 0:
            await message.channel.send('Playlist is empty. Use !addSong, !deleteSong, and !playList to manage playlists.')
            return
        await message.channel.send('Current Playlist:\n{0}'.format('\n'.join(songQueue)))
        return
#play commands
    if message.content.startswith('!play'):
        if not ready:
            await message.channel.send('Bot [{0}] is not connected to a voice channel.'.format(client.user))
            return
#!playlist  
        if message.content.startswith('!playlist'):
            try:
                songIndex = int(message.content[10:]) - 1
                if songIndex >= len(songQueue):
                    songIndex = len(songQueue) - 1
            except:
                pass    
            playSong()
            return
#!playSong
        if message.content.startswith('!playSong'):
            song = message.content[10:]
            if not path.exists(song):
                await message.channel.send('Song not found or invalid path specified.\nSpecified Path: {0}\nExample command: !play C:\\Users\\Public\\Music\\mySong.mp3'.format(song))
                return
            source = discord.FFmpegPCMAudio(song)
            vc.play(source, after=None)
            await message.channel.send('Playing song: {0}'.format(song))
            return
#!next
    if message.content.startswith('!next'):
        vc.stop()
#!prev
    if message.content.startswith('!prev'):
        songIndex -= 2
        if songIndex < -1:
            songIndex = -1
        vc.stop()
#!stop
    if message.content.startswith('!stop'):
        if not ready:
            await message.channel.send('Bot [{0}] is not connected to a voice channel.'.format(client.user))
            return
        vc.stop()
        songIndex = 0
        stopPlaylist = True
        await message.channel.send('Stopping music.')
        return
#!pause
    if message.content.startswith('!pause'):
        if not ready:
            await message.channel.send('Bot [{0}] is not connected to a voice channel.'.format(client.user))
            return
        vc.pause()
        await message.channel.send('Pausing music.')
        return
#!resume
    if message.content.startswith('!resume'):
        if not ready:
            await message.channel.send('Bot [{0}] is not connected to a voice channel.'.format(client.user))
            return
        vc.resume()
        await message.channel.send('Resuming music.')
        return
#!status
    if message.content.startswith('!status'):
        if not ready:
            await message.channel.send('Bot [{0}] is not connected to a voice channel.'.format(client.user))
            return
        if vc.is_playing():
            await message.channel.send('Something is playing.')
            return
        await message.channel.send('Nothing is playing.')
        return

def playSong():
    global songQueue
    global songIndex
    global vc
    try:
        song = songQueue[songIndex]
        source = discord.FFmpegPCMAudio(song)
        vc.play(source, after=nextSong)
    except Exception as e:
        print('playSong error {0}'.format(e))

def nextSong(error):
    global songQueue
    global songIndex
    global stopPlaylist
    try:
        songIndex += 1
        if songIndex >= len(songQueue):
            stopPlaylist = True
        if stopPlaylist:
            songIndex = 0
            stopPlaylist = False
            return
        futureFunction = asyncio.run_coroutine_threadsafe(playSong(), client.loop)
        futureFunction.result()
    except Exception as e:
        print('nextSong error {0}'.format(e))

#@client.event
#async def on_logout(user)
#   global ready
#   if user == client.user:
#       ready = False

client.run('TOKEN')
导入不一致
导入操作系统路径
导入日志记录
导入异步
从操作系统导入路径
全球就绪
全球风险投资
全球来源
全局歌曲队列
全球歌曲索引
全局命令列表
全球停止播放列表
就绪=错误
stopPlaylist=False
歌曲队列=[]
歌曲索引=0
logger=logging.getLogger('discord')
logger.setLevel(logging.DEBUG)
handler=logging.FileHandler(filename='D:\DnD\DiscordBot\discord.log',encoding='utf-8',mode='w')
handler.setFormatter(logging.Formatter('%(asctime)s:%(levelname)s:%(name)s:%(message)s'))
logger.addHandler(处理程序)
client=discord.client()
命令=[
“!connect\n按频道id连接到语音频道。使用!频道查找所需的id。\n例如命令:!connect 82720217097323305\n\n”,
“!channels\n列出所有语音频道及其连接id。\n\n”,
“!add\n向播放列表添加文件路径。\n例如命令:!add D:\\DnD\\DiscordBot\\mySong.mp3\n\n”,
“!delete\n删除添加到播放列表中的最后一首歌曲。\n\n”,
“!view\n按顺序显示当前播放列表。\n\n”,
“!playlist\n从头开始播放列表,或者可以选择添加一个数字作为开始位置。\n例如命令:!playlist 3\n\n”,
“!playSong\n显示指定的文件。\n例如命令:!playSong D:\\DnD\\DiscordBot\\mySong.mp3\n\n”,
“!next\n在播放列表中播放下一首歌曲。\n\n”,
“!prev\n在播放列表中显示上一首歌曲。\n\n”,
“!停止\n停止所有音乐歌曲。播放列表将从头开始。\n\n”,
“!pause\n使用当前歌曲。用!resumeSong重新启动。\n\n”,
“!resume\n恢复当前歌曲。\n\n”
“!status\n让您知道机器人是否认为自己在播放音乐。”
]
命令列表=“”
对于命令中的命令:
commandList+=命令
@客户端事件
_ready()上的异步定义:
打印('我们已以{0.user}的身份登录。格式(客户端))
@客户端事件
异步def on_消息(消息):
全球就绪
全球风险投资
全球来源
全局歌曲队列
全球歌曲索引
全局命令列表
全球停止播放列表
如果message.author==client.user:
回来
#!帮助
如果message.content.startswith(“!help”):
wait message.channel.send({0}.format(commandList))
回来
#!连接
如果message.content.startswith(“!connect”):
如果准备好了:
wait message.channel.send('Bot[{0}]已连接到语音频道。'.format(client.user))
回来
channel=int(message.content[9:]
vc=等待客户端。获取_通道(通道).connect()
就绪=真
wait message.channel.send('Bot[{0}]正在连接到语音。'.format(client.user))
回来
#!渠道
如果message.content.startswith(“!channels”):
频道列表=“”
对于客户端中的通道。获取所有通道()
如果channel.type==discord.ChannelType.voice:
channelList+='名称:'+channel.name+'\n'
channelList+='id:'+str(channel.id)+'\n\n'
wait message.channel.send({0}.format(channelList))
回来
#!添加
如果message.content.startswith(“!add”):
宋=消息。内容[5:]
如果路径不存在(歌曲):
wait message.channel.send('找不到歌曲或指定的路径无效。\n指定的路径:{0}\n示例命令:!addSong C:\\Users\\Public\\Music\\mySong.mp3'。格式(歌曲))
回来
songQueue.append(歌曲)
wait message.channel.send('添加的歌曲:{0}\n当前播放者长度:{1}首歌曲'。格式(歌曲,长度(歌曲队列)))
回来
#!删去
如果message.content.startswith(“!delete”):
如果len(songQueue)==0:
wait message.channel.send('播放列表为空。使用!addSong、!viewList和!Playlist管理播放列表')
回来
wait message.channel.send('Removed song:{0}'。格式(songQueue.pop())
回来
#!看法
如果message.content.startswith(“!view”):
如果len(songQueue)==0:
wait message.channel.send('播放列表为空。请使用!addSong、!deleteSong和!Playlist来管理播放列表')
回来
wait message.channel.send('当前播放列表:\n{0}'。格式('\n'.join(songQueue)))
回来
#播放命令
如果message.content.startswith(“!play”):
如果未准备好:
wait message.channel.send('Bot[{0}]未连接到语音频道。'.format(client.user))
回来
#!播放列表
如果message.content.startswith(“!playlist”):
尝试:
songIndex=int(message.content[10:])-1
如果songIndex>=len(歌曲队列):
songIndex=len(歌曲队列)-1
除:
通过
playSong()
回来
#!戏曲
如果message.content.startswith(“!playSong”):
歌曲=消息。内容[10:]
如果路径不存在(歌曲):
wait message.channel.send('找不到歌曲或指定的路径无效。\n指定的路径:{0}\n示例命令:!play C:\\Users\\Public\\Music\\mySong.mp3'。格式(歌曲))
回来
source=discord.ffmpegpcaudio(歌曲)
vc.play(source,after=None)
wait message.channel.send('播放歌曲:{0}'。格式(歌曲))
回来
#!下一个
如果message.content.startswith(“!next”):
停止
#!上
如果message.content.startswith(“!prev”):
指数-=2
如果歌曲索引<-1:
歌曲索引=-1
停止
#!停止
如果m