From 661d08c6b29853ba6b48edfcc8ecc7591baad084 Mon Sep 17 00:00:00 2001 From: Aceheliflyer Date: Sun, 8 Apr 2018 15:48:48 -0400 Subject: [PATCH] Feature: Proper game rotation and selection. --- src/config/gameConfig.yml | 18 ++++++++++++ src/events/client/ready.js | 60 +++++++++++++++++++++++++------------- 2 files changed, 57 insertions(+), 21 deletions(-) create mode 100644 src/config/gameConfig.yml diff --git a/src/config/gameConfig.yml b/src/config/gameConfig.yml new file mode 100644 index 0000000..e2f13e1 --- /dev/null +++ b/src/config/gameConfig.yml @@ -0,0 +1,18 @@ +--- +# http://discord.js.org/#/docs/main/master/typedef/PresenceData +enabled: false +rotate: false # When false, selects first game. +rotateTime: 150000 # Rotate speed in miliseconds. (This still applies even if rotate is disabled.) + +# WARNING: This uses eval to work, careful what you set each value at. +# If using STREAMING type, you must supply a proper Twitch link. +games: + - activityName: "${client.options.commandPrefix}help | ${client.guilds.size.toLocaleString()} ${pluralize('Server', client.guilds.size)} | ${client.users.size.toLocaleString()} ${pluralize('User', client.users.size)} ${client.shard ? ` | Shard ${client.shard.id}` : ''}" + status: 'online' + + - activityName: '${client.options.commandPrefix}help' + activityType: 'LISTENING' # Listening to + status: 'idle' + + - activityName: '${client.options.commandPrefix}stats to view stats.' + status: 'dnd' diff --git a/src/events/client/ready.js b/src/events/client/ready.js index 20ff30f..4f8e611 100644 --- a/src/events/client/ready.js +++ b/src/events/client/ready.js @@ -4,27 +4,20 @@ const { oneLine } = require('common-tags') module.exports = async (client) => { await client.log('success', `Logged in as ${client.user.tag} (${client.user.id}).`, 'Discord', 'Login') - await setTimeout(() => { - client.user.setStatus(client.provider.get('global', 'clientStatus', 'online')).then( - client.user.setActivity(oneLine` - ${client.options.commandPrefix}help | - ${pluralize('Server', client.guilds.size.toLocaleString(), true)} | - ${pluralize('User', client.users.size.toLocaleString(), true)} - ${client.shard ? ` | Shard ${client.shard.id}` : ''} - `) - ) - }, 1000) - - await setInterval(() => { - client.user.setStatus(client.provider.get('global', 'clientStatus', 'online')).then( - client.user.setActivity(oneLine` - ${client.options.commandPrefix}help | - ${pluralize('Server', client.guilds.size.toLocaleString(), true)} | - ${pluralize('User', client.users.size.toLocaleString(), true)} - ${client.shard ? ` | Shard ${client.shard.id}` : ''} - `) - ) - }, 600000) + if (client.config.gameConfig.enabled === true) { + var games = client.config.gameConfig.games + if (client.config.gameConfig.rotate === false) { + client.user.setPresence(await calcPresence(client, games[0])) + setInterval(async () => { + client.user.setPresence(await calcPresence(client, games[0])) + }, client.config.gameConfig.rotateTime) + } else { + client.user.setPresence(await calcPresence(client, games[Math.floor(Math.random() * games.length)])) + setInterval(async () => { + client.user.setPresence(await calcPresence(client, games[Math.floor(Math.random() * games.length)])) + }, client.config.gameConfig.rotateTime) + } + } await client.log('info', oneLine` ${client.shard ? `Shard ${client.shard.id} ready!` : 'Client Ready!'} @@ -84,3 +77,28 @@ module.exports = async (client) => { } }) } + +/** + * Dynamic presence hander. + * @param {object} game The object of the presence data. + */ +var calcPresence = (client, game) => { + var presence = {}; presence.activity = {} + // Presence Status + if (game.status) { + presence.status = game.status + } + // Activity Name + if (game.activityName) { + presence.activity.name = eval('`' + game.activityName + '`') // eslint-disable-line no-eval + } + // Activity Type + if (game.activityType) { + presence.activity.type = game.activityType + } + // Activity URL + if (game.activityURL) { + presence.activity.url = eval('`' + game.activityURL + '`') // eslint-disable-line no-eval + } + return presence +}