From 625bbf1e6f1655a53aa3c7f0ddced2687aa29869 Mon Sep 17 00:00:00 2001 From: Junior Date: Fri, 22 Nov 2024 14:52:20 +0000 Subject: [PATCH] Change reporting online count periodically to setting bot presence information --- config/config-dist.js | 3 +-- index.js | 53 ++++++++++++++++++++++++--------------------------- 2 files changed, 26 insertions(+), 30 deletions(-) diff --git a/config/config-dist.js b/config/config-dist.js index 51d7e2f..541b26e 100644 --- a/config/config-dist.js +++ b/config/config-dist.js @@ -2,12 +2,11 @@ const config = { GUILD_THUMBNAIL: 'https://www.circlecraft.info/images/circlecraft_discord.png', BOT_TOKEN: 'YOUR_BOT_TOKEN_GOES_HERE', CHANNELS_ALLOW: ['123456789012345678', '123456789012345678'], - REPORT_CHANNEL: '1309262211654619188', MOD_ROLE: '123456789012345678', STAFF_ROLE: '123456789012345678', RESTART_ROLE: '123456789012345678' ALLOW_RESTART: false, - REPORT_INTERVAL: 0 + PRESENCE_INTERVAL: 5 } module.exports = config; diff --git a/index.js b/index.js index 58d3314..e2bd081 100644 --- a/index.js +++ b/index.js @@ -17,10 +17,11 @@ const client = new Client({ client.on('ready', () => { console.log(`Logged in as ${client.user.tag}!`); - var interval = Config.REPORT_INTERVAL * 60 * 1000; + var interval = Config.PRESENCE_INTERVAL * 60 * 1000; if ( interval != 0 ) { + getOnlineReport(); setInterval(function() { - doOnlineReport(channel); + getOnlineReport(); }, interval); } }); @@ -30,28 +31,7 @@ process.on('SIGINT', function() { process.exit(1); }); -function sendOnline(online, channel) { - let playerCount = 0; - let embed = new EmbedBuilder() - .setColor(0x44ff44) - .setThumbnail(Config.GUILD_THUMBNAIL); - for ( const s in online ) { - if ( !online[s].tested ) { - return; - } - if ( online[s].players.length > 0 ) { - playerCount += online[s].players.length; - embed.addFields({name: "**"+online[s].name+"**", value: online[s].players.join(', '), inline: false}); - } - }; - embed.setTitle("**Players Currently Online:** " + playerCount); - channel.send({embeds: [embed]}); -} - -async function doOnlineReport(channel = null) { - if ( channel === null ) { - const channel = await client.channels.fetch(Config.REPORT_CHANNEL); - } +function getOnlineReport(channel = null) { let online = []; Servers.forEach(function (server) { if ( server.active ) { @@ -68,7 +48,7 @@ async function doOnlineReport(channel = null) { online[server.slug].tested = true; online[server.slug].online = true; online[server.slug].players = response.players; - sendOnline(online, channel); + //sendOnline(online, channel); }) .then(() => { q.close(); }) .catch(e => { @@ -76,18 +56,35 @@ async function doOnlineReport(channel = null) { //console.log("slug: " + server.slug); //console.log(online); online[server.slug].tested = true; - sendOnline(online, channel); + //sendOnline(online, channel); }); } catch (e) { console.log(e); } }); + let playerCount = 0; + let embed = new EmbedBuilder() + .setColor(0x44ff44) + .setThumbnail(Config.GUILD_THUMBNAIL); + for ( const s in online ) { + if ( !online[s].tested ) { + continue; + } + if ( online[s].players.length > 0 ) { + playerCount += online[s].players.length; + embed.addFields({name: "**"+online[s].name+"**", value: online[s].players.join(', '), inline: false}); + } + }; + embed.setTitle("**Players Currently Online:** " + playerCount); + if ( channel !== null ) channel.send({embeds: [embed]}); + if ( Config.PRESENCE_INTERVAL != 0 ) { + client.user.setPresence({ activities: [{ name: "Online: " + playerCount }], status: 'idle' }); + } } client.on('messageCreate', (msg) => { if ( msg.author.id === client.user.id ) return; if ( msg.content.startsWith(">>paddle") ) { - console.log("Trying to paddle"); if ( msg.member.roles.cache.has(Config.MOD_ROLE) || msg.member.roles.cache.has(Config.STAFF_ROLE) ) { let response = ""; msg.mentions.users.forEach(function (user) { @@ -111,7 +108,7 @@ client.on('messageCreate', (msg) => { } if ( msg.content == ">>online" && Config.CHANNELS_ALLOW.includes(msg.channel.id) ) { - doOnlineReport(msg.channel); + getOnlineReport(msg.channel); } if ( msg.content == ">>server" && Config.CHANNELS_ALLOW.includes(msg.channel.id) ) {