Discord bot stopped sending welcome messages - node.js

So, my code for making the bot greet new users stopped working, and i have no idea why or how
this is the code that im using for the welcome event itself ```module.exports = (client) => {
const channelId = '757493821251649608' // welcome channel
const targetChannelId = '757521186929246219' // rules and info
client.on('guildMemberAdd', (member) => {
const message = `Hi, hope you enjoy your stay <#${
member.id
}> , Oh i almost forgot to tell you, check out! ${member.guild.channels.cache
.get(targetChannelId)
.toString()}`
const channel = member.guild.channels.cache.get(channelId)
channel.send(message)
})
}```
And this is how i make the bot execute it
const hi = require('./events/hi')
const hello = require('./events/hello')
const yo = require('./events/yo')
const whatsup = require('./events/whatsUp')
const bye = require('./events/bye')
client.once('ready', () =>{
console.log('Aiko is working!');
client.user.setActivity(' your orders!|Prefix is +', { type: "LISTENING" });
hi(client)
hello(client)
yo(client)
whatsup(client)
bye(client)
welcome(client)
});```
The event that should also send a message when someone leaves the server also doesn't go off, anyone any idea why?

This is most likely a privileged intents problem, which recently became required. Check out this question to see if it solves it. Also read the discord API docs for privileged intents

Related

Socket.Io not emitting immediately after first emit (order important)

Environment:
Backend
node:latest
socket.io | 4.5.2
Frontend
React Native | 0.70.4
socket.io-client | 4.6.0
both Android and iOS
Here is my NodeJs entry file:
const numCPUs = cpus().length
if (cluster.isPrimary) {
const app = express()
const httpServer = http.createServer(app)
setupMaster(httpServer, { loadBalancingMethod: 'least-connection' })
setupPrimary()
for (let i = 0; i < numCPUs; i++) {
cluster.fork()
}
cluster.on('exit', (worker) => {
cluster.fork()
})
} else {
const app = express()
const httpServer = http.createServer(app)
const io = new Server(httpServer, { maxHttpBufferSize: 1e8 })
io.adapter(createAdapter())
setupWorker(io)
API.Socket.init(io, process.pid)
middlewares.forEach((middleware: any) => app.use(middleware))
routes.forEach((route) => app.use(route.path, route.handler))
httpServer.listen(CONFIG.PORT, () => {})
}
I have a simple chat application.
When user A sends message to user B, new chat message and notification is recorded in database. Now that chat message and notification* should be sent to the B user. There are 2 socket emit-functions for that:
sendNewNotification(
notification: BE.Entities.TNotification,
toUser: string,
) {
this.io
?.to(toUser)
.volatile.emit(ECustomEvents.NewNotification, notification)
}
sendPrivateMessage(
toUser: string | Array<string>,
chatMessage: BE.Entities.TChatMessage,
sourceUser: BE.Entities.TUser,
) {
this.io
?.to(toUser)
.volatile.emit(ECustomEvents.PrivateMessage, chatMessage, sourceUser)
}
If I do it like this, the targetUser is not going to receive the event with the newChatMessage however he will receive the savedNotification
API.Socket.sendPrivateMessage(targetUserId, newChatMessage, userToPass)
API.Socket.sendNewNotification(savedNotification, targetUserId)
Now, if I switch these lines:
API.Socket.sendNewNotification(savedNotification, targetUserId)
API.Socket.sendPrivateMessage(targetUserId, newChatMessage, userToPass)
the behavior would be as expected: the target user B will receive both saved notification and new chat message
How is that possible? What could be wrong?
Thank you mates in advance!
With the current information, I'm not so sure the order matters but perhaps that it's a side-effect / coincidence. Are you checking anywhere to make sure the server-side socket is ready before the client emits?
Consider this super simple WebSocket chat sandbox:
One of the issues I noticed when writing this is when the server WebSocket is not ready, I could not emit from the client to the server. To make sure the server is ready, I sent a ping from the server to the client notifying the client that the server is ready:
wss.on("connection", async function connection(client, request) {
console.log("user connected", Date.now());
client.send(JSON.stringify({ ready: true }));
...
});
I also notice you are usingg the volatile.emit which according to the documentation:
Volatile events
Volatile events are events that will not be sent if the underlying connection is not ready (a bit like UDP, in terms of reliability).
This can be interesting for example if you need to send the position of the characters in an online game (as only the latest values are useful).
socket.volatile.emit("hello", "might or might not be received");
The Socket.IO docs have a similar listener which lets you know when the server is ready.
If you prevent the client from emitting until the server is ready, you can avoid this issue. You also should not need to use the volatile.emit for something that must be delivered.

TypeError: throw new TypeError('CLIENT_MISSING_INTENTS'); Issue

I am recieving this message even though I added intents to my Index.js
Discord.js version: 13.3.1
Node: 16.6.1
Code of my Index.js
const config = require('../config.json');
const mongoose = require('mongoose');
const Discord = require("discord.js");
const bot = new Discord.Client();
const { Intents } = require('discord.js');
const client = new Client({ intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES] });
bot.on("ready", () => {
console.log(`Bot is online!\n${bot.users.size} users, in ${bot.guilds.size} servers connected.`);
});
// HERE IS MONGODB BUT NOT SHOWING
const Client = require('./Structures/Client');
const WelcomeSchema = require(`../src/Models/welcome`)
bot.on("guildMemberAdd", async (member, guil) => {
WelcomeSchema.findOne({ guildId: member.guild.id }, async (err, data) => {
if(!data) return;
const user = member.user;
const channel = member.guild.channels.cache.get(data.channelId);
channel.send({embed: {color: "BLUE", description: `sd`}})
})
})
const client = new Client(config);
client.start();
Would mean a lot if you could help me find the issue. Thanks!
I now realise that there are quite a few issues with the code you have provided.
No. 1 - Client Definitions
It looks like you're defining three different clients. (bot, client, and possibly Client.)
You should organise your code in such a way that all events and commands are tied to one client object, as having multiple clients running can lead to rate-limiting and performance issues. (as well as being completely and utterly pointless.)
The error seems to be stating that bot is not given any intents during its creation, which could be fixed with the use of...
// ...
const bot = new Discord.Client({ intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES] });
// ...
No. 2: Reference to an Undefined Variable
On the line where you define client at the start of the file, you use new Client, despite the fact that Client has not been imported yet, and is imported later in the file. This won't work, as Client is undefined at that point in the program.
No. 3: Re-assignment of a constant
It also seems that you re-assign another const client near the end of your file. This will cause an error, as client is already a defined constant which cannot be over-written.
No. 4: Access to Message Intents (maybe???)
As of recently, discord requires that you enable Gateway Intents to be able to access certain events and data (such as Server Messages and Members).
To enable the intents, head to the Discord Developer Dashboard for your bot, select "Bot" from the sidebar, and enable the intents you need access to.
While this intent is not required to be able to read messages until April 30th, 2022, if your bot specifies the GUILD_MESSAGES intent, this option needs to be enabled.
If your bot is in more than 100 servers, you will need to verify your bot to be able to continue accessing data which requires these intents. More about that here.

Discord.js - Without using Client EventEmitter cannot get channels property

I still have yet to get a better grasp of Node.js, but as I was tinkering around with my Discord bot, I couldn't seem to find a way to get the list of channels the bot was in without putting it in an EventEmitter. I'm rather confused as to why this wouldn't work, is there something that I'm missing?
Code:
const Discord = require("discord.js");
const client = new Discord.Client();
require('dotenv').config();
//this works
client.on('ready', ()=> {
const channelID = '803359668054786118';
const channel = client.channels.cache.get(channelID);
channel.send('working'); //this works
});
//this doesn't work
//intially tried using a wait function to see if the reason was because bot didn't have enough time to log on properly
setTimeout(function() {
const channelID = '803359668054786118';
const channel = client.channels.cache.get(channelID);
console.log(client.channels); //this is telling me that there's no channels in the collection...
//channel.send('working');
}, 500);
This is because the client is not logged in at that point of your code. At the compile stage of your file, the compiler runs through your code and compiles everything outside of event listeners. Once Client#login() is called, this is when the client has it's context. All of it's event listeners (ready, message ect.) are binded to the client.
In other words, the Discord client is not logged in when code outside of events being is compiled. Code inside events are executed once the client is logged in & the event itself is emitted.

How to make a ping-pong discord bot with websocket in Nodejs?

Im trying to make a simple ping-pong discord bot in Nodejs without any lib like discord.js in Nodejs
I think that using websockets with the rest api of discord is the best solution but I cant find any help or any code example to do it
I found this course : https://courses.cs.washington.edu/courses/cse154/17au/exploration/websockets/slides.html#/
with this "solution" that I can't understant : https://courses.cs.washington.edu/courses/cse154/17au/exploration/websockets/solution/
And this is an extract from the course :
const BOT_TOKEN = "xxx";
// Discord Gateway url
const GATEWAY_URL = "wss://gateway.discord.gg/?v=6&encoding=json";
// Websocket object
let ws = null;
connect();
// connect to gateway
function connect() {
ws = new WebSocket(GATEWAY_URL); // opens the websocket connection and creates WS object
ws.onmessage = messageHandler; // on message event
ws.onclose = connect; // reopen websockets when closed by discord
}
What I want to do can be simply done with this code using discord.js :
const Discord = require('discord.js');
const bot = new Discord.Client();
bot.on('ready', function () {
console.log("Connected !")
})
bot.on('message', message => {
if (message.content === 'ping') {
message.reply('pong !')
}
})
bot.login(token)
Any help please ?
I'm the one who provided this talk. While the lecture code does interact with Discord, it is not the most optimal method of doing so. Personally I would suggest using discord.js to create a bot that would respond to your ping.
For reference, the lecture I demonstrated in class is to showcase the functionality of websockets. It is executed within the browser and not with the help of node.js.
Under the hood, discord.js handles connection to Discord websocket gateway well. It responds to things like heartbeats and make sure Discord gateway does not boot the client from inactivity. The code I have demonstrated in lecture can only run for about 30 seconds at a time before being booted from Discord for inactivity.
However, if you are curious about the lecture, the session from Spring 18 is recorded live and can be viewable here.
I hope this help!

Skype Bot responding with empty body

I'm trying to get a Skype bot up and running based off of the echo example but I'm struggling to make a successful POST to my app. When I send a post to /v1/chat I get back a status of 201 (successful creation), and nothing in the body. My console.log does not print anything either, which leads me to believe that the botService.on('personalMessage', ...) function is not being run. Does anyone have any insight into how these POST requests should be formatted? I cannot seem to find anything in the documentation.
My code:
const fs = require('fs');
const restify = require('restify');
const skype = require('skype-sdk');
const botService = new skype.BotService({
messaging: {
botId: '28:<bot’s id="ID176db9ab-e313-4d76-a60c-bc2a280e9825">',
serverUrl : "https://apis.skype.com",
requestTimeout : 15000,
appId: process.env.APP_ID,
appSecret: process.env.APP_SECRET
}
});
botService.on('contactAdded', (bot, data) => {
console.log('contact added');
bot.reply('Hello ${data.fromDisplayName}!', true);
});
botService.on('personalMessage', (bot, data) => {
console.log('message incoming');
console.log(data);
bot.reply('Hey ${data.from}. Thank you for your message: "${data.content}".', true);
});
const server = restify.createServer();
server.post('/v1/chat', skype.messagingHandler(botService));
const port = process.env.PORT || 8080;
server.listen(port);
console.log('Listening for incoming requests on port ' + port);
Final Edit & Solution: I think the problem caused by Heroku somehow(it could be something with their free tier ,1 dyno). After some effort, I uploaded the program to Azure, and it is now working perfectly.
Potential Solution: You need to change the server address in the server.post command. If you run your program in "https:\www.yourwebsite.com/v1/chat" , you need to modify this;
server.post('/v1/chat', skype.messagingHandler(botService));
to this;
server.post('https:\\www.yourwebsite.com/v1/chat', skype.messagingHandler(botService));
Of course, don't forget to specify your app id, bot id, and app secret. If you don't have one, you need to generate a password in your Skype application page.
I have the exact problem with the OP. I followed the tutorial, and it doesn't specify how to modify our code to comply with our server. So, after running the program it only returns this;
{"code":"ResourceNotFound","message":"/ does not exist"}
In the Echo example in the Skype Bot Webpage; it says;
"We'll assume the bot's URL for messaging was set to https://echobot.azurewebsites.net/v1/chat during registration."
Make sure that Procfile and worker processes are setup.
My bot is working fine on heroku itself

Resources