質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Node.js

Node.jsとはGoogleのV8 JavaScriptエンジンを使用しているサーバーサイドのイベント駆動型プログラムです。

Q&A

1回答

1586閲覧

Node.js 複数ファイルの場合どうするのか

GLAYER

総合スコア0

Node.js

Node.jsとはGoogleのV8 JavaScriptエンジンを使用しているサーバーサイドのイベント駆動型プログラムです。

0グッド

0クリップ

投稿2021/12/27 11:11

前提・実現したいこと

play.jsのコマンドを使えるようにしたいです。

発生している問題・エラーメッセージ

なし

###ソースコード
index.js

const discord = require('discord.js'); const client = new discord.Client() const prefix = '!' const fs = require("fs"); client.on('ready', () => { console.log(`起動`); }); client.login(process.env.TOKEN);

play.js

const ytdl = require('ytdl-core'); const ytSearch = require('yt-search'); const fs = require("fs"); //Global queue for your bot. Every server will have a key and value pair in this map. { guild.id, queue_constructor{} } const queue = new Map(); module.exports = { name: 'play', aliases: ['skip', 'stop'], //We are using aliases to run the skip and stop command follow this tutorial if lost: https://www.youtube.com/watch?v=QBUJ3cdofqc cooldown: 0, description: 'Advanced music bot', async execute(message,args, cmd, client, Discord){ //Checking for the voicechannel and permissions (you can add more permissions if you like). const voice_channel = message.member.voice.channel; if (!voice_channel) return message.channel.send('You need to be in a channel to execute this command!'); const permissions = voice_channel.permissionsFor(message.client.user); if (!permissions.has('CONNECT')) return message.channel.send('You dont have the correct permissins'); if (!permissions.has('SPEAK')) return message.channel.send('You dont have the correct permissins'); //This is our server queue. We are getting this server queue from the global queue. const server_queue = queue.get(message.guild.id); //If the user has used the play command if (cmd === 'play'){ if (!args.length) return message.channel.send('You need to send the second argument!'); let song = {}; //If the first argument is a link. Set the song object to have two keys. Title and URl. if (ytdl.validateURL(args[0])) { const song_info = await ytdl.getInfo(args[0]); song = { title: song_info.videoDetails.title, url: song_info.videoDetails.video_url } } else { //If there was no link, we use keywords to search for a video. Set the song object to have two keys. Title and URl. const video_finder = async (query) =>{ const video_result = await ytSearch(query); return (video_result.videos.length > 1) ? video_result.videos[0] : null; } const video = await video_finder(args.join(' ')); if (video){ song = { title: video.title, url: video.url } } else { message.channel.send('Error finding video.'); } } //If the server queue does not exist (which doesn't for the first video queued) then create a constructor to be added to our global queue. if (!server_queue){ const queue_constructor = { voice_channel: voice_channel, text_channel: message.channel, connection: null, songs: [] } //Add our key and value pair into the global queue. We then use this to get our server queue. queue.set(message.guild.id, queue_constructor); queue_constructor.songs.push(song); //Establish a connection and play the song with the vide_player function. try { const connection = await voice_channel.join(); queue_constructor.connection = connection; video_player(message.guild, queue_constructor.songs[0]); } catch (err) { queue.delete(message.guild.id); message.channel.send('There was an error connecting!'); throw err; } } else{ server_queue.songs.push(song); return message.channel.send(`???? *${song.title}* added to queue!`); } } else if(cmd === 'skip') skip_song(message, server_queue); else if(cmd === 'stop') stop_song(message, server_queue); } } const video_player = async (guild, song) => { const song_queue = queue.get(guild.id); //If no song is left in the server queue. Leave the voice channel and delete the key and value pair from the global queue. if (!song) { song_queue.voice_channel.leave(); queue.delete(guild.id); return; } const stream = ytdl(song.url, { filter: 'audioonly' }); song_queue.connection.play(stream, { seek: 0, volume: 0.5 }) .on('finish', () => { song_queue.songs.shift(); video_player(guild, song_queue.songs[0]); }); await song_queue.text_channel.send(`???? Now playing **${song.title}**`) } const skip_song = (message, server_queue) => { if (!message.member.voice.channel) return message.channel.send('You need to be in a channel to execute this command!'); if(!server_queue){ return message.channel.send(`There are no songs in queue ????`); } server_queue.connection.dispatcher.end(); } const stop_song = (message, server_queue) => { if (!message.member.voice.channel) return message.channel.send('You need to be in a channel to execute this command!'); server_queue.songs = []; server_queue.connection.dispatcher.end(); }

試したこと

const fs = require("fs");を使ってみました。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

モジュール、JSON、ローカルファイルのインポートに使用します。モジュールは node_modules からインポートできます。ローカルモジュールとJSONファイルは、相対パス(例:./、./foo、./bar/baz、.../foo)を使ってインポートすることができます。POSIXスタイルの相対パスはOSに依存しない方法で解決されます。つまり、上記の例はWindows上でもUnixシステム上と同じように動きます。

js

1// Importing a local module with a path relative to the `__dirname` or current 2// working directory. (On Windows, this would resolve to .\path\myLocalModule.) 3const myLocalModule = require('./path/myLocalModule'); 4 5// Importing a JSON file: 6const jsonData = require('./path/filename.json'); 7 8// Importing a module from node_modules or Node.js built-in module: 9const crypto = require('crypto');

https://nodejs.org/api/modules.html#requireid

引用が参考になりますでしょうか。

投稿2021/12/27 12:51

退会済みユーザー

退会済みユーザー

総合スコア0

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだベストアンサーが選ばれていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問