-
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from brh55/discord-js
botkit-discord v0.1.0
- Loading branch information
Showing
17 changed files
with
1,897 additions
and
381 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,32 @@ | ||
// May just copy over client methods | ||
module.exports = (client) => ({ | ||
setPresence: client.setPresence, | ||
editUserInfo: client.editUserInfo, | ||
getAllUsers: client.getAllUsers, | ||
fixMessage: client.fixMessage, | ||
simulateTyping: client.simulateTyping, | ||
getMessage: client.getMessage, | ||
getMessages: client.getMessages, | ||
editMessage: client.editMessage, | ||
deleteMessage: client.deleteMessage, | ||
pinMessage: client.pinMessage, | ||
deletePinnedMessage: client.deletePinnedMessage | ||
}); | ||
const joinVoiceChannel = (message) => async () => { | ||
if (!message.member.voiceChannel) | ||
throw Error('User is not in a voice channel'); | ||
|
||
try { | ||
const connection = await message.member.voiceChannel.join(); | ||
return connection; | ||
} catch (error) { | ||
throw Error(error); | ||
} | ||
}; | ||
|
||
const leaveVoiceChannel = (message) => () => { | ||
const voiceChannel = message.member.voiceChannel; | ||
if (voiceChannel) { | ||
voiceChannel.leave(); | ||
} | ||
}; | ||
|
||
module.exports = (bot, message) => { | ||
// Direct message related APIs | ||
if (message.type === 'direct_message') { | ||
return { | ||
} | ||
} | ||
|
||
// Guild message types API (mentions, ambient) | ||
return { | ||
joinVoiceChannel: joinVoiceChannel(message), | ||
leaveVoiceChannel: leaveVoiceChannel(message) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
module.exports = [ | ||
"channelCreate", | ||
"channelDelete", | ||
"channelPinsUpdate", | ||
"channelUpdate", | ||
"clientUserGuildSettingsUpdate", | ||
"clientUserSettingsUpdate", | ||
// "debug", | ||
"disconnect", | ||
"emojiCreate", | ||
"emojiDelete", | ||
"emojiUpdate", | ||
"error", | ||
"guildBanAdd", | ||
"guildBanRemove", | ||
"guildCreate", | ||
"guildDelete", | ||
"guildMemberAdd", | ||
"guildMemberAvailable", | ||
"guildMemberRemove", | ||
"guildMembersChunk", | ||
"guildMemberSpeaking", | ||
"guildMemberUpdate", | ||
"guildUnavailable", | ||
"guildUpdate", | ||
// "message", | ||
"messageDelete", | ||
"messageDeleteBulk", | ||
"messageReactionAdd", | ||
"messageReactionRemove", | ||
"messageReactionRemoveAll", | ||
"messageUpdate", | ||
"presenceUpdate", | ||
"rateLimit", | ||
// "ready", | ||
"reconnecting", | ||
"resume", | ||
"roleCreate", | ||
"roleDelete", | ||
"roleUpdate", | ||
"typingStart", | ||
"typingStop", | ||
"userNoteUpdate", | ||
"userUpdate", | ||
"voiceStateUpdate", | ||
"warn" | ||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,84 +1,66 @@ | ||
const Botkit = require('botkit'); | ||
const Discord = require('discord.io'); | ||
|
||
const Discord = require('discord.js'); | ||
const discordEvents = require('./discord-events'); | ||
const middleware = require('./middleware'); | ||
const botDefinition = require('./bot'); | ||
|
||
const newMessageHandler = (req, res, controller) => { | ||
const payload = req.body; | ||
const newMessageHandler = (message, controller) => { | ||
const bot = controller.spawn({}); | ||
controller.ingest(bot, payload, res); | ||
} | ||
const source = { | ||
raw: message | ||
} | ||
|
||
controller.ingest(bot, {}, source); | ||
}; | ||
|
||
const DiscordBot = (configuration) => { | ||
const client = new Discord.Client({ | ||
token: configuration.token, | ||
autorun: true | ||
}); | ||
const client = new Discord.Client({}); | ||
configuration.client = client; | ||
|
||
const discordBotkit = Botkit.core(configuration || {}); | ||
discordBotkit.defineBot(botDefinition); | ||
discordBotkit.api = require('./api')(client); | ||
|
||
// Pass along classes | ||
discordBotkit.RichEmbed = Discord.RichEmbed; | ||
discordBotkit.Attachment = Discord.Attachment; | ||
|
||
// Attach Handlers and Middlewares | ||
discordBotkit.handleMessageRecieve = newMessageHandler; | ||
discordBotkit.middleware.ingest.use(middleware.ingest.handler); | ||
discordBotkit.middleware.normalize.use(middleware.normalize.handler); | ||
discordBotkit.middleware.categorize.use(middleware.categorize.handler); | ||
discordBotkit.middleware.format.use(middleware.format.handler); | ||
discordBotkit.middleware.receive.use(middleware.receive.handler); | ||
|
||
// discord.io forwarding and event handling | ||
// may move this elsewhere | ||
client.on('ready', event => { | ||
// Add some additional data to make it easier to work with | ||
const readyEvent = Object.assign({}, event, { | ||
username: client.username, | ||
id: client.id | ||
}); | ||
discordBotkit.trigger('ready', [discordBotkit, readyEvent]) | ||
discordBotkit.log('Logged in as %s - %s\n', client.username, client.id); | ||
client.on('ready', () => { | ||
discordBotkit.trigger('ready', [discordBotkit, client.user]) | ||
discordBotkit.log('Logged in as %s - %s\n', client.user.username, client.user.id); | ||
}); | ||
|
||
client.on('message', (user, userID, channelID, message, event) => { | ||
const req = { | ||
body: { | ||
user, userID, channelID, message, event | ||
} | ||
}; | ||
|
||
discordBotkit.handleMessageRecieve(req, {}, discordBotkit); | ||
}); | ||
|
||
client.on('disconnect', (errMsg, code) => { | ||
const event = { | ||
message: errMsg, | ||
code | ||
}; | ||
discordBotkit.trigger('disconnect', [discordBotkit, event]); | ||
client.on('message', async message => { | ||
discordBotkit.debug(`Received ${message}`); | ||
discordBotkit.handleMessageRecieve(message, discordBotkit); | ||
}); | ||
|
||
client.on('presence', (user, userID, status, game, event) => { | ||
const presenceEvent = event.d; | ||
discordBotkit.trigger('presence', [discordBotkit, event]); | ||
// Set up triggers for remaining events | ||
discordEvents.map(event => { | ||
client.on(event, (...params) => { | ||
discordBotkit.trigger(event, [discordBotkit, params]); | ||
}); | ||
}); | ||
|
||
client.on('guildMemberAdd', member => discordBotkit.trigger('guild_member_add', [discordBotkit, member])); | ||
client.on('guildMemberUpdate', (oldMember, newMember) => | ||
discordBotkit.trigger('guild_member_update', [discordBotkit, { oldMember, newMember }] | ||
)); | ||
client.on('guildMemberRemove', member => discordBotkit.trigger('guild_member_remove', [discordBotkit, member])); | ||
|
||
client.on('guildRoleCreate', role => discordBotkit.trigger('guild_role_create', [discordBotkit, role])); | ||
client.on('guildRoleUpdate', (oldRole, newRole) => discordBotkit.trigger('guild_role_update', [discordBotkit, { oldRole, newRole }])); | ||
client.on('guildRoleDelete', role => discordBotkit.trigger('guild_role_delete', [discordBotkit, role])); | ||
if (configuration.debug) { | ||
client.on('debug', info => { | ||
discordBotkit.debug(info); | ||
discordBotkit.trigger('debug', [discordBotkit, info]); | ||
}); | ||
} | ||
|
||
client.on('channelCreate', channel => discordBotkit.trigger('guild_role_create', [discordBotkit, channel])); | ||
client.on('channelUpdate', (newChannel, oldChannel) => discordBotkit.trigger('guild_role_update', [discordBotkit, { newChannel, oldChannel }])); | ||
client.on('channelDelete', channel => discordBotkit.trigger('guild_role_delete', [discordBotkit, channel])) | ||
|
||
// Stay Alive Please | ||
client.login(configuration.token); | ||
discordBotkit.startTicking(); | ||
|
||
return discordBotkit | ||
}; | ||
|
||
module.exports = DiscordBot; | ||
module.exports = DiscordBot; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
module.exports = { | ||
ingest: require('./ingest'), | ||
normalize: require('./normalize'), | ||
categorize: require('./categorize'), | ||
format: require('./format') | ||
} | ||
format: require('./format'), | ||
receive: require('./receive') | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
const cloneDeep = require('clone-deep'); | ||
|
||
// Safe clone done prior to ingestion to prevent issues f | ||
// from cloning a WS type | ||
const ingest = (bot, payload, source) => { | ||
payload.raw_message = source.raw; | ||
payload.message = cloneDeep(source.raw); | ||
return payload; | ||
}; | ||
|
||
module.exports.handler = (bot, opayload, source, next) => { | ||
ingest(bot, opayload, source); | ||
next(); | ||
} | ||
|
||
module.exports.exec = ingest; |
Oops, something went wrong.