I made a bot that watches if a member goes online and gives a welcome gif.
I am trying to make the bot to wait when more than 1 goes online for each user because it can be annoying to see a couple of gifs from the same person (like logon spam).
Here is my work
bot.on("presenceUpdate", (oldMember, newMember) => {
if(newMember.presence.status === 'online')
{
console.log(`${newMember.user.username} is now ${newMember.presence.status}`);
bot.channels.get('id').send(`${newMember.user.username} **online**`, {
files: [
"gif"
]
})
}
});
If you read the Discord.js documentation, you can see that Game class has a name property. It's this property which is equal to the name of the game.
So replace newMember.presence.game.state with newMember.presence.game.name.
Related
I have been trying to implement an interaction collector as per the discord.js guide, but the guide does not explain much of anything and the pieces I have puzzled together from other sources do not fit together. Here's the example from the guide:
message.awaitMessageComponent({ filter, componentType: 'SELECT_MENU', time: 60000 })
.then(interaction => interaction.editReply(`You selected ${interaction.values.join(', ')}!`))
.catch(err => console.log(`No interactions were collected.`));
It took me a day to figure out that you can define a message object like so:
message = await interaction.channel.send(content: 'text', components: >select menu<)
That works to run .awaitMessageComponent() and to grab the input from the select menu, however interaction.editReply() gives an error: INTERACTION_NOT_REPLIED
Moreover, I need it to be a reply anyway, however
message = await interaction.reply(content: 'text', components: >select menu<)
leaves message as undefined, so of course I cannot run .awaitMessageComponent() on it.
So I do not understand what I am supposed to do to do what that guide is doing.
I would very much appreciate any insight into this issue.
Unfortunately all other guides (usually on collectors) also start with a .send() message and most other resources go way over my head, as I have no real background in coding/scripting.
EDIT:
As Malik Lahlou points out, you can assign the reply by including the fetchReply:option, like so:
message = await interaction.reply({ components: [select menu], fetchReply: true })
However that still does not allow the guide's code to run (error INTERACTION_NOT_REPLIED, so after some more research I finally found a way to use .awaitMessageComponent() and applied to the guide's example it would look like this:
await interaction.reply({ components: [select menu], ephemeral: true });
const collectedSelect = await interaction.awaitMessageComponent({ filter, componentType: 'SELECT_MENU', time: 60000 })
.then(interaction => interaction.editReply(`You selected ${interaction.values.join(', ')}!`))
.catch(err => console.log(`No interactions were collected.`));
Simple answer, add the fetchReply option when replying to the interaction :
const message = await interaction.reply({
// content : ...,
// components : ...,
fetchReply: true
})
Another way would be to use interaction.fetchReply after awaiting the reply but it's better to directly give it as a parameter so discord.js does this stuff for us :D Here's an example on the official discord.js guide
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
basically what im trynna do here is if u have used carl bot you may know you can make it so on a message(for example a ping) it would react with message of ur choice, I'm trynna basically do that and heres what ive tried
if (lowerCaseMessage === messagetoReact) {
message.react('🐧')
}
and in my config file
"messagestoReact" : ["<#729795837223501834>"]
something like that, i have declared everything and made it like a on message thing, but it doesnt quite work, any one know a fix and if you need me to specify anything feel free to ask
I'm not sure what you're asking?Here's the way to get your bot to react to a message:
client.on('message', async message => {
if (message.content === '<#729795837223501834>') {
message.react('🐧');
}
})
I have a message edit log but I want to stop sending the log if a mobs message was updated, I tried a few codes like
if(bot.oldMessage.content.edit()){
return;
}
It showed and error
cannot read property 'edit' of undefined
I then removed edit then content was undefined. The code for the message update is below.
The Code
module.exports = async (bot, oldMessage, newMessage) => {
let channels = JSON.parse(
fs.readFileSync('././database/messageChannel.json', 'utf8')
);
let channelId = channels[oldMessage.guild.id].channel;
let msgChannel = bot.channels.cache.get(channelId);
if (!msgChannel) {
return console.log(`No message channel found with ID ${channelId}`);
}
if (oldMessage.content === newMessage.content){
return;
}
let mEmbed = new MessageEmbed()
.setAuthor(oldMessage.author.tag, oldMessage.author.displayAvatarURL({dynamic: true}))
.setColor(cyan)
.setDescription(`**Message Editied in <#${oldMessage.channel.id}>**`)
.addField(`Before`, `${oldMessage.content}`)
.addField(`After`, `${newMessage.content}`)
.setFooter(`UserID: ${oldMessage.author.id}`)
.setTimestamp()
msgChannel.send(mEmbed)
}
How would I stop it from sending the embed if a bots message was updated.
Making a really simple check will resolve this issue. In Discord.js there is a user field that tells you if the user is a bot or not.
In fact, it is really recommended you add this in the "onMessage" part of your code as it stops other bots from using your bot, this is to make sure things are safe and no loopbacks/feedbacks happen, either way, you don't want a malicious bot taking advantage of your bot, which can get your bot in trouble too.
Here is what you want to do;
if (message.author.bot) return;
What this code specifically does is check if the message's author is a bot, if it returns true, it will break the code from running, if it returns a false, the code continues running.
You can do the same if you want to listen to bots ONLY by simply adding a exclamation mark before the message.author.bot like this;
if (!message.author.bot) return;
It is also possible to see what other kinds of information something holds, you can print anything to your console. For example, if you want to view what a message object contains, you can print it into your console with;
console.log(message) // This will show everything within that object.
console.log(message.author) // This will show everything within the author object (like ID's, name, discriminators, avatars, etc.)
Go ahead and explore what you can do!
Happy developing! ^ -^
That is really easy to do. All you need to do is check if the author of the message ist a bot and then return if true. You do that like this
if (oldMessage.author.bot) return;
I'm trying to create a bot that kicks someone after they say a specific word, but I don't want to have to mention the user after I say the word. This is my code so far:
case 'ok':
const user = message.member
if (user) {
const member = message.guild.member(user);
if (member){
member.kick('Banned').then(() =>{
message.reply(`Banned`)
})
}
}
break;
}})const PREFIX = '';
Is there some way to make the bot automatically kick the user after they say the case word "ok"?
You could simply use String.prototype.includes(), then kick them using GuildMember.kick() as you are now.
// in your existing message event
client.on('message', (message) => {
if (message.content.includes('ok')) // if the content includes 'ok'
message.member.kick().catch(console.error); // kick the member
});
I would recommend reading the full discord.js guide, as it can help you find a greater understanding of the discord.js library, and how to use it with javascript.