Skip to content

Commit

Permalink
♻️ Minor re-factor and memory optimization
Browse files Browse the repository at this point in the history
  • Loading branch information
crock committed Jun 21, 2020
1 parent b12bfbb commit 77ad6bd
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 34 deletions.
7 changes: 4 additions & 3 deletions README.md
Expand Up @@ -7,9 +7,10 @@ I'm assuming you already know how to get a bot token and add the bot to your ser

1. Rename the `.env.example` file to `.env` and fill in the appropriate values.
2. Edit the files `intro-message.md`, `community-guidelines.md`, and `verification-message.md` to add your own server rules and custom verification message. Do not change the filenames.
3. Run `npm install`
4. Run `npm start` or use a process manager like [pm2](https://pm2.keymetrics.io/) to keep the bot running across server restarts and automatically restart the bot if it crashes.
5. As the guild owner, run the command `!svm` in the channel you want designated as the verification channel. **Make sure the bot has access to send messages, delete messages, and react to messages in this channel!**
3. Open `index.js` and edit the values of the variables on Lines 2-5. Don't touch anything else unless you know what you are doing.
4. Run `npm install`
5. Run `npm start` or use a process manager like [pm2](https://pm2.keymetrics.io/) to keep the bot running across server restarts and automatically restart the bot if it crashes.
6. As the guild owner, run the command `!svm` in the channel you want designated as the verification channel. **Make sure the bot has access to send messages, delete messages, and react to messages in this channel!**

## Command Output
![screenshot of command output](https://i.imgur.com/WtcdYsM.png)
65 changes: 34 additions & 31 deletions index.js
@@ -1,22 +1,25 @@
// Some hardcoded variables that you can change
const prefix = "!"
const verified_role_id = "652669689683509249"
const welcome_emoji_id = "637434582500900864"
const reaction_emoji_id = "720420182538977281"

// Don't touch the code beflow this line!
if (process.env.NODE_ENV === 'development') {
require('dotenv').config()
}

const fs = require('fs')
const Discord = require('discord.js')
const client = new Discord.Client()

const prefix = "!"
const role = "652669689683509249"
const roleName = msg.guild.roles.get(verified_role_id).name

client.on('ready', async () => {
console.log(`Logged in as ${client.user.tag}!`)
await client.user.setActivity('Verify in #verification');
})

client.on('message', async msg => {
if (msg.author.bot) return
if (msg.content.indexOf(prefix) !== 0) return
client.on('message', msg => {

const args = msg.content
.slice(prefix.length)
Expand All @@ -29,7 +32,11 @@ client.on('message', async msg => {
.replace('/', '')

// svm = set verification message
if (command === 'svm') {
if (
!msg.author.bot
&& msg.content.indexOf(prefix) === 0
&& command === 'svm'
) {

// If sender of message is not the guild owner, cancel action
if (msg.member.guild.owner.id !== msg.member.id) return
Expand All @@ -40,45 +47,41 @@ client.on('message', async msg => {

const embed = new Discord.RichEmbed()

const welcomeTitle = '<:domaincord:637434582500900864> Welcome to Domaincord!'
const welcomeEmoji = `<:${msg.guild.emojis.get(welcome_emoji_id).identifier}>`
const welcomeTitle = `${welcomeEmoji} Welcome to ${msg.guild.name}!`

embed.addField(welcomeTitle, introMessageContent)
embed.addField('🎗 Community Guidelines', communityGuidelinesContent)
embed.addField('🔐 Getting Verified', verificationMessageContent)

const theVerificationMessage = await msg.channel.send({embed})
theVerificationMessage.react('720420182538977281')

await msg.delete()
return
msg.channel.send({embed}).then(theVerificationMessage => theVerificationMessage.react(reaction_emoji_id))
msg.delete()
}

return
})

client.on('messageReactionAdd', async ( { message: { channel } }, user ) => {
client.on('messageReactionAdd', ( { message: { channel } }, user ) => {
if (/verification/.test(channel.name)) {
try {
const member = await channel.guild
.fetchMember(user)

await member.addRole(role)
return
} catch (error) {
channel.guild.fetchMember(user).then(member => {
return member.addRole(role)
}).then(() => {
console.log(`The ${roleName} role has been added to member ${user.tag} successfully!`)
}).catch(error => {
console.error(error)
}
})
}
})

client.on('messageReactionRemove', async ( { message: { channel } }, user ) => {
client.on('messageReactionRemove', ( { message: { channel } }, user ) => {
if (/verification/.test(channel.name)) {
try {
const member = await channel.guild
.fetchMember(user)

await member.removeRole(role)
return
} catch (error) {
channel.guild.fetchMember(user).then(member => {
return member.removeRole(role)
}).then(() => {
console.log(`The ${roleName} has been removed from member ${user.tag} successfully!`)
}).catch(error => {
console.error(error)
}
})
}
})

Expand Down

0 comments on commit 77ad6bd

Please sign in to comment.