message.guild.channelCreate is not a function - node.js

I feel ridiculous asking for help on this so in advance I apologize, but I got this error in my code when trying to create a channel. Here is my code:
if (message.author.bot) return;
msg = message.content.toLowerCase();
if (msg.startsWith("create channel")) {
const args = message.content.split(' ').slice(2).join(' ')
message.guild.createChannel(`${args}`).then(channel => {
channel.setTopic('Test')
})
}

It appears that you are trying to use the wrong function in v12. Guild#channels is a GuildChannelManager in v12 instead of a Collection.
Replace message.guild.createChannel (v11) with message.guild.channels.create (v12). Note that you can also set the topic in the same API call by passing options to GuildChannelManager#create() like so...
message.guild.channels.create(args.join(" "), { topic: "Test" })
See this guide on updating your code from v11 to v12.

Related

Collecting every guild and members returns "this.member.get" is not a function

So I have this discord bot and since I'm personally hosting it from my pc with nodejs I wanted to make the bot update the members list on my webapi everytime I boot it back up.
Problem is that Using the code below the console returns an error saying the member.get is not a function.
var timestamp=Date.now(), json={timestamp:timestamp, guilds:[]}, count=0;
try{
client.guilds.forEach(guild => {
json.guilds.push(guild);
var list = guild.members;
json.guilds[count].members=[];
list.forEach(member => json.guilds[count].members.push(member.user.id));
count++;
});
} catch(e) {
console.log(e);
}
The error:
TypeError: this.members.get is not a function
Anyone can help?
Thanks in advance.
Since Discord.js v12 you need to use the cache:
So instead of...
guild.members.get();
...you have to do this:
guild.members.cache.get();

Discord JS - TypeError: Cannot read property 'setChannel' of undefined

I'm trying to use multiple user mentions and message.mention.members.first() cannot do this. So I did some digging and found this function from Parsing Mention Arguments:
function getUserFromMention(mention) {
if (!mention) return;
if (mention.startsWith('<#') && mention.endsWith('>')) {
mention = mention.slice(2, -1);
if (mention.startsWith('!')) {
mention = mention.slice(1);
}
return client.users.cache.get(mention);
}
}
When I try to use this function I get the "Discord JS - TypeError: Cannot read property 'setChannel' of undefined" here is the code causing the error
let channel = client.channels.cache.find(channel => channel.name === args[0]);
const user1 = getUserFromMention(args[1]);
const user2 = getUserFromMention(args[2]);
message.member.voice.setChannel(channel.id);
user1.voice.setChannel(channel.id);
user2.voice.setChannel(channel.id);
This code Is meant to move Myself and mentioned users to selected Voice Channel it works perfectly fine when using message.mention.members.first() but can only handle one out of two of the mentioned users.
I was wondering if there was a fix for my current error or if there is another way I should be working this out?
Remember that,
message.mentions.members- Returns a GuildMember whereas
client.users.cache.get - Returns a User
You can only move/disconnect GuildMembers across VCs.
Therefore you can use message.mentions.members, which returns a Collection of all mentioned Users.
let channel = client.channels.cache.find(channel => channel.name === args[0]);
message.member.voice.setChannel(channel.id);
message.mentions.members.each(u => {
u.voice.setChannel(channel.id);
})
The problem can be, that the user is not in cache, so you must use GuildMemberManager#fetch(id). Problem is, that this function is async. The easiest solution is to make getUserFromMention and the functions where you use it async and use await.

Firebase Cloud Functions check is snapshot.exists() error

When I try to run a function
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();
exports.checkPostsRef = functions.https.onRequest((request, response) => {
const postId = 'foo'
admin.database().ref('/posts/' + postId).once('value', snapshot => {
if !snapshot.exists() {
console.log("+++++++++ post does not exist +++++++++") // I want this to print
return
}
});
});
I keep getting an error of Parsing error: Unexpected token snapshot:
Once I comment out if snapshot.exists() { .... } everything works fine.
I'm following this link that says there is an .exists() function, so why am I having this issue?
Good to see how you got it working Lance. Your conclusion on the return being the cause is wrong though, so I'll explain the actual cause below.
The problem is in this code:
if !snapshot.exists() ...
In JavaScript you must have parenthesis around the complete condition of an if statement. So the correct syntax is:
if (!snapshot.exists()) ...
In Swift those outer parenthesis are optional, but in JavaScript (and al other C based languages that I know of), they are required.
turns out it was the return; statement that was causing the problem. I had to use an if-else statement instead.
EDIT As #FrankvanPuffelen pointed out in the comments below the question and his answer, this issue wasn't about the return statement, it was about the way i initially had the !snapshot.exists(). Because it wasn't wrapped in parentheses (!snapshot.exists()) which was causing the problem. So it wasn't the return statement, I know very little Javascript and used the wrong syntax.
if (!snapshot.exists()) {
console.log("+++++++++ post does not exist +++++++++");
} else {
console.log("--------- post exists ---------");
}
FYI I'm a native Swift developer and in Swift you don't need to wrap anything in parentheses. In Swift you can do this:
let ref = Database.database().reference().child("post").child("foo")
ref.observeSingleEvent(of: .value, with: { (snapshot) in
if !snapshot.exists() {
print("+++++++++ post does not exist +++++++++")
return
}
})

Send is not a function discord.js v12

I have a problem with version 12 of discord JavaScript.
I get this error but I changed the find function.
What else can he have?
upBot.send is not a function
let upBot = bot.channels.cache.find(ch => ch.id = "");
let upBotEmbed = new Discord.MessageEmbed()
.setColor(colours.red_light)
.addField(`${bot.user.username} online`, datetime)
.setFooter(`Test`, bot.user.displayAvatarURL);
upBot.send(upBotEmbed).then(m => m.delete(900000))
The reason you are getting this error is because you are not using the delete timeout correctly.
Change
.delete(900000)
to
.delete({ timeout: 900000 })
The delete property takes an object in version v12.
find function should returns a list of object. Try this:
...
for(const upBot of upBots) {
upBot.send(upBotEmbed).then(m => m.delete(900000))
}

"array.forEach is not a fuction" after firebase deploy but works on firebase serve

After deployment my array does not seem to be defined and I get a "forEach is not a function" error:
textPayload: "TypeError: currencies.forEach is not a function
at currenciesMenuConfiguration (/srv/menu/currencies-menu.js:22:16)
Here's the code for the currencies array:
async function currenciesMenuConfiguration() {
currencies = await getCol("currencies")
currencies.forEach(element => {
return currenciesList[element[Object.keys(element)].id] = element[Object.keys(element)].current
});
}
This gets called right after being defined with currenciesMenuConfiguration()
getCol is defined as:
// Get all documents in collection from Firestore
async function getCol(colName) {
return fs.collection(colName).get()
.then(res => {
return res.docs.map(doc => {
var rObj = {};
rObj[doc.id] = doc.data();
return rObj;
})
})
.catch(err => {
return `Error getting collection: ${err}`;
});
}
As mentioned on firebase serve locally this works without an issue. After deployment, I get this error. Am I missing something about asynchronous code in the cloud?
Also, any tips on how I would go about debugging this myself would be much appreciated. #BeginngersLife.
// I have checked the questions on so about firebase serve vs deployment but they mostly deal with issues around deployment or don't address the issue I am facing. One question that might help me get further in this is: firebase serve and debugging functions?.
See comment below question: I did indeed not return an array but an object. Closing. Thanks for the hint #Frank van Puffelen.

Resources