Skip to content

Commit

Permalink
Add join/leave messages to backend and fix bot autorole \o/
Browse files Browse the repository at this point in the history
  • Loading branch information
retrixe committed Oct 14, 2018
1 parent 891b9a1 commit 9488c12
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 37 deletions.
79 changes: 46 additions & 33 deletions server/bot/index.ts
Original file line number Diff line number Diff line change
@@ -1,45 +1,58 @@
// We need types.
import { DB } from './imports/types'
import { Member, Message, Client } from 'eris'
import { Member, Message, Client, User, Guild } from 'eris'
import { Db } from 'mongodb'

// Database reading function.
import { getServerSettings } from './imports/tools'

// When client gains/loses a member, it will callback.
export const guildMemberEditCallback = (client: Client, event: string, db: Db) => async (
guild: { id: string }, member: Member
) => { // eslint-disable-line indent
// WeChill specific configuration.
if (guild.id === '402423671551164416' && event === 'guildMemberRemove') {
const message = `Well ${member.user.username}#${member.user.discriminator} left us.`
client.createMessage('402437089557217290', message)
return // Why wait.
} else if (guild.id === '402423671551164416' && event === 'guildMemberAdd') {
const message = `Yoyo <@${member.id}> welcome to WeChill, stay chill.`
client.createMessage('402437089557217290', message)
return // Why wait.
}
// When a server gains a member, this function will be called.
export const guildMemberAdd = (client: Client, db: Db) => async (
guild: Guild, member: Member
) => {
// Get server settings.
const serverSettings = await getServerSettings(db, guild.id)
/* if (event === 'guildMemberRemove' && serverSettings.joinLeaveMessages[2]) {
const channelID = member.guild.channels.find(
i => i.name === serverSettings.joinLeaveMessages[0]
).id
client.createMessage(channelID, serverSettings.joinLeaveMessages[2])
} else if (event === 'guildMemberAdd' && serverSettings.joinLeaveMessages[1]) {
const channelID = member.guild.channels.find(
i => i.name === serverSettings.joinLeaveMessages[0]
).id
client.createMessage(channelID, serverSettings.joinLeaveMessages[1])
} */
if (event === 'guildMemberAdd' && serverSettings.joinAutorole) {
const roles = serverSettings.joinAutorole.split('|')
for (let x = 0; x < roles.length; x++) {
const roleID = member.guild.roles.find(element => element.name === roles[x]).id
if (roles[x].startsWith('bot-') && member.user.bot) member.addRole(roleID)
else if (!roles[x].startsWith('bot-') && !member.user.bot) member.addRole(roleID)
}
// If there's autorole enabled..
if (serverSettings.joinAutorole) {
// For each role..
serverSettings.joinAutorole.split('|').forEach((role: string) => {
const roleName = role.startsWith('bot-') ? role.substr(4) : role
const roleID = member.guild.roles.find(element => element.name === roleName).id
if (!roleID) return
if (roleName.startsWith('bot-') && member.user.bot) member.addRole(roleID)
else if (!roleName.startsWith('bot-') && !member.user.bot) member.addRole(roleID)
})
}
// If join/leave messages is not configured/improperly configured..
if (!serverSettings.joinLeaveMessages) return
const { joinMessage, channelName } = serverSettings.joinLeaveMessages
if (!channelName || !joinMessage) return
// We send a message.
const channelID = guild.channels.find(i => i.name === channelName).id
const toSend = joinMessage
.split('{un}').join(member.user.username) // Replace the username.
.split('{m}').join(member.user.mention) // Replace the mention.
.split('{d}').join(member.user.discriminator) // Replace the discriminator.
try { client.createMessage(channelID, toSend) } catch (e) { }
}

// When a server loses a member, this function will be called.
export const guildMemberRemove = (client: Client, db: Db) => async (
guild: Guild, member: Member|{ id: string, user: User }
) => {
// Get server settings.
const serverSettings = await getServerSettings(db, guild.id)
// If join/leave messages is not configured/improperly configured..
if (!serverSettings.joinLeaveMessages) return
const { leaveMessage, channelName } = serverSettings.joinLeaveMessages
if (!channelName || !leaveMessage) return
// We send a message.
const channelID = guild.channels.find(i => i.name === channelName).id
const toSend = leaveMessage
.split('{un}').join(member.user.username) // Replace the username.
.split('{m}').join(member.user.mention) // Replace the mention.
.split('{d}').join(member.user.discriminator) // Replace the discriminator.
try { client.createMessage(channelID, toSend) } catch (e) {}
}

// When client recieves a message, it will callback.
Expand Down
6 changes: 3 additions & 3 deletions server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import { MongoClient } from 'mongodb'
// Import fs.
import { readdir, statSync } from 'fs'
// Import the bot.
import { guildMemberEditCallback } from './bot'
import { guildMemberAdd, guildMemberRemove } from './bot'
// Get the token needed.
import 'json5/lib/require'
import { token, mongoURL } from '../config.json5'
Expand All @@ -46,8 +46,8 @@ MongoClient.connect(mongoURL === 'dotenv' ? process.env.MONGO_URL : mongoURL, {
console.log('Bot connected successfully to MongoDB.')
const db = mongoDB.db('ivebot')
// When a server loses a member, it will callback.
client.on('guildMemberAdd', guildMemberEditCallback(client, 'guildMemberAdd', db))
client.on('guildMemberRemove', guildMemberEditCallback(client, 'guildMemberRemove', db))
client.on('guildMemberAdd', guildMemberAdd(client, db))
client.on('guildMemberRemove', guildMemberRemove(client, db))
// When a message is sent, the function should be called.
// client.on('messageCreate', botCallback(client, tempDB, db))
const commandParser = new CommandParser(client, tempDB, db)
Expand Down
2 changes: 1 addition & 1 deletion server/resolvers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export default (ctx: { tempDB: DB, client: Client }) => ({
leaveMessage: joinLeaveMessages.leaveMessage || ''
}
if (
member.permission.has('manageGuild') || host === ctx.tempDB.link[linkToken]
member && (member.permission.has('manageGuild') || host === ctx.tempDB.link[linkToken])
) return { serverId, addRoleForAll, joinLeaveMessages, joinAutorole }
else return { serverId: 'Forbidden.' }
},
Expand Down

0 comments on commit 9488c12

Please sign in to comment.