Reputation: 29
I am trying to build a bot with the discord.js library in node.js that will create a new voice channel in a certain category when a user joins a certain channel. After the creation of the channel, I want the bot to then move the user to the new channel!
I am trying the following code:
var temporary = [];
client.on('voiceStateUpdate', async (oldMember, newMember) => {
const mainCatagory = '815281015207624704';
const mainChannel = '814938402137833484';
if (newMember.voiceChannelID == mainChannel) {
await newMember.guild
.createChannel(`📞 ┋ Support Room`, { type: 'voice', parent: mainCatagory })
.then(async (channel) => {
temporary.push({ newID: channel.id, guild: newMember.guild.id });
await newMember.setVoiceChannel(channel.id);
});
}
if (temporary.length >= 0)
for (let i = 0; i < temporary.length; i++) {
let ch = client.guilds
.find((x) => x.id === temporary[i].guild)
.channels.find((x) => x.id === temporary[i].newID);
if (ch.members.size <= 0) {
await ch.delete();
return temporary.splice(i, 1);
}
}
});
The code comes with no error but doesn't create the voice channel!
Upvotes: 0
Views: 292
Reputation: 8402
The problem is that you are using discord.js v12, but your code is made for v11.
Client#voiceStateUpdate
now passes VoiceStates
as parameters, not GuildMembers
. You should rename newMember
and oldMember
to newState
and oldState
respectively.
GuildMember#voiceChannelID
is a deprecated property, which is why you don't get any errors. Your code never actually gets past the if statement. It has turned into GuildMember#voice#channelID
(newState.channelID
).
Guild#createChannel
is also deprecated, being replaced with Guild#channels#create
. The function still looks and acts the same, so you only need to change the name (newState.guild.channels.create
).
GuildMember#setVoiceChannel
has turned into GuildMember#voice#setChannel
(newState.setChannel
)
Client#guilds#find
has turned into Client#guilds#cache#find
(client.guilds.cache.find
)
Guild#channels#find
has turned into Guild#channels#cache#find
(client.cache.find(...).channels.cache.find
)
(As a side note, always use Collection#get
instead of Collection#find
when searching by IDs. .find((value) => value.id === '...')
should always be converted to simply .get('...')
. This also applies to switching Collection#some
with Collection#has
)
Guild#members#size
has turned into Guild#members#cache#size
(ch.members.cache.size
)Every single one of these deprecations occurred as a result of discord.js switching to a Manager/caching system. For example, Client#guilds
now returns a GuildManager
instead of a collection.
More information about switching from v11 to v12 (including Managers)
Upvotes: 1