Creating a Discord bot to notify people when to certain text on a website has changed - discord.js

const Discord = require('discord.js');
const bot = new Discord.Client();
const token = '';
bot.on('ready', () =>{
console.log('This bot is online');
})
bot.on('message', msg=>{
if(msg.content === "?rates"){
msg.reply('.')
}
})
bot.login(token);
This is what I have so far it is very basic, I understand, I'm just trying to get some sort of idea how to process. What I want is as soon as a website gets updated or changed. I would like it to tag everyone and in a certain channel and specifies what has changed. I know this will be a long process but I'm in for the ride :) would appreciate any help.

You need a webhook on the website and listen to it with your bot, if you have control over the site, this may help you, otherwise you could look if the site has one or perhaps ask an owner.
A probably working but not very nice (and not very clean) solution would be to save the text of the website every 5 seconds or so and compare it to the previous save. If it changed, you notify the members through sending a message.

Related

discord.js say command help meeee

so im trying to put this in a .js file in my replit discord bot and it wont power up, any know how to fix it? this Is how i did my other things but it includes the prefix, and i don't want the prefix to show it, like, when someone says hi i want the bot to say for example hello back to him. enter image description here
client.on("message", async message =>{
if (message.content.startsWith("ce faci"))
{
message.channel.send(uite ma scarpinam la coaie)
}
If you haven't already, go to the Discord Developer Portal (https://discord.com/developers/applications), click "New Application", give it any name you want, go to the left side of the screen and click on "Bot", and press the button "Add Bot", and press "Yes, do it!" to the pop-up. There should be a token that's been generated. Copy it and put it in the code template bellow.
// Import discord.js and create the client
const Discord = require('discord.js')
const client = new Discord.Client();
// Register an event so that when the bot is ready, it will log a messsage to the terminal
client.on('ready', () => {
console.log(`Logged in as ${client.user.tag}!`);
})
// Register an event to handle incoming messages
client.on('message', async msg => {
// Check if the message starts with '!hello' and respond with 'world!' if it does.
if(msg.content.startsWith("!hello")) {
msg.reply("world!")
}
})
// client.login logs the bot in and sets it up for use. You'll enter your token here.
client.login('your_token_here');
It might be helpful to use a guide, and you'll find plenty of them on YouTube that teaches you the basics of using Discord.js.
If this is your first time coding in JavaScript or coding in general, I'd suggest that you go on websites such as Codecademy, freeCodeCamp, Khan Academy, YouTube, W3Schools, etc. to learn the basics.

Discord: Reading messages from one channel in a (non-admin perm) server, and posting it to another (with admin perms)

I am new to Discord API framework.
ServerFROM is a public server that I was invited to (non-admin perms). Hence I cannot add bots there. But I can view the content in ChannelFROM (a text channel in ServerFROM)
I have my own ServerTO (in which I have admin perms and so can do anything). Inside of which, I have the target ChannelTO
I want to deploy a listener on ChannelFROM, such when there is a new message (announcement) in ChannelFROM, I want it to be read and reposted in ChannelTO.
Something similar to what is done in this Stackoverflow issue, except that I cannot have some script run locally 24x7 on my machine. Maybe use Github Actions or something similar?
How can I go about doing it? Any ideas are appreciated. Maybe some form of a server, or just a custom Discord bot
And thanks in advance.
you can use Custom bot for that. I dont know other way
here's how i do it
1st : We Get the ID of the Channel that you wanted to listen
2nd : We make a Output or where the bot copy the message from and send it
3rd : Bot required a permission to view the channel and send message
or in the nutshell ( sorry if im bad at explaining )
client.on("messageCreate", message => {
if(message.author.bot) return;
if(message.channel.id === ID_HERE) // ChannelFROM in ID_HERE
{
let MSG = message.content
let Author = message.member.displayName
let Avatar = message.author.displayAvatarURL({dynamic: true})
const Embed = new MessageEmbed()
.setAuthor(Author , Avatar )
.setDescription(MSG)
.setColor("RANDOM")
client.channels.cache.get(ID_HERE).send({ embeds: [Embed] }) // SendTo in ID_HERE
}
})
you can remove if(message.author.bot) return; if you want it also read other bot message on that specific channel

How to not send bots message edit discord.js

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;

Im trying to do a reaction role bot on discord

client.on('messageReactionAdd', async (reaction, user) => {
if(reaction.message.id === "731619243249893417"){
const guildMember = reaction.message.guild.members.cache.get(user.id)
if(!guildMember.roles.cache.get("692177977705889845")){
guildMember.roles.add("692177977705889845");
Im using this code, but when i react to the message it
don't give me the role, im a starter needing help, thanks u all, and sorry for my english
Bots only listen for new messages. You have to tell them to listen for an old message explicitly to get a reaction from them. Try using this code. It fetches the message when a reaction is added to that message.

Is it possible to have your discord bot kick someone automatically without a mention?

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.

Resources