Skip to content

Commit f2bbad3

Browse files
feat(GuildManager): add AFK and system channel options in create (#4837)
This commit adds support for the `afk_channel_id`, `afk_timeout`, and `system_channel_id` parameters in the [create guild](https://discord.com/developers/docs/resources/guild#create-guild-json-params) endpoint by adding the `afkChannelID`, `afkTimeout`, and `systemChannelID` options in `GuildManager#create`. This commit also fixes a minor bug in `create` when specifying types for the channels due to the channel types not being changed from `'text'`, `'voice'` etc to the corresponding numbers, so Discord would return an error.
1 parent 77c0788 commit f2bbad3

File tree

3 files changed

+57
-5
lines changed

3 files changed

+57
-5
lines changed

src/managers/GuildManager.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ const GuildMember = require('../structures/GuildMember');
88
const Invite = require('../structures/Invite');
99
const Role = require('../structures/Role');
1010
const {
11+
ChannelTypes,
1112
Events,
1213
VerificationLevels,
1314
DefaultMessageNotifications,
@@ -129,6 +130,8 @@ class GuildManager extends BaseManager {
129130
* <warn>This is only available to bots in fewer than 10 guilds.</warn>
130131
* @param {string} name The name of the guild
131132
* @param {Object} [options] Options for the creating
133+
* @param {number} [options.afkChannelID] The ID of the AFK channel
134+
* @param {number} [options.afkTimeout] The AFK timeout in seconds
132135
* @param {PartialChannelData[]} [options.channels] The channels for this guild
133136
* @param {DefaultMessageNotifications} [options.defaultMessageNotifications] The default message notifications
134137
* for the guild
@@ -137,18 +140,22 @@ class GuildManager extends BaseManager {
137140
* @param {string} [options.region] The region for the server, defaults to the closest one available
138141
* @param {PartialRoleData[]} [options.roles] The roles for this guild,
139142
* the first element of this array is used to change properties of the guild's everyone role.
143+
* @param {number} [options.systemChannelID] The ID of the system channel
140144
* @param {VerificationLevel} [options.verificationLevel] The verification level for the guild
141145
* @returns {Promise<Guild>} The guild that was created
142146
*/
143147
async create(
144148
name,
145149
{
150+
afkChannelID,
151+
afkTimeout,
146152
channels = [],
147153
defaultMessageNotifications,
148154
explicitContentFilter,
149155
icon = null,
150156
region,
151157
roles = [],
158+
systemChannelID,
152159
verificationLevel,
153160
} = {},
154161
) {
@@ -163,6 +170,7 @@ class GuildManager extends BaseManager {
163170
explicitContentFilter = ExplicitContentFilterLevels.indexOf(explicitContentFilter);
164171
}
165172
for (const channel of channels) {
173+
if (channel.type) channel.type = ChannelTypes[channel.type.toUpperCase()];
166174
channel.parent_id = channel.parentID;
167175
delete channel.parentID;
168176
if (!channel.permissionOverwrites) continue;
@@ -187,8 +195,11 @@ class GuildManager extends BaseManager {
187195
verification_level: verificationLevel,
188196
default_message_notifications: defaultMessageNotifications,
189197
explicit_content_filter: explicitContentFilter,
190-
channels,
191198
roles,
199+
channels,
200+
afk_channel_id: afkChannelID,
201+
afk_timeout: afkTimeout,
202+
system_channel_id: systemChannelID,
192203
},
193204
})
194205
.then(data => {

test/createGuild.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
'use strict';
2+
3+
const assert = require('assert');
4+
const { token } = require('./auth');
5+
const { Client } = require('../src');
6+
7+
const client = new Client();
8+
9+
client.on('ready', async () => {
10+
try {
11+
const guild = await client.guilds.create('testing', {
12+
channels: [
13+
{ name: 'afk channel', type: 'voice', id: 0 },
14+
{ name: 'system-channel', id: 1 },
15+
],
16+
afkChannelID: 0,
17+
afkTimeout: 60,
18+
systemChannelID: 1,
19+
});
20+
console.log(guild.id);
21+
assert.strictEqual(guild.afkChannel.name, 'afk channel');
22+
assert.strictEqual(guild.afkTimeout, 60);
23+
assert.strictEqual(guild.systemChannel.name, 'system-channel');
24+
await guild.delete();
25+
client.destroy();
26+
} catch (error) {
27+
console.error(error);
28+
}
29+
});
30+
31+
client.login(token).catch(console.error);

typings/index.d.ts

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1941,10 +1941,7 @@ declare module 'discord.js' {
19411941

19421942
export class GuildManager extends BaseManager<Snowflake, Guild, GuildResolvable> {
19431943
constructor(client: Client, iterable?: Iterable<any>);
1944-
public create(
1945-
name: string,
1946-
options?: { region?: string; icon: BufferResolvable | Base64Resolvable | null },
1947-
): Promise<Guild>;
1944+
public create(name: string, options?: GuildCreateOptions): Promise<Guild>;
19481945
public fetch(id: Snowflake, cache?: boolean, force?: boolean): Promise<Guild>;
19491946
}
19501947

@@ -2577,6 +2574,19 @@ declare module 'discord.js' {
25772574
name?: string;
25782575
}
25792576

2577+
interface GuildCreateOptions {
2578+
afkChannelID?: number;
2579+
afkTimeout?: number;
2580+
channels?: PartialChannelData[];
2581+
defaultMessageNotifications?: DefaultMessageNotifications | number;
2582+
explicitContentFilter?: ExplicitContentFilterLevel | number;
2583+
icon?: BufferResolvable | Base64Resolvable | null;
2584+
region?: string;
2585+
roles?: PartialRoleData[];
2586+
systemChannelID?: number;
2587+
verificationLevel?: VerificationLevel | number;
2588+
}
2589+
25802590
interface GuildWidget {
25812591
enabled: boolean;
25822592
channel: GuildChannel | null;

0 commit comments

Comments
 (0)