Skip to content

Commit

Permalink
Move admin.ts, fix ban error, enhance getUser and add /userinfo.
Browse files Browse the repository at this point in the history
  • Loading branch information
retrixe committed Oct 14, 2018
1 parent 18dd0f4 commit 39731de
Show file tree
Hide file tree
Showing 7 changed files with 94 additions and 23 deletions.
30 changes: 15 additions & 15 deletions server/bot/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,20 +96,6 @@ export class Command {
}

async execute (context: Context, message: Message, args: string[]) { // eslint-disable-line indent
// We check for arguments.
if (args.length === 0 && this.argsRequired) {
message.channel.createMessage(this.invalidUsageMessage)
return
// Guild and DM only.
} else if (this.guildOnly && message.channel.type !== 0) return
else if (this.dmOnly && message.channel.type !== 1) return
// Check for permissions.
else if (!this.requirementsCheck(message)) {
message.channel.createMessage(
`**Thankfully, you don't have enough permissions for that, you ${getInsult()}.**`
)
return
}
// Define 2 vars.
let messageToSend: MessageContent | void | Promise<MessageContent> | Promise<void>
// If it's a function, we call it first.
Expand Down Expand Up @@ -145,6 +131,20 @@ export default class CommandParser {
}
const args = message.content.split(' ')
args.shift()
// We check for arguments.
if (args.length === 0 && command.argsRequired) {
message.channel.createMessage(command.invalidUsageMessage)
return
// Guild and DM only.
} else if (command.guildOnly && message.channel.type !== 0) return
else if (command.dmOnly && message.channel.type !== 1) return
// Check for permissions.
else if (!command.requirementsCheck(message)) {
message.channel.createMessage(
`**Thankfully, you don't have enough permissions for that, you ${getInsult()}.**`
)
return
}
// We get the exact content to send.
const messageToSend = await command.execute(context, message, args)
// We define a sent variable to keep track.
Expand All @@ -154,7 +154,7 @@ export default class CommandParser {
message.member.guild.channels.find(i => i.id === message.channel.id)
.permissionsOf(this.client.user.id).has('sendMessages')
) sent = await message.channel.createMessage(messageToSend)
if (command.postGenerator) command.postGenerator(message, args, sent)
if (command.postGenerator) command.postGenerator(message, args, sent, context)
if (command.deleteCommand) message.delete('Automatically deleted by IveBot.')
}

Expand Down
4 changes: 3 additions & 1 deletion server/bot/commands/admin/ban.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ export const handleBan: Command = {
} else if (!user && client.users.find(i => i.id === userSpecified)) {
user = client.users.find(i => i.id === userSpecified)
} else if (!user && [18, 17].includes(userSpecified.length) && !isNaN(+userSpecified)) {
user = await client.getRESTUser(userSpecified)
try { user = await client.getRESTUser(userSpecified) } catch (e) {
return 'I cannot find that user.'
}
} else return 'I cannot find that user.'
if (!user) return `Specify a valid user, ${getInsult()}.`
// If the user cannot ban the person..
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { Command } from '../imports/types'
import { getInsult, getUser } from '../imports/tools'
import { checkRolePosition } from '../imports/permissions'
import { Command } from '../../imports/types'
import { getInsult, getUser } from '../../imports/tools'
import { checkRolePosition } from '../../imports/permissions'
import { Message } from 'eris'
export { handleGiverole, handleTakerole } from './admin/roles'
export { handleWarn, handleWarnings, handleClearwarns, handleRemovewarn } from './admin/warn'
export { handleMute, handleUnmute } from './admin/mute'
export { handleBan, handleUnban } from './admin/ban'
export { handleGiverole, handleTakerole } from './roles'
export { handleWarn, handleWarnings, handleClearwarns, handleRemovewarn } from './warn'
export { handleMute, handleUnmute } from './mute'
export { handleBan, handleUnban } from './ban'

export const handlePurge: Command = {
name: 'purge',
Expand Down
1 change: 1 addition & 0 deletions server/bot/commands/help.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ let generalHelp = ` ** Jony Ive can do many commands 📡**
\`/remindme\` - Reminders.
\`/leave\` - Makes you leave the server.
\`/avatar\` - Avatar of a user.
\`/userinfo\` - User info.
\`/about\`, \`/ping\`, \`/uptime\` and \`/version\` - About the running instance of IveBot.
\`/giverole\` and \`/takerole\` - Edit roles.
**Administrative commands.**
Expand Down
61 changes: 61 additions & 0 deletions server/bot/commands/tools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,68 @@ import * as ms from 'ms'
import { version } from '../../../package.json'
import { execSync } from 'child_process'
import 'json5/lib/require'
import * as moment from 'moment'
import { host } from '../../../config.json5'
import { getUser, getInsult } from '../imports/tools'

export const handleUserinfo: Command = {
name: 'userinfo',
aliases: ['useri', 'uinfo', 'ui'],
opts: {
description: 'Displays info on a particular user.',
fullDescription: 'Displays info on a particular user.',
example: '/userinfo voldemort#6931',
usage: '/userinfo <user by ID/mention/username>',
argsRequired: false
},
generator: async (message, args, { client }) => {
// Find the user ID.
const toGet = args.length === 0 ? message.author.id : args.shift()
let user = getUser(message, toGet)
if (!user && message.author.id === host && [18, 17].includes(toGet.length) && !isNaN(+toGet)) {
try { user = await client.getRESTUser(toGet) } catch (e) {}
}
if (!user) return `Specify a valid member of this guild, ${getInsult()}.`
// Display information.
const member = message.member.guild.members.find(i => i.user.id === user.id)
const color = member
? member.roles.map(i => member.guild.roles.get(i)).sort(
(a, b) => a.position > b.position ? 0 : 1
).shift().color : 0
return {
content: `👥 **Userinfo on ${user.username}:**`,
embed: {
author: { name: `User info`, icon_url: user.avatarURL },
title: `${user.username}#${user.discriminator}`,
description: user.mention,
thumbnail: { url: user.avatarURL },
color,
fields: [
{ name: 'Status', value: member ? member.status : 'N/A', inline: true },
// { name: 'Join Position }
// { name: 'Name', value: user.username, inline: true },
// { name: 'Discriminator', value: user.discriminator, inline: true },
{
name: 'Joined server at',
value: member ? moment(member.joinedAt).format('DD/MM/YYYY, hh:mm:ss A') : 'N/A',
inline: true
},
{ name: 'User ID', value: user.id, inline: true },
{
name: 'Registered at',
value: moment(user.createdAt).format('DD/MM/YYYY, hh:mm:ss A'),
inline: true
},
{
name: `Roles (${member ? member.roles.length : 'N/A'})`,
value: member ? member.roles.map(i => `<@&${i}>`).join(' ') : 'N/A'
}
// { name: 'Permissions' }
]
}
}
}
}

export const handleToken: Command = {
name: 'token',
Expand Down
6 changes: 6 additions & 0 deletions server/bot/imports/tools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ export const getUser = (message: Message, arg: string) => {
else if (mentions.length && mentions[0].id === getIdFromMention(arg)) return mentions[0]
else if (guild.members.find(i => i.username.toLowerCase() === arg.toLowerCase())) {
return guild.members.find(i => i.username.toLowerCase() === arg.toLowerCase()).user
} else if (guild.members.find(
i => i.username.toLowerCase() + '#' + i.discriminator === arg.toLowerCase()
)) {
return guild.members.find(
i => i.username.toLowerCase() + '#' + i.discriminator === arg.toLowerCase()
).user
}
}

Expand Down
1 change: 1 addition & 0 deletions server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ MongoClient.connect(mongoURL === 'dotenv' ? process.env.MONGO_URL : mongoURL, {
if (err) { console.error(err); throw new Error('Commands could not be retrieved.') }
// This only supports two levels of files, one including files inside commands, and one in..
// a subfolder.
commandFiles.push('admin/index.ts')
commandFiles.forEach(commandFile => {
// If it's a file..
if (statSync('./server/bot/commands/' + commandFile).isFile() && commandFile.endsWith('.ts')) {
Expand Down

0 comments on commit 39731de

Please sign in to comment.