'Player Login/ Logout Feed for Discord
I'm currently working on a Discord Bot for my game server and I would like to implement a Player Login/Logout feed. I have set up a SQL database where it stores information about a player, I have created a Table name called 'users' and in this table I have a column called 'presence' this has a default value called 'offline' and switches to 'online' when a player has joined the server.
My goal here is to have the Bot send a message only when the column 'presence' changes from 'offline' to 'online' and also from 'online' to 'offline'. enter image description here
So far I'm able to get the bot to send a message when the bot starts about a players presence, my question is how to make it 'listen' for when a player presence has changed and then send a message?.
I hope this makes sense and thanks in advance.
main.js
setInterval(() => {
PlayerFeed.publish(client);
}, 30000);
});
PlayerFeed.js
const { Users } = require("../dbObjects");
const { Sequelize } = require("sequelize");
const { codeBlock } = require("@discordjs/builders");
module.exports = {
publish: (client) => {
const channel = client.guilds.cache.get(Config.guildId).channels.cache.get(Config.PlayerFeedChannel)
Users.findAll({where: {presence: 'online'}, order: ['presenceUpdatedAt']}).then(users => {
users.forEach(Users => {
let presence = Users.presence;
if(presence == 'online')
channel.send(codeBlock("diff",
`+Login ID:${Users.steamId64} ID:${Users.ign}`));
else if
(presence == 'offline')
channel.send(codeBlock("diff",
`-Logout ID:${Users.steamId64} ID:${Users.ign}`));
})
})
}
}
Solution 1:[1]
So, if I understand well, "presence" is set to "online" when user join your guild and it set to "offline" if he leaves it right?
If so, you should use Discord Events to manage this.
on guildMemberAdd => add user to db with presence online => send message in your feed
on guildMemberRemove => set presence to offline => send message in your feed
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | aymcg31 |
