Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
curtisf committed Dec 1, 2019
0 parents commit 958e58d
Show file tree
Hide file tree
Showing 11 changed files with 971 additions and 0 deletions.
19 changes: 19 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
module.exports = {
env: {
commonjs: true,
es6: true,
node: true
},
extends: [
'standard'
],
globals: {
Atomics: 'readonly',
SharedArrayBuffer: 'readonly'
},
parserOptions: {
ecmaVersion: 2018
},
rules: {
}
}
64 changes: 64 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*

# Runtime data
pids
*.pid
*.seed
*.pid.lock

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage

# nyc test coverage
.nyc_output

# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# Bower dependency directory (https://bower.io/)
bower_components

# node-waf configuration
.lock-wscript

# Compiled binary addons (https://nodejs.org/api/addons.html)
build/Release

# Dependency directories
node_modules/
jspm_packages/

# Typescript v1 declaration files
typings/

# Optional npm cache directory
.npm

# Optional eslint cache
.eslintcache

# Optional REPL history
.node_repl_history

# Output of 'npm pack'
*.tgz

# Yarn Integrity file
.yarn-integrity

# dotenv environment variables file
.env

# next.js build output
.next

# idea files
.idea
660 changes: 660 additions & 0 deletions LICENSE.md

Large diffs are not rendered by default.

16 changes: 16 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@

(async () => {
require('dotenv').config()
const Client = require('./src/classes/Client')

const client = await Client.build(process.env.AMQP_URI, process.env.AMQP_QUEUE || 'wildbeast')
client.on('ready', () => {
console.log('Client is ready')
})
client.on('guildCreate', (guild) => {
console.log('guild create from init', guild.iconURL)
})
client.on('messageCreate', m => {
console.log('message create', m)
})
})()
31 changes: 31 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"name": "midgard",
"version": "0.0.1",
"description": "Worker library for Heimdall",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "git+https://github.com/thesharks/midgard.git"
},
"author": "Curtis Fowler ([email protected])",
"license": "AGPL-3.0",
"bugs": {
"url": "https://github.com/thesharks/midgard/issues"
},
"homepage": "https://github.com/thesharks/midgard#readme",
"dependencies": {
"amqplib": "^0.5.5",
"dotenv": "^8.2.0"
},
"devDependencies": {
"eslint": "^6.6.0",
"eslint-config-standard": "^14.1.0",
"eslint-plugin-import": "^2.18.2",
"eslint-plugin-node": "^10.0.0",
"eslint-plugin-promise": "^4.2.1",
"eslint-plugin-standard": "^4.0.1"
}
}
44 changes: 44 additions & 0 deletions src/classes/Client.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
const amqp = require('amqplib')
const { EventEmitter } = require('events')
const MessageProcessor = require('./MessageProcessor')

module.exports = class Client extends EventEmitter {
constructor (connection, channel, queue) {
super(MessageProcessor)
this._conn = connection
this._chann = channel
this._queue = queue
this._messageProcessor = new MessageProcessor(this)
this._chann.consume(this._queue, m => {
let msg
try {
msg = JSON.parse(m.content.toString())
} catch (_) {
console.error('Invalid message received from RabbitMQ')
return
}
this._messageProcessor.handle(msg)
this._chann.ack(m)
})
super.emit('ready')
process.on('exit', () => this._conn.close())
}

static async build (uri, queue) {
const connection = await amqp.connect(undefined)
const channel = await connection.createChannel()
channel.assertQueue(queue, {
durable: true
})
return new this(connection, channel, queue)
}

get isAlive () {
return async () => {
this._chann.sendToQueue(this._queue, Buffer.from('hi'), {
persistent: true
})
return true
}
}
}
24 changes: 24 additions & 0 deletions src/classes/MessageProcessor.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
const { EventEmitter } = require('events')
const Guild = require('./types/Guild')
const Message = require('./types/Message')

module.exports = class MessageProcessor extends EventEmitter {
constructor (client) {
super()
this._client = client
}

handle (msg) {
// console.log(msg.t)
switch (msg.t) {
case 'GUILD_CREATE': {
this._client.emit('guildCreate', new Guild(msg.d))
break
}
case 'MESSAGE_CREATE': {
this._client.emit('messageCreate', new Message(msg.d))
break
}
}
}
}
1 change: 1 addition & 0 deletions src/classes/rest/EndpointConstants.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
exports.CDN_URL = 'https://cdn.discordapp.com'
9 changes: 9 additions & 0 deletions src/classes/types/Base.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module.exports = class Base { // Heavily inspired by the Eris Discord bot library
constructor (id) {
this.id = id
}

get createdAt () {
return Math.floor(this.id / 4194304) + 1420070400000
}
}
85 changes: 85 additions & 0 deletions src/classes/types/Guild.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
const { CDN_URL } = require('../rest/EndpointConstants')
const Base = require('./Base')

module.exports = class Guild extends Base {
constructor (packet) {
super(packet.id)
this.update(packet)
}

update (data) {
if (data.name !== undefined) {
this.name = data.name
}
if (data.verification_level !== undefined) {
this.verificationLevel = data.verification_level
}
if (data.splash !== undefined) {
this.splash = data.splash
}
if (data.banner !== undefined) {
this.banner = data.banner
}
if (data.region !== undefined) {
this.region = data.region
}
if (data.owner_id !== undefined) {
this.ownerID = data.owner_id
}
if (data.icon !== undefined) {
this.icon = data.icon
}
if (data.features !== undefined) {
this.features = data.features
}
if (data.emojis !== undefined) {
this.emojis = data.emojis
}
if (data.afk_channel_id !== undefined) {
this.afkChannelID = data.afk_channel_id
}
if (data.afk_timeout !== undefined) {
this.afkTimeout = data.afk_timeout
}
if (data.default_message_notifications !== undefined) {
this.defaultNotifications = data.default_message_notifications
}
if (data.mfa_level !== undefined) {
this.mfaLevel = data.mfa_level
}
if (data.large !== undefined) {
this.large = data.large
}
if (data.max_presences !== undefined) {
this.maxPresences = data.max_presences
}
if (data.explicit_content_filter !== undefined) {
this.explicitContentFilter = data.explicit_content_filter
}
if (data.system_channel_id !== undefined) {
this.systemChannelID = data.system_channel_id
}
if (data.premium_tier !== undefined) {
this.premiumTier = data.premium_tier
}
if (data.premium_subscription_count !== undefined) {
this.premiumSubscriptionCount = data.premium_subscription_count
}
if (data.vanity_url_code !== undefined) {
this.vanityURL = data.vanity_url_code
}
if (data.preferred_locale !== undefined) {
this.preferredLocale = data.preferred_locale
}
if (data.description !== undefined) {
this.description = data.description
}
if (data.max_members !== undefined) {
this.maxMembers = data.max_members
}
}

get iconURL () { // just this one for now.
return this.icon ? `${CDN_URL}/icons/${this.id}/${this.icon}.${this.icon.startsWith('a_') ? 'gif' : 'png'}?size=256` : null
}
}
18 changes: 18 additions & 0 deletions src/classes/types/Message.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
const Base = require('./Base')

module.exports = class Message extends Base {
constructor (packet) {
super(packet.id)
// These definitions are not in the update function
// because they will never be updated
this.type = packet.type || 0
this.timestamp = Date.parse(packet.timestamp)
this.channel = {
id: packet.channel_id
}
this.content = ''
if (packet.author) {
// this.author = new User(packet)
}
}
}

0 comments on commit 958e58d

Please sign in to comment.