Can't send embed message on join - javascript

No idea why it won't work, the exact code worked on my old bot. Code:
client.on("guildMemberAdd", member => {
const Discord = require("discord.js");
const embed = new Discord.RichEmbed()
.setTitle("**Please be sure to read our rules carefully thanks**")
.setAuthor("Welcome to BACKUP")
.setColor(3447003)
.setDescription("Please enjoy your stay")
.setThumbnail(message.author.avatarURL)
client.channels.get('505107608391254030').send({embed});
})
}
The thing that confuses me most, is that if I replace that code with this code, it works fine.
client.on('guildMemberAdd', member => {
member.guild.channels.get('505107608391254030').send("This works, but embed does not, fix it boi, line 102");
});
(On the code that did not work, I tried: client.channels.get, member.channels.get, member.guild.channels.get, client.guild.channels.get

The problem is when you are finding the channel, client.channels.get isn't a method. doesn't work in this situation for reasons I'm not aware of
You have to use client.guilds.get(GUILD_ID).channels.get(CHANNEL_ID).send({embed});

Related

Name shows up as "undefined"

So, I have been working on a kick command, and I pretty much got the whole thing working, except when I have the bot send a message telling us who was kicked, it just shows up as "undefined was kicked"
client.on("message", message => {
if (message.content.startsWith('grimm!kick')) {
var member = message.mentions.members.first();
if (message.member.hasPermission('KICK_MEMBERS')) {
const kicked = new Discord.MessageEmbed()
.setColor('#ff6700')
.setDescription(`${message.mentions.members.first.username} Was kicked`)
message.channel.send(kicked)
member.kick()
} else {
const notKicked = new Discord.MessageEmbed()
.setColor('#ff6700')
.setDescription(`${message.author.username}! you do not have permission to kick people! you must have the "Kick Members" Permission to use this command`)
message.channel.send(notKicked)
}}});
I have tried "member.username" and that still wont work
Could someone tell me what exactly I'm doing wrong, so I wont make this same mistake again? Thanks!
There are 2 problems in this code.
You are using first as a property, not a method.
the property username doesnt exist on The GuildMember class.
what you are looking for is GuildMember.user.username.
replace
.setDescription(`${message.mentions.members.first.username} Was kicked`)
with
.setDescription(`${message.mentions.members.first().user.username} Was kicked`)

making a discord bot say the rules when aksed

hello I am trying to code a Discord bot, I am trying to make it so that if someone says $rules the rules will be sent, I can't send the code because I have a problem with posting something on this website, I have searched a lot on the internet but I can not find anything
edit: I have found out how I can post the code https://glitch.com/edit/#!/melted-messy-leotard
this is the link to see it
You have a syntax error in your code - you never close the listener with any brackets.
To fix this you can use this:
let Discord = require("discord.js");
let client = new Discord.Client();
client.on("message", message => {
if (message.content === "ping") {
message.channel.send("pong!")
}
if(message.content === "embed") {
let embed = new Discord.MessageEmbed()
.setTitle("this is Emded title")
.setDescription("this is Embed description")
.setColor("RANDOM")
.setFooter("This is embed footer")
message.channel.send(embed)
}
if (message.content.startswith("$rules")) {
let member = message.mentions.members.first()
if (!member) message.send("here are the rules, 1. no politics 2. be nice 3. respect others 4. think before you speak 5. no nsfw 6. don't leak personal info 7.don't argue abt opinions, 3 strikes and you're out")
}
});
client.login("token");
Also your line where you do if(!member) probably won't do what you want it to do - this will respond if it DMs the bot, if this is what you wanted then it is fine.
If you want to check if the member is in a guild, you can check if they have a common role (such as everyone) using this:
if (!member.roles.cache.some(role => role.name === '<role name>')) {
// member is not in guild
}

Why is guild.members.fetch timing out

Hello so I am writing a discord bot and all I want is an ordered list of members ordered by their ids. To achieve this I am running following code, however it just console.logs "Couldn't fetch members", with no further errors. :(
Upon further inspection, I discovered that it is a timeout error. Everything else works fine and I think that the const guild isn't the problem, but other than that hope that you people can help, Cheers!
const IDs = new Map();
var repeat = new Boolean(false);
var randomInt = new Number(0);
client.on('ready', () => {
console.log('Ready!');
const guild = client.guilds.cache.get("xxxxxxxxxxxxxxxxxx");
guild.members.fetch().then(members => {
console.log("Found the members");
IDs = members.map(user => user.id);
const OnlineMembers = members.filter(member => member.presence.status == "online");
}).catch(e => console.log("Couldn't fetch members."));
});
P.S. This is running on a Raspberry Pi over Nodemon --inspect, in case that changes anything
I found the solution, sorry if I bothered anybody, there is this really small setting in the Discord Dev Hub. That enables or disables this feature, obviously I was to dumb to turn it on from the start, thank you to anybody who took some time for this, Cheers!

Discord.js messageReactionAdd Not firing

I understand that messageReactionAdd only works with cached messages but even after manually caching them it still wont fire.
Currently my code is:
client.once('ready', () => {
console.log(config.readymesssage);
var channel = client.channels.resolve('761577724379791450');
channel.messages.fetch({limit: 90}).then((message) => {
console.log("done")
})
});
client.on('messageReactionAdd', async(reaction) => {
console.log("reacted");
});
I have tried using raw events I used the code block from Here and I have also tried using partials, neither of which seem to be working. I'm unsure as to what else I cant try.
Enabling intents has fixed the issue, they first needed turning on in the developer portal and then I needed to add the following lines,
let Intss = new Discord.Intents(Discord.Intents.ALL);
const client = new Discord.Client({ws: { intents: Intss }});
Now the event is firing as expected.
Another working solution that is most likely better is
const bot = new Discord.Client({ partials: ['USER', 'REACTION', 'MESSAGE'] });
Now, you will have to check for partials before doing anything with the data (and fetch the data if it is partial). But this way, you won't have to deal with intents and if your bot scales to 100+ servers, you won't have to wait for verification either.

connection.playStream is not a function

I know that there are a lot of question on this topic, but I browsed all of them but none of the answers seemed to fix my issue. I'm just creating a Discord bot for fun, I have almost never programmed in JavaScript but I wanted to try it.
My Code looks like this:
var url = "https://www.youtube.com/watch?v=HHv-s2OqYpw"
if(!message.member.voice.channel){
message.channel.send("Join a voice channel.");
return
} else {
vc = message.member.voice.channel;
connection = await vc.join();
isValid = ytdl.validateURL(url);
if(!isValid){
message.channel.send("The url you gave doesn't exist");
} else {
const stream = ytdl(url, {filter: "audioonly"});
const dispatcher = connection.playStream(stream)
dispatcher.on("end", function() {
vc.leave()
message.channel.send("Done playing the only music I can play lol")
})
I could do that it could play more songs but I only want it to play this one and in the terminal i get this error:
connection.playStream is not a function
I do have ytdl-core and opusscript installed and they both have the newest version. I tried doing this in lots of different ways, and it really annoys me that I can't figure out what the problem is. Sorry for bad english and thank those who help me. Have a nice day!
https://discord.js.org/#/docs/main/stable/class/VoiceConnection?scrollTo=play
As you can see, there's a VoiceConnection#play() method, and since you're most likely on v12, this is the method you're looking for.
const stream = ytdl(url, options);
const dispatcher = connection.play(stream);

Categories

Resources