創作內容

9 GP

用discord機器人分享自己電腦的音樂

作者:あかつき│2020-06-06 05:03:09│巴幣:18│人氣:1026

play.js

const Discord = require('discord.js');
const client = new Discord.Client();
const config = require("./config.json");
const prefix = config.prefix;
const readline = require('readline');
var queue = new Map();

const rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout
});

//login message

client.on('ready', () => {
    console.log(`Logged in as ${client.user.tag}!`);
    client.user.setActivity(`${prefix}help \|`, { type: 'LISTENING' });
    client.user.setStatus('online');
});

client.on('message', message => {
    if (message.content.indexOf(prefix) !== 0) return;
    if (message.author.id != config.userid) return;
    const args = message.content.slice(prefix.length).trim().split(/ +/g);
    const command = args.shift().toLowerCase();
    const serverQueue = queue.get(message.guild.id);
    if (command === 'play') { play(message, serverQueue) };
    if (command === 'leave') {
        playSong(message.guild)
    };
})

async function play(message, serverQueue) {
    const args = message.content.split(" ");
    const voiceChannel = message.member.voice.channel;
    if (!voiceChannel) return message.reply("You must be in a voice channel!");
    const song = {
        path: args[1],
    };
    console.log('輸入路徑:' + song)
    if (!serverQueue) {
        const queueConstruct = {
            textChannel: message.channel,
            voiceChannel: voiceChannel,
            connection: null,
            songs: [],
            volume: config.volume,
            playing: true,
        };
        queue.set(message.guild.id, queueConstruct);

        queueConstruct.songs.push(song);

        try {
            var connection = await voiceChannel.join();
            queueConstruct.connection = connection;
            playSong(message.guild, queueConstruct.songs[0]);
        } catch (err) {
            console.log(err);
            queue.delete(message.guild.id)
            return message.channel.send("There was an error playing! " + err);
        }
    } else {
        serverQueue.songs.push(song);
        return console.log(`Has been added to the queue!`);
    }
}

function playSong(guild, song) {
    const serverQueue = queue.get(guild.id);

    if (!song) {
        serverQueue.voiceChannel.leave();
        queue.delete(guild.id);
        return;
    }

    const dispatcher = serverQueue.connection.play(song.path)
        .on('speaking', status => {
            if (!status) {
                serverQueue.songs.shift();
                playSong(guild, serverQueue.songs[0]);
            }
        })
        .on('error', error => {
            console.log(error);
            serverQueue.songs.shift();
            playSong(guild, serverQueue.songs[0]);
        })
    dispatcher.setVolumeLogarithmic(serverQueue.volume / 100);
}
//login

client.login(config.token);


config.json
prefix:指令前綴
token:bot token
volume:播放音量(最好低於40)
userid:使用者的id

{
    "prefix":"",
    "token":"",
    "volume":40,
    "userid":""
}

package.json

{
  "name": "musicbot",
  "version": "1.0.0",
  "description": "",
  "main": "play.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "discord.js": "^12.2.0",
    "node-opus": "^0.3.3",
    "opusscript": "0.0.7"
  }
}




使用前記得安裝FFmpeg
使用方法可以參考之前的文章
可以參考此文章 https://www.wikihow.com/Install-FFmpeg-on-Windows
使用時進入語音頻道後使用以下指令
(前綴)play [相對路徑\絕對路徑] :路徑與檔名不能有\跟空白
(前綴)leave :清空播放清單並離開

因為朋友需要隨手做的,有需要的自行取用==
引用網址:https://home.gamer.com.tw/TrackBack.php?sn=4807127
All rights reserved. 版權所有,保留一切權利

相關創作

留言共 2 篇留言

擅長頭痛的矽藻朋友
這是像立體聲混音那樣的效果嗎

06-06 09:23

あかつき
不是06-06 10:33
擅長頭痛的矽藻朋友
喔好 我看懂ㄌ 我是87

06-06 11:46

我要留言提醒:您尚未登入,請先登入再留言

9喜歡★mike9411 可決定是否刪除您的留言,請勿發表違反站規文字。

前一篇:讓你用Discord機器... 後一篇:學習...

追蹤私訊切換新版閱覽

作品資料夾

hyzgdivina喜歡虹咲的LLer
我的小屋裡有很多又香又甜的Hoenn繪師虹咲漫畫翻譯喔!歡迎LoveLiver來我的小屋裡坐坐~看更多我要大聲說昨天23:17


face基於日前微軟官方表示 Internet Explorer 不再支援新的網路標準,可能無法使用新的應用程式來呈現網站內容,在瀏覽器支援度及網站安全性的雙重考量下,為了讓巴友們有更好的使用體驗,巴哈姆特即將於 2019年9月2日 停止支援 Internet Explorer 瀏覽器的頁面呈現和功能。
屆時建議您使用下述瀏覽器來瀏覽巴哈姆特:
。Google Chrome(推薦)
。Mozilla Firefox
。Microsoft Edge(Windows10以上的作業系統版本才可使用)

face我們了解您不想看到廣告的心情⋯ 若您願意支持巴哈姆特永續經營,請將 gamer.com.tw 加入廣告阻擋工具的白名單中,謝謝 !【教學】