So I made a bot that will send welcome messages in a channel to new members.
My code is supposed to work because it didn't send any errors in my console, but my bot didn't send the welcome message.
I tried many things:
Made the embed an object (nope)
Made 4 long different codes (all not working)
Searched the web and watched some tuts (no error in the console but not sending)
I'm using discord.js#v12
Code:
client.on('guildMemberAdd', member => {
// Finds the channel name from the array
function channelNamesFilter(channel) {
let channelNames = ['name', 'welcome', 'welcoming', 'greeting', 'general', 'hello'];
if (channelNames.includes(channel.name)) {
return true;
}
return false;
}
const welcome = new Discord.MessageEmbed()
.setColor('#F2CC0D')
.setTitle('Welcome To The Server!')
.addFields({
name: member.nickname
}, {
name: '\n ',
value: 'Make sure to visit the FAQ and Rules channel (if there are)!'
})
.setImage(member.user.avatarURL)
let filteredChannels = member.guild.channels.cache.filter(channelNamesFilter);
filteredChannels.forEach(element => element.send(welcome));
});
Ok I just solved it after like researching for more than 10 hours! I finally got it!
client.on('guildMemberAdd', member =>{
const channel = member.guild.channels.cache.find(channel => channel.name.includes('welcome'));
if (!channel) return;
//send through channel
const welcomegreet = {
color: 0xF2CC0D,
title: `**WELCOME TO THE SERVER, ${member.user.tag} !!**`,
description: `I hope you will enjoy it here! \n A place full of chaos and wonder!`,
thumbnail: {
url: member.user.avatarURL(),
},
fields:[
{
name: 'Need Help?',
value: 'Please read the FAQ and Rules Channels (if there are)! \n Have Fun at the Server! CHEERS 🥂 !',
inline: false
},
{
name: 'Join Me!',
value: 'Do: `h.help` for entertainment and profanities!',
inline: false
}
],
timestamp: new Date(),
};
//send through dm
const welcome = {
color: 0xF2CC0D,
title: `**WELCOME TO THE SERVER, ${member.user.tag} !!**`,
description: `I hope you will enjoy it here! \n A place full of chaos and wonder!`,
thumbnail: {
url: member.user.avatarURL(),
},
timestamp: new Date(),
};
member.send({ embed: welcomegreet });
channel.send({ embed: welcome });
Thanks guys for trying to help me, even though It didn't get too close to the amswer.
I tested this out and it worked splendidly!
note:
This only works if the channel has the word "welcome"
Related
I wanted to ask how to create channels in discord.js 14 (i tried to researched but found nothing) I want to make a simple text channel! :)) Also, with the stuff i found, it worked ( no errors) it just didnt create anything / post errors in debug log
It's literally under Discord.js documentation. Please, next time before asking a question read the docs.
https://discord.js.org/#/docs/discord.js/main/general/welcome
guild.channels.create({
name: "hello",
type: ChannelType.GuildText,
parent: cat[0].ID,
// your permission overwrites or other options here
});
//If the command is slash type
const { Permissions } = require("discord.js")
interaction.guild.channels.create({
name: "new-channel",
type: 'GUILD_TEXT',
permissionOverwrites: [
{
id: interaction.guild.id,
accept: [Permissions.FLAGS.VIEW_CHANNEL],
},
],
});
//If the command is message type
const { Permissions } = require("discord.js")
message.guild.channels.create({
name: "new-channel",
type: 'GUILD_TEXT',
permissionOverwrites: [
{
id: message.guild.id,
accept: [Permissions.FLAGS.VIEW_CHANNEL],
},
],
});
I am currently having an issue with my /help command, the way my /help command works is it sends a nice fancy embed with a selectmenu that you can select different pages. That all works fine the issue comes when if I were to do /help and get the embed then do /help again and interact with the second embed it will crash and give the error "Interaction has already been acknowledged" Here is my code.
const generalHelp = { // Creates generalHelp embed.
color: 0x901ab6,
title: 'join our support server!',
url: 'https://discord.gg/MUwJ85wpKP',
author: {
name: 'Help Menu',
icon_url: 'https://cdn.discordapp.com/attachments/937276227692150815/937552170520301588/Letter_Z.png',
},
description: 'Select an option to view the commands I have!',
fields: [
{
name: ':tada: Fun Commands',
value: 'Shows all the bots varying fun commands in a nice little page for easy viewing.',
inline: true,
},
{
name: ':tools: Admin Commands',
value: 'Shows all the bots varying admin commands in a nice little page for easy viewing.',
inline: true,
},
/*{
name: '\u200b',
value: ' \u200b ',
inline: false,
},*/
],
}
const adminHelp = { // Creates moderationHelp embed.
color: 0x901ab6,
author: {
name: 'Help Menu',
icon_url: 'https://cdn.discordapp.com/attachments/937276227692150815/937552170520301588/Letter_Z.png',
},
description: 'Here are the commands!',
fields: [
{
name: 'Prefix: `/`',
value: '\u200b',
},
{
name: ':tools: Admin Commands',
value: '`toggle`, `settings`'
},
]
}
const funHelp = { // Creates funHelp embed.
color: 0x901ab6,
author: {
name: 'Help Menu',
icon_url: 'https://cdn.discordapp.com/attachments/937276227692150815/937552170520301588/Letter_Z.png',
},
description: 'Here are the commands!',
fields: [
{
name: 'Prefix: `/`',
value: '\u200b',
},
{
name: ':tada: Fun Commands',
value: '`ping`, `poll`',
},
]
}
const row = new MessageActionRow() // Creates MessageActionRow with name row.
.addComponents(
new MessageSelectMenu()
.setCustomId('select')
.setPlaceholder('Select an option')
.addOptions([
{
label: '🎉 Fun Commands',
description: '',
value: 'first_option',
},
{
label: '🔨 Admin Commands',
description: '',
value: 'second_option',
},
])
)
await interaction.reply({ embeds: [generalHelp], components: [row]}) // Displays general help embed
And here is my code that handles the interactions.
interaction.client.on('interactionCreate', interaction => { // Detects which value is selected.
if(!interaction.isSelectMenu()) return
if (interaction.values[0] === 'first_option') // Checks if values[0] is = to first_option to display gameHelp embed.
{
interaction.update({embeds: [funHelp]}) // Updates bots interaction embed
}
if (interaction.values[0] === 'second_option') // Checks if values[0] is = to second_option to display musicHelp embed.
{
interaction.update({embeds: [adminHelp]}) // Updates bots interaction embed
}
else // If values[0] is anything else display error message to console. (PM2 will auto restart bot if error happens.)
{
return//console.log('Help Menu Select Menu Error!') // Logging to PM2 console.
}
})
If someone could help me fix it that would be great <3
Its because you already .reply()-ed to the interaction, so the interaction is already acknowledged with that.
To solve this you can use .editReply():
interaction.reply({ content: 'This is my first reply!' });
interaction.editReply({ content: 'This my edited second reply!' });
For anyone still looking for an answer the reason this happens is because the first time it works 100% fine because there is no update and is a fresh interaction. However when you try and update it which also counts as an interaction. Putting your interactionCreate code into an event handler solves this issue.
module.exports = {
name: 'interactionCreate',
execute(interaction) {
//console.log(`${interaction.user.tag} in #${interaction.channel.name} triggered an interaction.`);
if (!interaction.isSelectMenu() && interaction.isCommand()) return
if (interaction.customId !== 'select') return
switch (interaction.values[0]) {
case 'first_option':
interaction.update({embeds: [funHelp], ephemeral: true})
break
case 'second_option':
interaction.update({embeds: [adminHelp], ephemeral: true})
break
default:
return
}
},
};
So I'm working on a bot right now and when looking at the api I get this as the example of how it'll work.
(async () => {
console.log(await bookwebsite.getHomepage(1))
})()
{
results: [ { bookId: 'web id',
thumbnail: 'thumbnail link',
title: 'book title' },
{ bookId: 'web id',
thumbnail: 'thumbnail link',
title: 'book title' },
{ bookId: 'web id',
thumbnail: 'thumbnail link',
title: 'book title' },
...
],
}
Can anyone lead me in the right direction on how to translate this from a console log script to running it within discord embeds? API WARNING NSFW
I'm not extremely sure what you're meaning as of translating console into embeds but I'm guessing you're trying to format the data returned in the api in to a embed in Discord.
const bookwebsite = require('nhentai-js');
(async () => {
var data = await bookwebsite.getHomepage(1);
data = data.results.slice(0, 25);
if(!data) return message.channel.send('Failed to retrieve data from api. ')
// slices to avoid discord embeds going beyond 25 fields
var embed = new Discord.MessageEmbed()
.setTitle('Results found');
// For each the data that's received(after its trimmed)
data.forEach(d=>{
// For every object inside of data, it takes the name and sets it as a field title and sets the description of the field as the id and thumbnail link.
embed.addField(`Name: ${d.title}`, `ID: ${d.bookId}\nThumbnail: ${d.thumbnail}`)
})
// Embeds done, now need to send into the channel
message.channel.send(embed)
})()
If you need any help further on, please comment below.
I am making a Discord JS bot which is supposed to have a mute function that assigns a role to a member so they can't text.
I tried looking all over the web for how to create a role (even the Discord JS documentation) but to no avail.
I've tried the code below but it doesn't work (pulled straight from https://discord.js.org/#/docs/main/stable/class/RoleManager?scrollTo=create).
guild.roles.create({
data: {
name: 'Super Cool People',
color: 'BLUE',
},
reason: 'we needed a role for Super Cool People',
})
.then(console.log)
.catch(console.error);
Thanks in advance!
I'm pretty sure that guild is not defined within your code. roles is a property of Guild, so you need a Guild class to access RoleManager and create a Role.
If your code is executed within a command, you can use message.guild to get the Guild, otherwise, you'll need to get the Guild manually.
Here's a simple example of how to use it:
First Scenario
client.on("message", message => {
if (message.author.bot) return false;
if (message.author.id !== message.guild.ownerID) return false;
message.guild.roles.create({
data: {
name: "Muted",
permissions: [],
color: "RED"
},
reason: "Created the mute role."
}).catch(console.log)
});
Second Scenario
const Guild = client.guilds.cache.get("1234567890123456789");
Guild.roles.create({
data: {
name: "Muted",
permissions: [],
color: "RED"
},
reason: "Created the mute role."
}).catch(console.log)
as long as you have a role manager you should have the create function according to the docs
so my advice to fix this issue is see where it goes wrong. I've done it with the createGuild event in typeScript. this will create the roles when the bot joins a new guild.
client.on('guildCreate', async guild => {
await guild.roles.create({ data: { name: 'roleName' } });
});
also be aware that you need high enough permissions to actually create a role.
ps: providing errors when you have an error would be useful (the full error)
If you will try to do guild.create.roles the console will give you an error with message: guild <= is not defined!
You need to write message.guild.create.roles not the guild.create.roles, after this, you will get the created role.
Example code creating the role, with the "debug messages":
message.channel.send('creating a role')
message.guild.roles.create({
data: {
name: 'Testing Role',
color: 'GREY'
},
reason: 'Stackoverflow.com - created for user14470589'
})
.then((res => {
message.channel.send(`debug result:\n${res}`)
})).catch((err => {
message.channel.send(`error:\n${err}`)
}))
I am currently trying to add a custom status to my discord.js bot to request new developers, and it is not working. Here is my code:
client.on('ready', () => {
console.log(`Ready to comply.`);
client.user.setPresence({
status: 'online',
game: {
name:
'need developers, DM Daniel for more info, BTW, you can learn the language at discord.js.org.', //The message shown
type: 'PLAYING',
},
});
});
I have tried searching Stack Overflow, and found an answer that tells me to use the code above, but it's not working.
If you are referring to a custom status, which one of the recent Discord updates have introduced, it is not possible.
If you mean something like 'Playing with 100 commands' you can use:
client.user.setPresence({ activity: { name: 'with discord.js' }, status: 'idle' })
If you use discord.js v12 then try this code, it works perfectly for me:
client.user.setPresence({
status: 'online',
activity: {
type: 'PLAYING',
name: 'need developers, DM Daniel for more info',
},
});