Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get to successful compile #14

Merged
merged 19 commits into from
Nov 1, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Rename D to Discord in many places to improve readablity
  • Loading branch information
tonyxforce committed Nov 1, 2023
commit b98bacadb50fca02170dabd3df4e9718a9f507ab
12 changes: 6 additions & 6 deletions tests/Client.test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import * as D from 'discord.js'
import * as Discord from 'discord.js'
import * as DM from '../src'
import type {MatchObjectGuild} from './utils'
import './matchers'

describe('mockClient', () => {
test('ready event is emitted', async () => {
const client = new D.Client({intents: []})
const client = new Discord.Client({intents: []})
const promise = new Promise<void>(resolve => {
client.on('ready', () => resolve())
})
Expand All @@ -24,9 +24,9 @@ describe('mockClient', () => {
.addGuildWithBot({id: id2}, {}, app)

test('are emitted with GUILDS intent', async () => {
const client = new D.Client({intents: ['GUILDS']})
const emittedGuilds = new Promise<ReadonlySet<D.Guild>>(resolve => {
const guilds = new Set<D.Guild>()
const client = new Discord.Client({intents: ['GUILDS']})
const emittedGuilds = new Promise<ReadonlySet<Discord.Guild>>(resolve => {
const guilds = new Set<Discord.Guild>()
client.on('guildCreate', guild => {
guilds.add(guild)
if (guilds.size === 2) resolve(guilds)
Expand All @@ -42,7 +42,7 @@ describe('mockClient', () => {
})

test('not emitted without GUILDS intent', async () => {
const client = new D.Client({intents: []})
const client = new Discord.Client({intents: []})
const promise = new Promise<void>((resolve, reject) => {
setTimeout(resolve, 500)
client.on('guildCreate', ({id}) => {
Expand Down
4 changes: 2 additions & 2 deletions tests/channels/messages/patch.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {ChannelType, PermissionFlagsBits} from 'discord-api-types/v9'
import * as D from 'discord.js'
import * as Discord from 'discord.js'
import {guildWithBot, withClient, withClientF} from '../../utils'
import {getChannel, send} from './utils'
import type * as DM from '../../../src'
Expand Down Expand Up @@ -36,7 +36,7 @@ describe('successes', () => {
withClientF(async client => {
const message = await send(client, {
content: 'foo',
embeds: [new D.MessageEmbed({title: 'foo'})]
embeds: [new Discord.MessageEmbed({title: 'foo'})]
})
const oldEmbeds = message.embeds
const newContent = 'bar'
Expand Down
12 changes: 6 additions & 6 deletions tests/channels/messages/post.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
*/

import {ChannelType} from 'discord-api-types/v9'
import * as D from 'discord.js'
import * as Discord from 'discord.js'
import {guildWithBot, withClient, withClientF} from '../../utils'
import {getChannel, send} from './utils'
import type {Snowflake} from 'discord-api-types/v9'
Expand Down Expand Up @@ -50,7 +50,7 @@ describe('successes', () => {
test('basic embed', async () => {
const title = 'title'
const description = 'description'
const fields: D.EmbedFieldData[] = [
const fields: Discord.EmbedFieldData[] = [
{name: 'field 1', value: 'field 1 value'},
{name: 'field 2', value: 'field 2 value'}
]
Expand All @@ -60,7 +60,7 @@ describe('successes', () => {
(
await send(client, {embeds: [{title, description, fields}]})
).embeds
).toMatchObject<DeepPartialOmit<D.MessageEmbed[]>>([
).toMatchObject<DeepPartialOmit<Discord.MessageEmbed[]>>([
{title, description, fields}
]),
defaultOpts()
Expand All @@ -75,7 +75,7 @@ describe('successes', () => {
})
expect(message.attachments.size).toBe(1)
expect(message.attachments.first()!).toMatchObject<
DeepPartialOmit<D.MessageAttachment>
DeepPartialOmit<Discord.MessageAttachment>
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment -- asymmetric matcher
>({name, attachment: expect.stringContaining(message.id)})
}, defaultOpts())
Expand All @@ -91,7 +91,7 @@ describe('successes', () => {
const roleId1 = '20'
const expectMentions = (
content: string,
allowedMentions: D.MessageMentionOptions | undefined,
allowedMentions: Discord.MessageMentionOptions | undefined,
{
everyone,
users,
Expand All @@ -116,7 +116,7 @@ describe('successes', () => {
{roles: [{id: roleId1}]},
{
backendOpts: {
users: new D.Collection([
users: new Discord.Collection([
[userId1, {}],
[userId2, {}]
])
Expand Down
12 changes: 6 additions & 6 deletions tests/channels/messages/utils.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
import assert from 'assert'
import type * as D from 'discord.js'
import type * as Discord from 'discord.js'

export const getChannel = (client: D.Client): D.GuildTextBasedChannel => {
export const getChannel = (client: Discord.Client): Discord.GuildTextBasedChannel => {
const guild = client.guilds.cache.first()
assert(
guild,
'There are no cached guilds. Perhaps you forgot to include backend?'
)
const channel = guild.channels.cache.find(
(chan): chan is D.GuildTextBasedChannel => chan.isText()
(chan): chan is Discord.GuildTextBasedChannel => chan.isText()
)
assert(channel, 'There are no text channels!')
return channel
}

export const send = async (
client: D.Client,
options: D.MessageOptions | D.MessagePayload | string
): Promise<D.Message> => getChannel(client).send(options)
client: Discord.Client,
options: Discord.MessageOptions | Discord.MessagePayload | string
): Promise<Discord.Message> => getChannel(client).send(options)
4 changes: 2 additions & 2 deletions tests/guilds.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as D from 'discord.js'
import * as Discord from 'discord.js'
import * as DM from '../src'

describe('initial guilds', () => {
Expand All @@ -8,7 +8,7 @@ describe('initial guilds', () => {
const backend = new DM.Backend()
const app = backend.addApplication({bot: {id: userId}})
backend.addGuildWithBot({id: guildId, owner_id: app.bot.id}, {}, app)
const client = new D.Client({intents: ['GUILDS']})
const client = new Discord.Client({intents: ['GUILDS']})
const promise = new Promise<void>(resolve => {
client.on('guildCreate', async guild => {
expect(guild.id).toBe(guildId)
Expand Down
20 changes: 10 additions & 10 deletions tests/guilds/post.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
*/

import {RESTJSONErrorCodes} from 'discord-api-types/v9'
import * as D from 'discord.js'
import * as Discord from 'discord.js'
import * as DM from '../../src'
import {withClient, withClientF} from '../utils'
import type {ChannelTypes} from 'discord.js/typings/enums'
Expand All @@ -13,10 +13,10 @@ import '../matchers'

// TODO: fix Discord.js types
type GuildCreateOpts = Override<
D.GuildCreateOptions,
Discord.GuildCreateOptions,
{
channels?: Override<
D.PartialChannelData,
Discord.PartialChannelData,
{type?: keyof typeof ChannelTypes}
>[]
}
Expand Down Expand Up @@ -50,10 +50,10 @@ describe('successes', () => {
'overriding basic defaults',
withClientF(async client => {
const afkTimeout = 60
const defaultMessageNotifications: D.DefaultMessageNotificationLevel =
const defaultMessageNotifications: Discord.DefaultMessageNotificationLevel =
'ONLY_MENTIONS'
const explicitContentFilter: D.ExplicitContentFilterLevel = 'ALL_MEMBERS'
const verificationLevel: D.VerificationLevel = 'HIGH'
const explicitContentFilter: Discord.ExplicitContentFilterLevel = 'ALL_MEMBERS'
const verificationLevel: Discord.VerificationLevel = 'HIGH'
await expect(
client.guilds.create(name, {
afkTimeout,
Expand Down Expand Up @@ -123,15 +123,15 @@ describe('successes', () => {
'channel overrides',
withClientF(async client => {
const id = 0
const deny: D.PermissionString = 'VIEW_CHANNEL'
const deny: Discord.PermissionString = 'VIEW_CHANNEL'
const guild = await client.guilds.create(name, {
roles: [{}, {id}],
channels: [{name, permissionOverwrites: [{id, deny}]}]
})
const channel = guild.channels.cache.first()!
expect(channel).toBeInstanceOf(D.TextChannel)
expect(channel).toBeInstanceOf(Discord.TextChannel)
expect(
(channel as D.TextChannel).permissionOverwrites.cache.get(
(channel as Discord.TextChannel).permissionOverwrites.cache.get(
guild.roles.cache.findKey(role => role.id !== guild.id)!
)?.deny
).toEqualBitfield(deny)
Expand All @@ -158,7 +158,7 @@ describe('errors', () => {
// every time otherwise TypeErrors may occur
// This is why a function is sometimes used to provide the options
const _formErr = (
...args: Parameters<D.GuildManager['create']>
...args: Parameters<Discord.GuildManager['create']>
): (() => Promise<void>) =>
withClientF(async client =>
expect(client.guilds.create(...args)).toThrowAPIFormError()
Expand Down
6 changes: 3 additions & 3 deletions tests/guilds/templates.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import * as DM from '../../src'
import {guildWithBot, withClient, withClientF} from '../utils'
import type * as D from 'discord.js'
import type * as Discord from 'discord.js'
import type {DeepPartialOmit, MatchObjectGuild} from '../utils'
import '../matchers'

type MatchObjectTemplate = DeepPartialOmit<D.GuildTemplate, 'valueOf'>
type MatchObjectTemplate = DeepPartialOmit<Discord.GuildTemplate, 'valueOf'>

const guildId = '0'

Expand Down Expand Up @@ -118,7 +118,7 @@ describe('create guild template', () => {
})
})

const getTemplate = async (client: D.Client): Promise<D.GuildTemplate> =>
const getTemplate = async (client: Discord.Client): Promise<Discord.GuildTemplate> =>
(await client.guilds.cache.get(guildId)!.fetchTemplates()).first()!

describe('modify guild template', () => {
Expand Down
18 changes: 9 additions & 9 deletions tests/matchers.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {RESTJSONErrorCodes} from 'discord-api-types/v9'
import * as D from 'discord.js'
import * as Discord from 'discord.js'
import {getMatchers} from 'expect/build/jestMatchersObject'
import type {SyncExpectationResult} from 'expect/build/types'

Expand All @@ -18,8 +18,8 @@ declare global {
: never
): R
toEqualBitfield(
...args: T extends D.BitField<infer S, infer N> | undefined
? [bits: D.BitFieldResolvable<S, N>]
...args: T extends Discord.BitField<infer S, infer N> | undefined
? [bits: Discord.BitFieldResolvable<S, N>]
: IncompatibleExpectType
): R
toThrowAPIError(
Expand Down Expand Up @@ -75,7 +75,7 @@ Received: resolved with ${utils.printReceived(result)}`
const instanceOfResult = toBeInstanceOf.call(
context,
error,
D.DiscordAPIError
Discord.DiscordAPIError
)
if (
(!instanceOfResult.pass && !isNot) ||
Expand All @@ -88,7 +88,7 @@ ${removeMatcherHint(instanceOfResult)}`
}
}

const matchObjectInput: Partial<D.DiscordAPIError> = {code}
const matchObjectInput: Partial<Discord.DiscordAPIError> = {code}
const matchObjectResult = toMatchObject.call(
context,
error,
Expand Down Expand Up @@ -169,15 +169,15 @@ Received: ${utils.printReceived(received)}`

toEqualBitfield<S extends string, N extends bigint | number>(
this: jest.MatcherContext,
received: D.BitField<S, N> | undefined,
bits: D.BitFieldResolvable<S, N>
received: Discord.BitField<S, N> | undefined,
bits: Discord.BitFieldResolvable<S, N>
): jest.CustomMatcherResult {
const {isNot, promise, utils} = this
const matcherHint = (): string =>
`${utils.matcherHint('toEqualBitfield', 'received', 'expected', {
isNot,
promise,
...(received instanceof D.Permissions
...(received instanceof Discord.Permissions
? {comment: 'Not checking admin'}
: {})
})}
Expand All @@ -188,7 +188,7 @@ Received: ${utils.printReceived(received)}`
pass: false,
message: (): string => `${matcherHint()}
Expected: ${utils.printExpected(bits)}${
bits instanceof D.BitField
bits instanceof Discord.BitField
? ` (${utils.printExpected(bits.toArray())})`
: ''
}
Expand Down
8 changes: 4 additions & 4 deletions tests/oauth2.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as D from 'discord.js'
import * as Discord from 'discord.js'
import * as DM from '../src'
import {expectNotToBeNull, withClient, withClientF} from './utils'
import './matchers'
Expand All @@ -21,10 +21,10 @@ describe('fetchApplication', () => {
async client => {
expectNotToBeNull(client.application)
const application = await client.application.fetch()
expect(application).toBeInstanceOf(D.ClientApplication)
expect(application).toBeInstanceOf(Discord.ClientApplication)
expect(application.name).toBe(name)
expect(application.owner).toBeInstanceOf(D.User)
expect((application.owner as D.User).username).toBe(ownerUsername)
expect(application.owner).toBeInstanceOf(Discord.User)
expect((application.owner as Discord.User).username).toBe(ownerUsername)
},
{
backend: new DM.Backend({
Expand Down
12 changes: 6 additions & 6 deletions tests/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as D from 'discord.js'
import * as Discord from 'discord.js'
import * as DM from '../src'
import type {AnyFunction} from '../src/utils'

Expand Down Expand Up @@ -31,10 +31,10 @@ export type DeepPartialOmit<

// Omitting valueOf because ({...}).valueOf() is Object, whereas
// (guild as D.Guild).valueOf() is string
export type MatchObjectGuild = DeepPartialOmit<D.Guild, 'valueOf'>
export type MatchObjectGuild = DeepPartialOmit<Discord.Guild, 'valueOf'>

export interface WithClientOptions {
intents?: D.ClientOptions['intents']
intents?: Discord.ClientOptions['intents']
backend?: DM.Backend
applicationId?: DM.Snowflake
}
Expand All @@ -57,7 +57,7 @@ export const guildWithBot: (
return {backend, applicationId: app.id}
}

const defaultIntents = new D.Intents([
const defaultIntents = new Discord.Intents([
'GUILDS',
'GUILD_MESSAGES',
'GUILD_MESSAGE_REACTIONS',
Expand All @@ -66,11 +66,11 @@ const defaultIntents = new D.Intents([
])

export const withClient = <T>(
fn: (client: D.Client) => T,
fn: (client: Discord.Client) => T,
{intents = defaultIntents, backend, applicationId}: WithClientOptions = {}
): T => fn(new DM.Client({intents}, backend, applicationId))

export const withClientF =
<T>(fn: (client: D.Client) => T, options?: WithClientOptions) =>
<T>(fn: (client: Discord.Client) => T, options?: WithClientOptions) =>
(): T =>
withClient(fn, options)
6 changes: 3 additions & 3 deletions tests/voice.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as D from 'discord.js'
import * as Discord from 'discord.js'
import * as DM from '../src'
import {withClientF} from './utils'

Expand All @@ -8,7 +8,7 @@ describe('fetchVoiceRegions', () => {
withClientF(async client => {
const voiceRegions = await client.fetchVoiceRegions()
for (const [, region] of voiceRegions)
expect(region).toBeInstanceOf(D.VoiceRegion)
expect(region).toBeInstanceOf(Discord.VoiceRegion)
})
)

Expand All @@ -18,7 +18,7 @@ describe('fetchVoiceRegions', () => {
async client => {
const voiceRegions = await client.fetchVoiceRegions()
for (const [, region] of voiceRegions)
expect(region).toBeInstanceOf(D.VoiceRegion)
expect(region).toBeInstanceOf(Discord.VoiceRegion)
expect(voiceRegions.get('id1')?.name).toBe('name 1')
expect(voiceRegions.get('id2')?.name).toBe('name 2')
},
Expand Down
6 changes: 3 additions & 3 deletions types/WebSocketShard.d.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import type * as D from 'discord.js'
import type * as Discord from 'discord.js'

// constructor is private
declare const WebSocketShard: new (
manager: D.WebSocketManager,
manager: Discord.WebSocketManager,
id: number
) => D.WebSocketShard
) => Discord.WebSocketShard
export = WebSocketShard