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

fix(DataResolver): fix circular dependency error with GuildTemplate #5622

Merged
merged 4 commits into from May 22, 2021
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/util/DataResolver.js
Expand Up @@ -5,7 +5,6 @@ const path = require('path');
const stream = require('stream');
const fetch = require('node-fetch');
const { Error: DiscordError, TypeError } = require('../errors');
const GuildTemplate = require('../structures/GuildTemplate');
const Invite = require('../structures/Invite');

/**
Expand Down Expand Up @@ -56,6 +55,9 @@ class DataResolver {
* @returns {string}
*/
static resolveGuildTemplateCode(data) {
// Circular dependency
// https://github.com/discordjs/discord.js/issues/5600
cherryblossom000 marked this conversation as resolved.
Show resolved Hide resolved
const GuildTemplate = require('../structures/GuildTemplate');
return this.resolveCode(data, GuildTemplate.GUILD_TEMPLATES_PATTERN);
}

Expand Down
3 changes: 2 additions & 1 deletion test/escapeMarkdown.test.js
@@ -1,6 +1,7 @@
'use strict';

/* eslint-disable max-len, no-undef */
/* eslint-env jest */
/* eslint-disable max-len */
Comment on lines -3 to +4
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I noticed that jest is listed as a dev dependency in package.json, so I enabled the jest ESLint environment so that no-undef can still be enabled without spewing warnings about describe, test, and expect being undefined.


const Util = require('../src/util/Util');
const testString = "`_Behold!_`\n||___~~***```js\n`use strict`;\nrequire('discord.js');```***~~___||";
Expand Down
11 changes: 11 additions & 0 deletions test/resolveGuildTemplateCode.test.js
@@ -0,0 +1,11 @@
'use strict';

/* eslint-env jest */

const { DataResolver } = require('../src');

describe('resolveGuildTemplateCode', () => {
test('basic', () => {
expect(DataResolver.resolveGuildTemplateCode('https://discord.new/abc')).toBe('abc');
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wasn't sure if this test is really necessary, but because I technically changed the code in resolveGuildTemplateCode, I added this just in case. I'm happy to remove it if this is unnecessary.

There don't seem to be many existing tests, so I just copied what was in test/escapeMarkdown.test.js.

});
});
27 changes: 27 additions & 0 deletions test/templateCreateGuild.js
@@ -0,0 +1,27 @@
'use strict';

const { token } = require('./auth');
const { Client } = require('../src');

const client = new Client({ intents: ['GUILDS', 'GUILD_MESSAGES'] });
client
.on('ready', () => console.log('ready'))
.on('message', async message => {
try {
const templates = await message.guild.fetchTemplates();
if (!templates.size) {
console.log('no templates');
} else {
const guild = await templates.first().createGuild('guild name');
console.log(`created guild with ID ${guild.id}`);
await guild.delete();
console.log('deleted guild');
}
} catch (error) {
console.error(error);
} finally {
client.destroy();
}
})
.login(token)
.catch(console.error);