I want to add users with specific role to threads. My code is:
bot.on('message', async message => {
if (message.content == '?threadref') {
let list = message.guild.roles.cache.get('948258030192058410').members.map(m=>m.user.id);
for (var i = 1; i < list.length; i++) {
const user = list[i-1];
const thread1 = channel.threads.cache.find(x => x.id === '945763756728787006');
const thread2 = channel.threads.cache.find(x => x.id === '947100498035638283');
const thread3 = channel.threads.cache.find(x => x.id === '947100722321846272');
const thread4 = channel.threads.cache.find(x => x.id === '945763910756204585');
const thread5 = channel.threads.cache.find(x => x.id === '946785089486987305');
const thread6 = channel.threads.cache.find(x => x.id === '946784996537012254');
const thread7 = channel.threads.cache.find(x => x.id === '946784810981023754');
await thread1.members.add(user);
await thread2.members.add(user);
await thread3.members.add(user);
await thread4.members.add(user);
await thread5.members.add(user);
await thread6.members.add(user);
await thread7.members.add(user);
console.log(`${user} was added to threads`);
}
}
});
But when i try to use this code i get error:
(node:453) UnhandledPromiseRejectionWarning: ReferenceError: channel is not defined
(node:453) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:453) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
Can someone tell how get channel variable. I get code from https://discordjs.guide/popular-topics/threads.html#adding-and-removing-members and there i can't find what channel mean.
You need to access the channel property from your message object, say:
bot.on('messageCreate', async (message) => {
let { channel } = message;
if (message.content == '?threadref') {
...
}
});
/*
* Alternatively you can use message.channel
directly on each time you a define thread.
* Welcome to stackoverflow! 😄
*/