Skip to content

Commit

Permalink
feat(avatar): avatar command guildonly property (#1)
Browse files Browse the repository at this point in the history
* Add new avatar that prints the command users' avatar and roles
* Add `guildOnly` property to stop commands from running in direct
  messages
  • Loading branch information
8Mobius8 authored Oct 3, 2020
1 parent 90643f2 commit a82a093
Show file tree
Hide file tree
Showing 4 changed files with 784 additions and 14 deletions.
29 changes: 29 additions & 0 deletions commands/avatar.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
const { MessageEmbed } = require('discord.js')

module.exports = {
name: 'avatar',
description: 'Shows your avatar url',
aliases: ['av'],
args: false,
guildOnly: true,
execute: function (message, args) {
const user = message.author
const name = !user.displayName ? user.username : user.displayName

const avatarEmbed = new MessageEmbed()
.setURL(user.displayAvatarURL())
.setTitle(name)
.setThumbnail(user.displayAvatarURL({ dynamic: true, size: 128 }))

if (message.guild.available) {
avatarEmbed
.addField('Roles',
message.guild.member(user).roles.cache
.mapValues(role => role.name)
.reduce((acc, role) => acc + '\n' + role)
)
}

message.reply(avatarEmbed)
}
}
30 changes: 18 additions & 12 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const http = require('http')
const fs = require('fs')
const Discord = require('discord.js')
const { prefix, token } = require('./config.js')
const { prefix, token, listenServer } = require('./config.js')
const { salutations } = require('./commands/phrases.json')

const client = new Discord.Client()
Expand All @@ -28,9 +28,9 @@ client.on('message', message => {
const commandName = args.shift().toLowerCase()

const command = client.commands.get(commandName) ||
client.commands.find(cmd => {
return cmd.aliases && cmd.aliases.includes(commandName)
})
client.commands.find(cmd => {
return cmd.aliases && cmd.aliases.includes(commandName)
})

if (!command) return

Expand Down Expand Up @@ -63,6 +63,10 @@ client.on('message', message => {
timestamps.set(message.author.id, now)
setTimeout(() => timestamps.delete(message.author.id), cooldownAmount)

if (command.guildOnly && message.channel.type === 'dm') {
return message.reply('I can\'t execute that command inside DMs!')
}

try {
command.execute(message, args)
} catch (error) {
Expand All @@ -75,7 +79,7 @@ client.login(token)
.catch((error) => {
if (error.code === 'TOKEN_INVALID') {
console.error('Discord token is not acceptable. Please set config.json or DISCORD_TOKEN appropiately.')
} else {
} else {
throw error
}
})
Expand All @@ -84,10 +88,12 @@ function randomFrom (anArray) {
return anArray[Math.floor(Math.random() * anArray.length)]
}

http.createServer(
function (request, response) {
const content = randomFrom(salutations)
response.writeHead(200)
response.end(content, 'utf-8')
}
).listen(process.env.PORT || 443)
if (listenServer) {
http.createServer(
function (request, response) {
const content = randomFrom(salutations)
response.writeHead(200)
response.end(content, 'utf-8')
}
).listen(process.env.PORT || 443)
}
Loading

0 comments on commit a82a093

Please sign in to comment.