I have been trying different ways to retrieve the current list of members in a specific channel (e.g. 'networking'). But I am only get one, the actual bot user inside the channel, but nothing else.
This is the latest version of the code I am trying.
client.channels.cache.filter((c) => c.name === 'networking').forEach(channel => {
channel.fetch().then((channel) => {
console.log(channel.name);
for (let [snowflake, guildMember] of channel.members) {
console.log(`${guildMember.displayName} (${guildMember.id})`);
}
});
});
It's probably something to do with caching but I'm just not able to find the right sequence. Any help would be appreciated.
Thank you Tyler2P for the answer. You were absolutely right. Membership intents were disabled by default. I have lost so much time on this :) Thanks a million.
For everyone who gets to this question. Solution is so simple as enabling the "Server Members Intent".
Bot configuration in Discord Application page
Related
I have been using autocode.com to create some Discord bots. I have very little programming experience and found autocode to be quite easy. However, I've tried asking a question on autocode's discord that no one seems to understand or is asking.
I am trying to create a bot that replies to reactions--but does not assign roles, but instead, provides a reply--either in thread or a DM to that user who uses that specific emoji reaction.
For example, this is what I am looking to do: if there is a bot message in #channelx, userX will react to that message with a pepperoni emoji and then a pizza bot will reply back with a message either in thread or DM such as, "Hi #userx, your pizza topping has been recorded and will be ready for pickup in 15 minutes, please visit this link.com to track your order".
Autocode has a bot that can react to reactions and assign roles but I can't seem to reverse engineer it give a reply, rather than assign roles.
I appreciate any assistance. Thanks!
What does autocode use? Python or node.js? If python, you can do something like this:
#client.event
async def on_message(message):
if message.author == client.user:
return
if message.content.startswith('message'):
await message.channel.send('hi')
If node.js, you can do something like this:
client.on('messageCreate', msg => {
if (msg.content === 'specific message') {
msg.reply(`response text`);
}
});
I was previously a Community Hero at the Autocode Discord server. Try finding another app through this, and if none are available, the thing to do would be looking through the API's. Here's one for messaging in general, here's one for responding, and here's one for dm-ing.
Let's say, for example, I'd be making it reply to a reaction through DM:
The first thing you do is make sure the event trigger is set to message.reaction.add. This is so that the code you will write is triggered whenever a reaction is added.
Make either a switch or if statement to change what happens depending on what reaction triggers the code. In this example, I'll just use an if statement for easy explanation.
const lib = require('lib')({token: process.env.STDLIB_SECRET_TOKEN});
if (context.params.event.emoji.id == '1234567890') {
await lib.discord.users['#0.2.1'].dms.create({
recipient_id: `${context.params.event.member.user.id}`,
content: `Hi <#${context.params.event.member.user.id}>, your pizza topping has been recorded and will be ready for pickup in 15 minutes, please visit this link.com to track your order`
});
}
What this does is check if the thing that triggered this event has the emoji id equaling '1234567890': If it does, then it follows into that if statement, and if it does not, it skips over it.
In the future, please stay patient in the Autocode Discord server; The ones who are helping are also community members, similar to here. You may always ask the same question every now and then.
Good morning everyone,
I am currently running into one pretty irritating issue with getting a users messages from before the bots launch. I have been able to do this in the past (a few months ago), but it seems they have replaced the Intents.FLAGS approach for GatewayIntentBits. This has not been to complicated to change, but some problems have occurred.
One of the biggest issues, and the reason for this question is that even though I contain data in my intents that would allow for reading of reactions, as well as adding partials (I read it may help online). This does not seem to fix the issue.
const client = new Client({ intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMessageReactions, GatewayIntentBits.GuildMessages, GatewayIntentBits.GuildMembers], partials:["Message", "Channel", "Reaction"]})
. . .
client.on('messageReactionAdd', _ => {
console.log('a reaction has been added');
});
client.on('messageReactionRemove', _ => {
console.log('a reaction has been removed');
});
I know this isn't allot to go off of, but I have tested it with barely anything in my application and I still run into this error. Is this a known issue with the change to GatewayIntentBits? I would love to continue working on this bot.
Thank you for any assistance you may be able to provide.
EDIT: I have managed to get the reactions to work on the application now. I have not even started touching those old messages, and its working. Thank you for your help. My best bet of why its working is that the messages needed to be resent with the partials and intents specified above. I dont know why this didnt work before, but whatever.
Your gateway intent bits are fine, you need to do something similar to that for the partials as well, you need to import partials from discord.js and use them like that.
const { Client, Partials } = from 'discord.js';
const client = new Client({
intents: [],
partials: [Partials.Message, Partials.Channel, Partials.Reaction]
});
I’m trying to set up a chat reply bot for helping admin a game discord server. I have a list of words for it to reply to and can get him reply to them no problem. The problem I’m having is that one of the words is ESP so when someone was to type the word response it picks up the word esp in the word response and replies when I don’t want him to. Another example is the work hack. When someone types hacksaw ridge in chat he replies to that to. I want it to just look for the word esp along with a few other cheating terms and not look for it in the middle of a longer word.
let cheater = ["cheating", "cheater", "aim_bot", 'esp', "hack"]
client.on('messageCreate', (message) => {
if (message.author.bot) return false;
if (cheater.some(w => message.content.toLowerCase().includes(w))) {
message.reply('if you think someone is cheating report it here <#976517390701563955>');
}
})
Any help would be greatly appreciated
Thanks.
One of ways to solve it is very simple in fact, if you want your bot to react only to full words, you can use:
if(message.content.toLowerCase().includes(` ${your_variable} `)) {
// your code
}
instead of what you have in your code
You need to look for an Array.prototype.some() to get the message in your array. To accomplish this.
client.on('messageCreate', async(message) => {
if (message.author.bot) return false;
let messages = ["cheating", "cheater", "aim_bot", 'esp', "hack"]
if(messages.some(element => message.content.toLowerCase().includes(element))) {
message.reply("if you think someone is cheating report it here <#976517390701563955>")
}
})
My bot is currently part of multiple server and Im trying to get it to add a role to a user when they join one of the servers, I have done stuff like
if(bot.guild.id === [SERVER ID]) and I've tried different forms of the bot.on command to no avail.
the current code is
bot.on('guildMemberAdd', guildMember => {id
let welcomerole = guildMember.guild.roles.find(role => role.name === 'Nomad');
guildMember.roles.add(welcomerole);
guildMember.guild.channels.get([SERVER_ID]).send("Welcome to the server")
})
But as per usual it does not work.
I'm not sure if there is a function im missing in the bot.on section of the block, or if the issue is something else. Note I am running Discord.js 11.6, so these functions do or should work with the version. (there is a reason).
Is there is a way to have it so the bot only adds the role to one of the servers that its a part of.
Just update to Discord.js V13, v11 is many years out of date and new docs solutions won't work with it.
I just began trying to learn how to write my first Discord bot this morning so I am very inexperienced with discord.js, but I am familiar with JavaScript. However I have been searching for a couple of hours trying to find a way to call a function whenever a user in my server receives or loses a role.
In my server I have added the Patreon bot which assigns a role to users who become patrons. And I would like to create a custom bot that posts "hooray username" in my general channel when a user receive the patron role.
I can not find any example that shows how to detect when a user gains or loses a role. Is it possible to do this simply using an event? Or would I possibly need to periodically iterate over all users and maintain a list of their current roles while checking for changes?
I apologize that my question doesn't include any code or examples but I haven't made any progress and am reaching out to the SO community for guidance.
You want to use the event guildMemberUpdate.
You can compare the oldMember state to the newMember state and see what roles have changed.
This is not the most elegent solution but will get the job done.
client.on('guildMemberUpdate', (oldMember, newMember) => {
// Roles
const oldRoles = oldMember.roles.cache,
newRoles = newMember.roles.cache;
// Has Role?
const oldHas = oldRoles.has('role-id'),
newHas = newRoles.has('role-id');
// Check if removed or added
if (oldHas && !newHas) {
// Role has been removed
} else if (!oldHas && newHas) {
// Role has been added
}
});
https://discord.js.org/#/docs/main/stable/class/Client?scrollTo=e-guildMemberUpdate
This is simple with Client#guildMemberUpdate. Here’s some simple code that may help you
(This is the shortest code I could come up with)
client.on('guildMemberUpdate', async (oldMember, newMember) => {
if(oldMember.roles.cache.has('patreonRoleId')) return;
if(newMember.roles.cache.has('patreonRoleId')) {
//code here to run if member received role
//Warning: I didn’t test it as I had no time.
}
})
To see the removed role, just put the logical NOT operator (!) in front of both of the if statements like this:
if(!oldMember.roles.cache.has('patreonRoleId'))
if(!newMember.roles.cache.has('patreonRoleId'))
Note: make sure you have guildMembers intent enabled from the developers portal