up
This commit is contained in:
parent
98874dd7e7
commit
7bdbb165f4
6 changed files with 73 additions and 78 deletions
|
@ -1,6 +1,5 @@
|
||||||
import { Message } from '../structures/message.ts'
|
import { Message } from '../structures/message.ts'
|
||||||
import { GuildTextChannel } from '../structures/textChannel.ts'
|
import { GuildTextChannel } from '../structures/textChannel.ts'
|
||||||
import { awaitSync } from '../utils/mixedPromise.ts'
|
|
||||||
import { Client, ClientOptions } from './client.ts'
|
import { Client, ClientOptions } from './client.ts'
|
||||||
import {
|
import {
|
||||||
CategoriesManager,
|
CategoriesManager,
|
||||||
|
@ -129,35 +128,29 @@ export class CommandClient extends Client implements CommandClientOptions {
|
||||||
async processMessage(msg: Message): Promise<any> {
|
async processMessage(msg: Message): Promise<any> {
|
||||||
if (!this.allowBots && msg.author.bot === true) return
|
if (!this.allowBots && msg.author.bot === true) return
|
||||||
|
|
||||||
const isUserBlacklisted = await awaitSync(
|
const isUserBlacklisted = await this.isUserBlacklisted(msg.author.id)
|
||||||
this.isUserBlacklisted(msg.author.id)
|
if (isUserBlacklisted) return
|
||||||
)
|
|
||||||
if (isUserBlacklisted === true) return
|
|
||||||
|
|
||||||
const isChannelBlacklisted = await awaitSync(
|
const isChannelBlacklisted = await this.isChannelBlacklisted(msg.channel.id)
|
||||||
this.isChannelBlacklisted(msg.channel.id)
|
if (isChannelBlacklisted) return
|
||||||
)
|
|
||||||
if (isChannelBlacklisted === true) return
|
|
||||||
|
|
||||||
if (msg.guild !== undefined) {
|
if (msg.guild !== undefined) {
|
||||||
const isGuildBlacklisted = await awaitSync(
|
const isGuildBlacklisted = await this.isGuildBlacklisted(msg.guild.id)
|
||||||
this.isGuildBlacklisted(msg.guild.id)
|
if (isGuildBlacklisted) return
|
||||||
)
|
|
||||||
if (isGuildBlacklisted === true) return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
let prefix: string | string[] = []
|
let prefix: string | string[] = []
|
||||||
if (typeof this.prefix === 'string') prefix = [...prefix, this.prefix]
|
if (typeof this.prefix === 'string') prefix = [...prefix, this.prefix]
|
||||||
else prefix = [...prefix, ...this.prefix]
|
else prefix = [...prefix, ...this.prefix]
|
||||||
|
|
||||||
const userPrefix = await awaitSync(this.getUserPrefix(msg.author.id))
|
const userPrefix = await this.getUserPrefix(msg.author.id)
|
||||||
if (userPrefix !== undefined) {
|
if (userPrefix !== undefined) {
|
||||||
if (typeof userPrefix === 'string') prefix = [...prefix, userPrefix]
|
if (typeof userPrefix === 'string') prefix = [...prefix, userPrefix]
|
||||||
else prefix = [...prefix, ...userPrefix]
|
else prefix = [...prefix, ...userPrefix]
|
||||||
}
|
}
|
||||||
|
|
||||||
if (msg.guild !== undefined) {
|
if (msg.guild !== undefined) {
|
||||||
const guildPrefix = await awaitSync(this.getGuildPrefix(msg.guild.id))
|
const guildPrefix = await this.getGuildPrefix(msg.guild.id)
|
||||||
if (guildPrefix !== undefined) {
|
if (guildPrefix !== undefined) {
|
||||||
if (typeof guildPrefix === 'string') prefix = [...prefix, guildPrefix]
|
if (typeof guildPrefix === 'string') prefix = [...prefix, guildPrefix]
|
||||||
else prefix = [...prefix, ...guildPrefix]
|
else prefix = [...prefix, ...guildPrefix]
|
||||||
|
@ -361,10 +354,10 @@ export class CommandClient extends Client implements CommandClientOptions {
|
||||||
try {
|
try {
|
||||||
this.emit('commandUsed', ctx)
|
this.emit('commandUsed', ctx)
|
||||||
|
|
||||||
const beforeExecute = await awaitSync(command.beforeExecute(ctx))
|
const beforeExecute = await command.beforeExecute(ctx)
|
||||||
if (beforeExecute === false) return
|
if (beforeExecute === false) return
|
||||||
|
|
||||||
const result = await awaitSync(command.execute(ctx))
|
const result = await command.execute(ctx)
|
||||||
command.afterExecute(ctx, result)
|
command.afterExecute(ctx, result)
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
this.emit('commandError', ctx, e)
|
this.emit('commandError', ctx, e)
|
||||||
|
|
|
@ -201,7 +201,7 @@ client.on('messageCreate', async (msg: Message) => {
|
||||||
)
|
)
|
||||||
.join('\n\n')}`
|
.join('\n\n')}`
|
||||||
)
|
)
|
||||||
} else if (msg.content === '!getPermissions') {
|
} else if (msg.content === '!perms') {
|
||||||
if (msg.channel.type !== ChannelTypes.GUILD_TEXT) {
|
if (msg.channel.type !== ChannelTypes.GUILD_TEXT) {
|
||||||
return msg.channel.send("This isn't a guild text channel!")
|
return msg.channel.send("This isn't a guild text channel!")
|
||||||
}
|
}
|
||||||
|
@ -210,7 +210,11 @@ client.on('messageCreate', async (msg: Message) => {
|
||||||
// eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion
|
// eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion
|
||||||
msg.member as Member
|
msg.member as Member
|
||||||
)
|
)
|
||||||
msg.channel.send(`Your permissions:\n${permissions.toArray().join('\n')}`)
|
msg.channel.send(
|
||||||
|
Object.entries(permissions.serialize())
|
||||||
|
.map((e) => `${e[0]}: ${e[1] === true ? '`✅`' : '`❌`'}`)
|
||||||
|
.join('\n')
|
||||||
|
)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
|
@ -1,35 +1,36 @@
|
||||||
// https://discord.com/developers/docs/topics/permissions#permissions-bitwise-permission-flags
|
// https://discord.com/developers/docs/topics/permissions#permissions-bitwise-permission-flags
|
||||||
|
|
||||||
export const PermissionFlags: { [key: string]: number } = {
|
export const PermissionFlags: { [key: string]: bigint } = {
|
||||||
CREATE_INSTANT_INVITE: 1 << 0,
|
CREATE_INSTANT_INVITE: 1n << 0n,
|
||||||
KICK_MEMBERS: 1 << 1,
|
KICK_MEMBERS: 1n << 1n,
|
||||||
BAN_MEMBERS: 1 << 2,
|
BAN_MEMBERS: 1n << 2n,
|
||||||
ADMINISTRATOR: 1 << 3,
|
ADMINISTRATOR: 1n << 3n,
|
||||||
MANAGE_CHANNELS: 1 << 4,
|
MANAGE_CHANNELS: 1n << 4n,
|
||||||
MANAGE_GUILD: 1 << 5,
|
MANAGE_GUILD: 1n << 5n,
|
||||||
ADD_REACTIONS: 1 << 6,
|
ADD_REACTIONS: 1n << 6n,
|
||||||
VIEW_AUDIT_LOG: 1 << 7,
|
VIEW_AUDIT_LOG: 1n << 7n,
|
||||||
PRIORITY_SPEAKER: 1 << 8,
|
PRIORITY_SPEAKER: 1n << 8n,
|
||||||
STREAM: 1 << 9,
|
STREAM: 1n << 9n,
|
||||||
VIEW_CHANNEL: 1 << 10,
|
VIEW_CHANNEL: 1n << 10n,
|
||||||
SEND_MESSAGES: 1 << 11,
|
SEND_MESSAGES: 1n << 11n,
|
||||||
SEND_TTS_MESSAGES: 1 << 12,
|
SEND_TTS_MESSAGES: 1n << 12n,
|
||||||
MANAGE_MESSAGES: 1 << 13,
|
MANAGE_MESSAGES: 1n << 13n,
|
||||||
EMBED_LINKS: 1 << 14,
|
EMBED_LINKS: 1n << 14n,
|
||||||
ATTACH_FILES: 1 << 15,
|
ATTACH_FILES: 1n << 15n,
|
||||||
READ_MESSAGE_HISTORY: 1 << 16,
|
READ_MESSAGE_HISTORY: 1n << 16n,
|
||||||
MENTION_EVERYONE: 1 << 17,
|
MENTION_EVERYONE: 1n << 17n,
|
||||||
USE_EXTERNAL_EMOJIS: 1 << 18,
|
USE_EXTERNAL_EMOJIS: 1n << 18n,
|
||||||
VIEW_GUILD_INSIGHTS: 1 << 19,
|
VIEW_GUILD_INSIGHTS: 1n << 19n,
|
||||||
CONNECT: 1 << 20,
|
CONNECT: 1n << 20n,
|
||||||
SPEAK: 1 << 21,
|
SPEAK: 1n << 21n,
|
||||||
MUTE_MEMBERS: 1 << 22,
|
MUTE_MEMBERS: 1n << 22n,
|
||||||
DEAFEN_MEMBERS: 1 << 23,
|
DEAFEN_MEMBERS: 1n << 23n,
|
||||||
MOVE_MEMBERS: 1 << 24,
|
MOVE_MEMBERS: 1n << 24n,
|
||||||
USE_VAD: 1 << 25,
|
USE_VAD: 1n << 25n,
|
||||||
CHANGE_NICKNAME: 1 << 26,
|
CHANGE_NICKNAME: 1n << 26n,
|
||||||
MANAGE_NICKNAMES: 1 << 27,
|
MANAGE_NICKNAMES: 1n << 27n,
|
||||||
MANAGE_ROLES: 1 << 28,
|
MANAGE_ROLES: 1n << 28n,
|
||||||
MANAGE_WEBHOOKS: 1 << 29,
|
MANAGE_WEBHOOKS: 1n << 29n,
|
||||||
MANAGE_EMOJIS: 1 << 30
|
MANAGE_EMOJIS: 1n << 30n,
|
||||||
|
USE_SLASH_COMMANDS: 1n << 31n
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,19 +6,21 @@ export type BitFieldResolvable =
|
||||||
| string
|
| string
|
||||||
| string[]
|
| string[]
|
||||||
| BitField[]
|
| BitField[]
|
||||||
|
| bigint
|
||||||
|
| Array<bigint>
|
||||||
|
|
||||||
/** Bit Field utility to work with Bits and Flags */
|
/** Bit Field utility to work with Bits and Flags */
|
||||||
export class BitField {
|
export class BitField {
|
||||||
#flags: { [name: string]: number } = {}
|
#flags: { [name: string]: number | bigint } = {}
|
||||||
bitfield: number
|
bitfield: bigint
|
||||||
|
|
||||||
constructor(flags: { [name: string]: number }, bits: any) {
|
constructor(flags: { [name: string]: number | bigint }, bits: any) {
|
||||||
this.#flags = flags
|
this.#flags = flags
|
||||||
this.bitfield = BitField.resolve(this.#flags, bits)
|
this.bitfield = BitField.resolve(this.#flags, bits)
|
||||||
}
|
}
|
||||||
|
|
||||||
any(bit: BitFieldResolvable): boolean {
|
any(bit: BitFieldResolvable): boolean {
|
||||||
return (this.bitfield & BitField.resolve(this.#flags, bit)) !== 0
|
return (this.bitfield & BitField.resolve(this.#flags, bit)) !== 0n
|
||||||
}
|
}
|
||||||
|
|
||||||
equals(bit: BitFieldResolvable): boolean {
|
equals(bit: BitFieldResolvable): boolean {
|
||||||
|
@ -42,7 +44,7 @@ export class BitField {
|
||||||
}
|
}
|
||||||
|
|
||||||
add(...bits: BitFieldResolvable[]): BitField {
|
add(...bits: BitFieldResolvable[]): BitField {
|
||||||
let total = 0
|
let total = 0n
|
||||||
for (const bit of bits) {
|
for (const bit of bits) {
|
||||||
total |= BitField.resolve(this.#flags, bit)
|
total |= BitField.resolve(this.#flags, bit)
|
||||||
}
|
}
|
||||||
|
@ -53,7 +55,7 @@ export class BitField {
|
||||||
}
|
}
|
||||||
|
|
||||||
remove(...bits: BitFieldResolvable[]): BitField {
|
remove(...bits: BitFieldResolvable[]): BitField {
|
||||||
let total = 0
|
let total = 0n
|
||||||
for (const bit of bits) {
|
for (const bit of bits) {
|
||||||
total |= BitField.resolve(this.#flags, bit)
|
total |= BitField.resolve(this.#flags, bit)
|
||||||
}
|
}
|
||||||
|
@ -63,12 +65,12 @@ export class BitField {
|
||||||
return this
|
return this
|
||||||
}
|
}
|
||||||
|
|
||||||
flags(): { [name: string]: number } {
|
flags(): { [name: string]: bigint | number } {
|
||||||
return this.#flags
|
return this.#flags
|
||||||
}
|
}
|
||||||
|
|
||||||
serialize(...hasParams: any[]): { [key: string]: any } {
|
serialize(...hasParams: any[]): { [key: string]: boolean } {
|
||||||
const serialized: { [key: string]: any } = {}
|
const serialized: { [key: string]: boolean } = {}
|
||||||
for (const [flag, bit] of Object.entries(this.#flags))
|
for (const [flag, bit] of Object.entries(this.#flags))
|
||||||
serialized[flag] = this.has(
|
serialized[flag] = this.has(
|
||||||
BitField.resolve(this.#flags, bit),
|
BitField.resolve(this.#flags, bit),
|
||||||
|
@ -83,11 +85,11 @@ export class BitField {
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
toJSON(): number {
|
toJSON(): string {
|
||||||
return this.bitfield
|
return this.bitfield.toString()
|
||||||
}
|
}
|
||||||
|
|
||||||
valueOf(): number {
|
valueOf(): bigint {
|
||||||
return this.bitfield
|
return this.bitfield
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -95,9 +97,10 @@ export class BitField {
|
||||||
yield* this.toArray()
|
yield* this.toArray()
|
||||||
}
|
}
|
||||||
|
|
||||||
static resolve(flags: any, bit: BitFieldResolvable = 0): number {
|
static resolve(flags: any, bit: BitFieldResolvable = 0n): bigint {
|
||||||
if (typeof bit === 'string' && !isNaN(parseInt(bit))) return parseInt(bit)
|
if (typeof bit === 'bigint') return bit
|
||||||
if (typeof bit === 'number' && bit >= 0) return bit
|
if (typeof bit === 'string' && !isNaN(parseInt(bit))) return BigInt(bit)
|
||||||
|
if (typeof bit === 'number' && bit >= 0) return BigInt(bit)
|
||||||
if (bit instanceof BitField) return this.resolve(flags, bit.bitfield)
|
if (bit instanceof BitField) return this.resolve(flags, bit.bitfield)
|
||||||
if (Array.isArray(bit))
|
if (Array.isArray(bit))
|
||||||
return (bit.map as any)((p: any) => this.resolve(flags, p)).reduce(
|
return (bit.map as any)((p: any) => this.resolve(flags, p)).reduce(
|
||||||
|
|
|
@ -1,3 +0,0 @@
|
||||||
export const awaitSync = async (val: any | Promise<any>): Promise<any> => {
|
|
||||||
return val instanceof Promise ? await val : val
|
|
||||||
}
|
|
|
@ -2,18 +2,15 @@
|
||||||
import { PermissionFlags } from '../types/permissionFlags.ts'
|
import { PermissionFlags } from '../types/permissionFlags.ts'
|
||||||
import { BitField, BitFieldResolvable } from './bitfield.ts'
|
import { BitField, BitFieldResolvable } from './bitfield.ts'
|
||||||
|
|
||||||
export type PermissionResolvable =
|
export type PermissionResolvable = BitFieldResolvable
|
||||||
| string
|
|
||||||
| string[]
|
|
||||||
| number
|
|
||||||
| number[]
|
|
||||||
| Permissions
|
|
||||||
| PermissionResolvable[]
|
|
||||||
|
|
||||||
/** Manages Discord's Bit-based Permissions */
|
/** Manages Discord's Bit-based Permissions */
|
||||||
export class Permissions extends BitField {
|
export class Permissions extends BitField {
|
||||||
static DEFAULT = 104324673
|
static DEFAULT = 104324673n
|
||||||
static ALL = Object.values(PermissionFlags).reduce((all, p) => all | p, 0)
|
static ALL = Object.values(PermissionFlags).reduce(
|
||||||
|
(all, p) => BigInt(all) | BigInt(p),
|
||||||
|
0n
|
||||||
|
)
|
||||||
|
|
||||||
constructor(bits: BitFieldResolvable) {
|
constructor(bits: BitFieldResolvable) {
|
||||||
super(PermissionFlags, bits)
|
super(PermissionFlags, bits)
|
||||||
|
|
Loading…
Reference in a new issue