I am trying to log presence for my bot called Timer Bot. I want it to alert everyone when he goes online and when he goes offline. Here is the script I'm using -
client.on('presenceUpdate', (oldPresence, newPresence) => {
let member = newPresence.member;
// User id of the user you're tracking status.
if (member.id === '603517534720753686') {
if (oldPresence.status !== newPresence.status) {
// Your specific channel to send a message in.
let channel = member.guild.channels.cache.get('788547135234375712');
// You can also use member.guild.channels.resolve('<channelId>');
let text = "";
if (newPresence.status === "online") {
text = "**Hello #everyone, Timer Bot is now online! Thank you for your patience.**";
} else if (newPresence.status === "offline") {
text = "**#everyone Due to issues, Timer Bot is currently offline. We apologize for the inconvenience.**";
}
// etc...
channel.send(text);
}
}
});
For some reason, it doesn't work. Anyone know why?
Thanks,
Brian.#1111
You need to enable presence intent and then pass it in to the client when logging in.
new Discord.Client({ws:{intents: [Intents.FLAGS.GUILD_PRESENCES]}});
Source: https://discord.js.org/#/docs/main/stable/class/Intents?scrollTo=s-FLAGS
https://discord.com/developers/applications/
Related
Hello I am trying to use a discord bot to wait for a message from an admin of my server and then check the following messages from users. My code waits for the admin message however it somehow caches it ?? At least it doesnt check the users messages. I made my bot write the message where it got triggered to make it more understandable:
const Discord = require("discord.js");
const bot = new Discord.Client();
const bot_config = require("./settings.json")
var token = bot_config.usertoken
var userid = bot_config.userid
var channel = "940800688219369483"; //
bot.on("ready", () => {
console.log(`SUCCESSFULLY LOGGED IN AS ${bot.user.tag}!`);
});
let counter = 0
bot.on("message", (msg) => {
//Waiting for mesage from specific admin
if(msg.author == "1011983215650672781"){
if(counter == 0){ // check if message has already been send
if (msg.channel.id != channel) return; // Checks valid Channel ID
if (msg.author == userid && msg.author == "703629528454660106" && msg.authot == "255396012045238222" && msg.authot == "142772377047199744" && msg.authot == "235748962103951360") { //CHECK IF NEXT MESSAGE IS NOT AN ADMIN
console.log("ADMIN MESSGAE");
}
else{
let answer = msg.content
bot.channels.cache.get(channel).send(answer)
counter ++
console.log("SUCCESSFULLY SENT MESSAGE")
}
}
else{
console.log("ALREADY SENT MESSAGE - CLOSE BOT")
}
}
})
bot.login(usertoken)
The problem is, the bot still writes the first admin message in the channel and not the onw from the first user
You might kick yourself for this, but you have used msg.authot instead of msg.author.id.
You may want to check out the docs, they come in handy when you need to know how to access properties.
So I want that in a specific channel, where advertising is enabled, when someone pastes in a facebook, or instagram or whatever link, then the bot automatically reacts to that message with the specific logo emote, what the server has.
Is there any way to do this?
I'm thinking about something like this:
client.on('message', message => {
if (message.channel.id == channel.id.here && message.author.bot == false && message.content("facebook")) { addreactions1(message); }
});
function addreactions1(message) {
if (message.attachments.size === 1) {
message.react("facebook.logo.emote")
return;
}
Since <Message>.content returns a String, you can use the includes() method which performs a case-sensitive search of one string in another string. Eg:
client.on('message', message => {
if(message.author.bot) return; //returns doesn't respond to bot or webhook messages.
if(message.channel.id === "channelid"){
if(message.content.includes("Facebook"){ //searches for Facebook in the message.
message.channel.send("Message content had Facebook as a word in it"); // do anything here
}
}
}
I have the problem that the bot the member count updates only once and does nothing else after. Does anyone know how to solve it?
Heres my current code:
bot.on("ready", () => {
const guild = bot.guilds.cache.get('779790603131158559');
setInterval(() => {
const memberCount = guild.memberCount;
const channel = guild.channels.cache.get('802083835092795442')
channel.setName(`DC︱Member: ${memberCount.toLocaleString()}`)
}, 5000);
});
If I am understanding you correctly, you want to rename a VC to the member count. The Discord API only lets you rename a channel 2 times every 10 minutes. You are trying to run that code every 5 seconds.
Try setting your timeout delay to 600000 instead of 5000.
You could try to use voiceStateUpdate, it's fired everytime a user leaves, enters, mutes mic or unmutes mic. Here's a link to it: voiceStatusUpdate
You can also use voiceChannelID if you want to get the ID of the channel. Here a link: voiceChannelID
Here's a basic idea of the code you can use:
bot.on('voiceStateUpdate', (oldMember, newMember) => {
let newUserChannel = newMember.voiceChannel
let oldUserChannel = oldMember.voiceChannel
if(oldUserChannel === undefined && newUserChannel !== undefined) {
// User Joins a voice channel
} else if(newUserChannel === undefined){
// User leaves a voice channel
}
})
I'm attempting to make it so that when someone joins the Voice Channel, so the Bot will add the specific person to the text channel with the permission to read and send messages and remove the individual and their permissions when they leave the Voice Channel. I'm not overly familiar with discord.js so I'm not sure on how to do it.
First and foremost, welcome to Stack Overflow. I hope we can be of help to you.
Let's start by detecting when a member joins a voice channel. To do so, we can listen to your client's voiceStateUpdate event. Next, we can compare the old voice channel with the new one, and see if the member joined or left. Finally, we can change the permissions for the member in the text channel using GuildChannel.overwritePermissions().
Update: Multiple "pairs" of text channels and voice channels with similar behavior.
To do this for many different channels, you could set up a json file to store the voice channels and corresponding text channels, and then iterate over each pair, checking if the situation matches any.
channelPairs.json
[
{ "voice": "voiceChannelIDHere", "text": "textChannelIDHere" }
]
index.js
const pairs = require('./channelPairs.json'); // Keep in mind the path may vary
client.on('voiceStateUpdate', (oldMember, newMember) => {
let oldID;
let newID;
if (oldMember.voiceChannel) oldID = oldMember.voiceChannel.id;
if (newMember.voiceChannel) newID = newMember.voiceChannel.id;
for (let i = 0; i < pairs.length; i++) {
const textChannel = newMember.guild.channels.get(pairs[i].text);
if (!textChannel) {
console.log('Invalid text channel ID in json.');
continue;
}
const vcID = pairs[i].voice;
if (oldID !== vcID && newID === vcID) { // Joined the voice channel.
textChannel.overwritePermissions(newMember, {
VIEW_CHANNEL: true,
SEND_MESSAGES: true
}).catch(console.error);
} else if (oldID === vcID && newID !== vcID) { // Left the voice channel.
textChannel.overwritePermissions(newMember, {
VIEW_CHANNEL: null,
SEND_MESSAGES: null
}).catch(console.error);
}
}
});
Okay so let's take a look at the DiscordJS API:
Found here.
As we can see, there's a class called "WSEvent" (stands for WebsocketEvent). We can use this to detect when a user joins the channel...
But we can't really use that because there's no "CHANNEL_JOIN" event. So instead, we must listen for this voiceStateUpdate or VOICE_STATUS_UPDATE event.
Like so:
// voiceStateUpdate
/* Emitted whenever a user changes voice state - e.g. joins/leaves a channel, mutes/unmutes.
PARAMETER TYPE DESCRIPTION
oldMember GuildMember The member before the voice state update
newMember GuildMember The member after the voice state update */
client.on("voiceStateUpdate", function(oldMember, newMember){
console.log(`a user changes voice state`);
// Here we can just check if newMember is in the channel that we want. Bam.
if(newMember.voiceChannel.name == 'channelname') {
// DO SOMETHING.
myVoiceChannel.overwritePermissions(newMember, {
SEND_MESSAGES: true
});
} else {
myVoiceChannel.overwritePermissions(newMember, {
SEND_MESSAGES: null
});
}
});
Let's reduce this:
newMember.voiceChannel - This means what channel their connected to.
voiceChannel.name - Get the name of the channel so we can check it.
Hope I was a help, and by the way, here's a neat little cheatsheet I like to use when developing DiscordJS bots: https://gist.github.com/koad/316b265a91d933fd1b62dddfcc3ff584
So I have been search far and wide over the internet, trying to find a possible way to make a purge command. Now I have found quite a lot of different ways to make one, but none of them either suited me in the way I wanted, or simply worked for me.
SO to start off, here is my code
const Discord = require("discord.js"); // use discord.js
const BOT_TOKEN = "secret bot token :)" // bot's token
const PREFIX = "*" // bot's prefix
var eightball = [ // sets the answers to an eightball
"yes!",
"no...",
"maybe?",
"probably",
"I don't think so.",
"never!",
"you can try...",
"up to you!",
]
var bot = new Discord.Client(); // sets Discord.Client to bot
bot.on("ready", function() { // when the bot starts up, set its game to Use *help and tell the console "Booted up!"
bot.user.setGame("Use *info") // sets the game the bot is playing
console.log("Booted up!") // messages the console Booted up!
});
bot.on("message", function(message) { // when a message is sent
if (message.author.equals(bot.user)) return; // if the message is sent by a bot, ignore
if (!message.content.startsWith(PREFIX)) return; // if the message doesn't contain PREFIX (*), then ignore
var args = message.content.substring(PREFIX.length).split(" "); // removes the prefix from the message
var command = args[0].toLowerCase(); // sets the command to lowercase (making it incase sensitive)
var mutedrole = message.guild.roles.find("name", "muted");
if (command == "help") { // creates a command *help
var embedhelpmember = new Discord.RichEmbed() // sets a embed box to the variable embedhelpmember
.setTitle("**List of Commands**\n") // sets the title to List of Commands
.addField(" - help", "Displays this message (Correct usage: *help)") // sets the first field to explain the command *help
.addField(" - info", "Tells info about myself :grin:") // sets the field information about the command *info
.addField(" - ping", "Tests your ping (Correct usage: *ping)") // sets the second field to explain the command *ping
.addField(" - cookie", "Sends a cookie to the desired player! :cookie: (Correct usage: *cookie #username)") // sets the third field to explain the command *cookie
.addField(" - 8ball", "Answers to all of your questions! (Correct usage: *8ball [question])") // sets the field to the 8ball command
.setColor(0xFFA500) // sets the color of the embed box to orange
.setFooter("You need help, do you?") // sets the footer to "You need help, do you?"
var embedhelpadmin = new Discord.RichEmbed() // sets a embed box to the var embedhelpadmin
.setTitle("**List of Admin Commands**\n") // sets the title
.addField(" - say", "Makes the bot say whatever you want (Correct usage: *say [message])")
.addField(" - mute", "Mutes a desired member with a reason (Coorect usage: *mute #username [reason])") // sets a field
.addField(" - unmute", "Unmutes a muted player (Correct usage: *unmute #username)")
.addField(" - kick", "Kicks a desired member with a reason (Correct usage: *kick #username [reason])") //sets a field
.setColor(0xFF0000) // sets a color
.setFooter("Ooo, an admin!") // sets the footer
message.channel.send(embedhelpmember); // sends the embed box "embedhelpmember" to the chatif
if(message.member.roles.some(r=>["bot-admin"].includes(r.name)) ) return message.channel.send(embedhelpadmin); // if member is a botadmin, display this too
}
if (command == "info") { // creates the command *info
message.channel.send("Hey! My name is cookie-bot and I'm here to assist you! You can do *help to see all of my commands! If you have any problems with the Minecraft/Discord server, you can contact an administrator! :smile:") // gives u info
}
if (command == "ping") { // creates a command *ping
message.channel.send("Pong!"); // answers with "Pong!"
}
if (command == "cookie") { // creates the command cookie
if (args[1]) message.channel.send(message.author.toString() + " has given " + args[1].toString() + " a cookie! :cookie:") // sends the message saying someone has given someone else a cookie if someone mentions someone else
else message.channel.send("Who do you want to send a cookie to? :cookie: (Correct usage: *cookie #username)") // sends the error message if no-one is mentioned
}
if (command == "8ball") { // creates the command 8ball
if (args[1] != null) message.reply(eightball[Math.floor(Math.random() * eightball.length).toString(16)]); // if args[1], post random answer
else message.channel.send("Ummmm, what is your question? :rolling_eyes: (Correct usage: *8ball [question])"); // if not, error
}
if (command == "say") { // creates command say
if (!message.member.roles.some(r=>["bot-admin"].includes(r.name)) ) return message.reply("Sorry, you do not have the permission to do this!");
var sayMessage = message.content.substring(4)
message.delete().catch(O_o=>{});
message.channel.send(sayMessage);
}
if(command === "purge") {
let messagecount = parseInt(args[1]) || 1;
var deletedMessages = -1;
message.channel.fetchMessages({limit: Math.min(messagecount + 1, 100)}).then(messages => {
messages.forEach(m => {
if (m.author.id == bot.user.id) {
m.delete().catch(console.error);
deletedMessages++;
}
});
}).then(() => {
if (deletedMessages === -1) deletedMessages = 0;
message.channel.send(`:white_check_mark: Purged \`${deletedMessages}\` messages.`)
.then(m => m.delete(2000));
}).catch(console.error);
}
if (command == "mute") { // creates the command mute
if (!message.member.roles.some(r=>["bot-admin"].includes(r.name)) ) return message.reply("Sorry, you do not have the permission to do this!"); // if author has no perms
var mutedmember = message.mentions.members.first(); // sets the mentioned user to the var kickedmember
if (!mutedmember) return message.reply("Please mention a valid member of this server!") // if there is no kickedmmeber var
if (mutedmember.hasPermission("ADMINISTRATOR")) return message.reply("I cannot mute this member!") // if memebr is an admin
var mutereasondelete = 10 + mutedmember.user.id.length //sets the length of the kickreasondelete
var mutereason = message.content.substring(mutereasondelete).split(" "); // deletes the first letters until it reaches the reason
var mutereason = mutereason.join(" "); // joins the list kickreason into one line
if (!mutereason) return message.reply("Please indicate a reason for the mute!") // if no reason
mutedmember.addRole(mutedrole) //if reason, kick
.catch(error => message.reply(`Sorry ${message.author} I couldn't mute because of : ${error}`)); //if error, display error
message.reply(`${mutedmember.user} has been muted by ${message.author} because: ${mutereason}`); // sends a message saying he was kicked
}
if (command == "unmute") { // creates the command unmute
if (!message.member.roles.some(r=>["bot-admin"].includes(r.name)) ) return message.reply("Sorry, you do not have the permission to do this!"); // if author has no perms
var unmutedmember = message.mentions.members.first(); // sets the mentioned user to the var kickedmember
if (!unmutedmember) return message.reply("Please mention a valid member of this server!") // if there is no kickedmmeber var
unmutedmember.removeRole(mutedrole) //if reason, kick
.catch(error => message.reply(`Sorry ${message.author} I couldn't mute because of : ${error}`)); //if error, display error
message.reply(`${unmutedmember.user} has been unmuted by ${message.author}!`); // sends a message saying he was kicked
}
if (command == "kick") { // creates the command kick
if (!message.member.roles.some(r=>["bot-admin"].includes(r.name)) ) return message.reply("Sorry, you do not have the permission to do this!"); // if author has no perms
var kickedmember = message.mentions.members.first(); // sets the mentioned user to the var kickedmember
if (!kickedmember) return message.reply("Please mention a valid member of this server!") // if there is no kickedmmeber var
if (!kickedmember.kickable) return message.reply("I cannot kick this member!") // if the member is unkickable
var kickreasondelete = 10 + kickedmember.user.id.length //sets the length of the kickreasondelete
var kickreason = message.content.substring(kickreasondelete).split(" "); // deletes the first letters until it reaches the reason
var kickreason = kickreason.join(" "); // joins the list kickreason into one line
if (!kickreason) return message.reply("Please indicate a reason for the kick!") // if no reason
kickedmember.kick(kickreason) //if reason, kick
.catch(error => message.reply(`Sorry #${message.author} I couldn't kick because of : ${error}`)); //if error, display error
message.reply(`${kickedmember.user.username} has been kicked by ${message.author.username} because: ${kickreason}`); // sends a message saying he was kicked
}
});
bot.login(BOT_TOKEN); // connects to the bot
It is the only file in my bot folder except the package.json, package-lock.json and all the node_modules.
What I'm trying to do is type in discord *purge [number of messages I want to purge] and get the bot to delete the amount of the messages I asked it to delete, PLUS the command I entered (for example, if I ask the bot to delete 5 messages, he deletes 6 including the *purge 5 message.
Any help would be much appreciated, thanks!
What you are looking for is this (bulkDelete()) method for Discord.js.
It bulk deletes a message, just simply pass in a collection of message in the method and it will do the job for you.
(You can use messages property from channel, otherwise if you prefer promises, then try fetchMessages() method. )
Just make sure that the channel is not a voice channel, or a DM channel. And finally, your bot needs to have permission too.
You can get your own bot's permission for the guild using message.guild.member(client).permissions, or you can just directly use message.guild.member(client).hasPermission(permission), which returns a boolean that determines if your bot have a desired permission.
(The method docs for hasPermission() is here)
note sure if this is still relevant but this what i use in my bots
if (!suffix) {
var newamount = "2";
} else {
var amount = Number(suffix);
var adding = 1;
var newamount = amount + adding;
}
let messagecount = newamount.toString();
msg.channel
.fetchMessages({
limit: messagecount
})
.then(messages => {
msg.channel.bulkDelete(messages);
// Logging the number of messages deleted on both the channel and console.
msg.channel
.send(
"Deletion of messages successful. \n Total messages deleted including command: " +
newamount
)
.then(message => message.delete(5000));
console.log(
"Deletion of messages successful. \n Total messages deleted including command: " +
newamount
);
})
.catch(err => {
console.log("Error while doing Bulk Delete");
console.log(err);
});
basic function is to specify number of messages to purge and it will delete that many plus the command used, the full example is here