Right now I am working on a Discord bot and am attempting to list all users that are currently connected to the "general" voice channel.
My main issue right now is that my code is able to realize the number of people in the voice channel, but all of the "member" objects are undefined. This is both the console outputs and code. I have searched everywhere and can't seem to find anything.
This is the output from the console, the three "undefined" logs are the current users in the voice channel:
This is the code I have written:
For convience, this is also the code...
var chan = bot.channels['363589387411259396'];
var mems = chan.members;
for (var x in mems) {
console.log(x.userID);
}
return 'ANYTHING';
Any inputs helps, thanks!
Guild.members returns a Collection<Snowflake, GuildMember> object, where Snowflake is the GuildMember's ID.
Your main issue is this:userID property does not exist. What you should be looking for is GuildMember.id or simply printing out the Snowflake.
(Since it does not exist, printing out GuildMember.userID would result in printing out undefined)
Also, you can do your loop as what Venkata mentioned in the comments.
Related
When I’m trying to get the member list from my discord server, djsv12 returns me a list, but it has only 1 entry.
In fact, there should be 23 entries instead because that's the amount of members on my server.
I don't understand what's wrong. You can find my code below:
const Guild = client.guilds.cache.first(); // My one server
const Members = Guild.members.cache.map(member => member.id);
console.log(Members);
Console:
[ '578565698461106204' ]
Maybe it's because only one member is cached. Try to fetch all the members first.
members.fetch() fetches members from Discord, even if they're offline and returns promise that resolves to a collection of GuildMembers:
const guild = client.guilds.cache.first(); // My one server
const members = await guild.members.fetch();
const memberIDs = members.map((member) => member.id);
console.log(memberIDs);
PS: As I'm using the await keyword, make sure that you're in an async function.
I believe you're encountering the same problem as mentioned in this question.
Since Discord has been implementing privileged intents, that means that you have to grant your bot permission access to certain data. This privilege is turned off by default.
To turn it on, simply go to the Discord Developer Portal. Then select your Application, then go to the Bot section on the left side and scroll down. You should see the two options "Presence Intent" and "Server Members Intent".
Resources:
None of my discord.js guildmember events are emitting, my user caches are basically empty, and my functions are timing out?
Discord.js Guide - Gateway Intents
So, I am new to JavaScript. I am making a Discord bot because it's the only thing I would really use JavaScript for at the moment and I want to learn something. Whenever I try to show something like an id it says undefined.
client.on("channelCreate", function(channel){
console.log(channel.type+` channel, `+channel.name+`, was created by `+`${channel.owner_id}`+` in the category `+channel.parent_id);
});
I want to make a basic log section for my bot, and I am just starting. I want it to be like
00:00:00 CREATE > [Text / Voice] Channel was created by [Creator] under category [category]!
I know the exact format isn't implemented into the code, I test a lot of the function before I make it pretty lol. But it comes out like this (using the code I have now)
text channel, s, was created by undefined in the category undefined
I'm so confused because I like detail so I want to have who created it, when, and also the category! So how would I do that?
Also, would there be a way to send messages and stuff from the console?
Like if I typed this in the console
[channel or user] > [the message]
#testchannel > Announcing new video or something idk
and have it send to the channel? any help is appreciated!
edit: I forgot to mention that I have the owner_id part in ${var} format because I was testing to see if that would make it work, but I never switched it back lol
Changing the channel.parent.name would work but there isn't any way to know who created the channel , here's the code -
client.on("channelCreate", function(channel){
console.log(channel.type+` channel, `+channel.name+`,`+` in the category `+channel.parent.name);
});
I am setting up a new entry system for my Discord server with my bot, but I am having a hang up on getting some code to function.
My basic goal, is to have a post sitting in the welcome channel with rules and info, and the user has to click a reaction on the post, then the bot gives them a specific role and removes the 'Newbie' role. I have no had any issues doing this as long as the user is the one calling the message, but my issue comes in when trying to pull the guildMember object in, so I can roles.add them, it just doesn't seem to work? I've tried several different methods and have came up short every time. :C
message_id is being identified and ran properly, I am console logging and caching it outside of this block, seems fine. It's just this part that I cannot get to work. The bot doesn't error, or reply to the event.
Using node.js 12 (dino is my Client, btw)
dino.on("messageReactionAdd", (reaction, user) => {
if (reaction.emoji.id == '☑️' && reaction.message.id === message_id) {
let ourUser = reaction.message.guild.member(user)
ourUser.roles.add(`add ID would go here`)
ourUser.roles.remove(`Remove ID would go here`)
}
});
I found the issue, I was calling emoji.id instead of emoji.name lol. I'm an idiot. Works fine now, soon as I post it, I figure it out.. ofc.
I'm making a bot where if you do d!move, the bot will move the channel where the message was sent in under a category via ID. I also want to make it so that whoever does the command has permissions such as MANAGE_CHANNELS, which I've already added. The problem is that when I want to confirm whoever created that channel is the person that activated the command, the bot says yes. I did this on an alt account, where I made the channel and my alt was the one initializing it, and the bot said "success!" I also wanted to make it so if someone else made the channel, and when I did it, it would work because I made the bot know my ID.
I've researched Google and found nothing.
I've tried using a function with fetchAuditlog but get anywhere.
if(!message.channel.client.user.id == message.author || !message.author.id == `329023088517971969`) return message.channel.send("You don't own this channel!")
else message.channel.send("success!");
message.channel.setParent(`576976244575305759`);
I expect the bot to be able to check if the author created the channel, and lead to You don't own this channel if they don't own it. But if they do then the bot moves the channel.
The actual result is the bot moving the channel anyway regardless if they own the channel or not.
As #André has pointed out, channel.client represents the client itself, not the user who created the channel. Also, the last line in your code is not part of the else statement, so that's why it's run regardless of the conditions you defined.
To reach a solution, you can make use of the guild's audit logs. You can search for entries where the user is the message author and a channel was created. Then, all you have left is to check if one of those entries is for the current channel, and run the rest of your code if so.
Sample:
message.guild.fetchAuditLogs({
user: message.author,
type: 'CHANNEL_CREATE'
}).then(logs => {
if (!logs.entries.find(e => e.target && e.target.id === message.channel.id)) return message.channel.send('You don\'t own this channel.');
else {
// rest of code
}
}).catch(err => console.error(err));
When you go <anything>.client.user it will return the bot client.
If you want to see who created the channel you would have to check the Audit Logs or save it internally.
I've checked the documents. Here's what it says for .client about the
channel. It says the person that initialized the channel, or the
person that created it.
On documentation I see this:
The client that instantiated the Channel
instantiated is different of initialized
I have recently been trying to make a Reaction-to-Role bot and have been struggling with some criteria for making it.
I have researched and looked at other peoples' versions of a similar bot on it, yet I have not been able to use it purposefully in my own code.
The goal of the bot is to have multiple options on a message. Here's an example of what it would look like as a message.
**What do you drive?**
🚙 - A car
🚲 - A bicycle
❌ - Nothing
There would be reactions under the message of each emoji that was specified. Once a user clicked on one of these emojis, it would add a role to them such as "Drives a Bike" or "Rides a Bicycle."
I have managed to go as far as making an addrole command (which receives the message the bot will react to, the emoji, and the role it assigns) and stores some info to a JSON file.
The area in which I have been having issues is actually capturing each user that reacts to the specific message, finding out which reaction they sent, and assigning the role assigned to that emoji.
Here is my current code, I'm thank you for any help you may provide :)
addrole.js
if(!args[0] || !args[1] || !args[2] || !args[3]) return message.reply("please use the following format: `autorole #channel messageID icon #role`.");
let channel = message.mentions.channels.first();
let messageID = args[1];
let icon = args[2];
let role = message.mentions.roles.first();
if(!channel.fetchMessage(messageID)) return message.reply("could not find the specified message. Please check the channel and message ID again.");
channel.fetchMessage(messageID).then(msg => {
msg.react(icon);
info[role.name] = {
roleID: role.id,
channelID: channel.id,
icon: icon,
};
fs.writeFile("./configs/info.json", JSON.stringify(info), (err)=>{
if(err) console.log(err);
});
});
I know that creating an event named messageReactionAdd and messageReactionRemove are needed, but I'm not sure how to find their variables and match them accordingly.
Additionally, I am not sure how to make it constantly watch one message even after a restart. From some testing, I noticed that messageReactionAdd would not continue watching the specified messages reactions after a restart.
I am looking for any guidance/help that anyone may provide, thank you!
I've seen on: https://github.com/Sam-DevZ/Discord-RoleReact/blob/master/roleReact.js that there is an event. I've tried this out and it's working.