【发布时间】:2019-12-03 17:43:34
【问题描述】:
我正在尝试在 Nodejs 中制作一个简单的乒乓不和谐机器人,而没有像 Nodejs 中的 discord.js 那样的任何库
我认为使用 websockets 和不和谐的其余 api 是最好的解决方案,但我找不到任何帮助或任何代码示例来做到这一点
我找到了这门课程:https://courses.cs.washington.edu/courses/cse154/17au/exploration/websockets/slides.html#/ 有了这个我无法理解的“解决方案”:https://courses.cs.washington.edu/courses/cse154/17au/exploration/websockets/solution/
这是课程的摘录:
const BOT_TOKEN = "xxx";
// Discord Gateway url
const GATEWAY_URL = "wss://gateway.discord.gg/?v=6&encoding=json";
// Websocket object
let ws = null;
connect();
// connect to gateway
function connect() {
ws = new WebSocket(GATEWAY_URL); // opens the websocket connection and creates WS object
ws.onmessage = messageHandler; // on message event
ws.onclose = connect; // reopen websockets when closed by discord
}
我想做的事可以用 discord.js 用这段代码简单地完成:
const Discord = require('discord.js');
const bot = new Discord.Client();
bot.on('ready', function () {
console.log("Connected !")
})
bot.on('message', message => {
if (message.content === 'ping') {
message.reply('pong !')
}
})
bot.login(token)
有什么帮助吗?
【问题讨论】: