[Discord Bot] Playtime Tracking

Playtime Tracking Script

Overview

This script provides real-time tracking and synchronization of player playtime for FiveM servers.


Features

  • Real-time Playtime Tracking: Updates playtime for each connected player every minute.
  • MySQL Integration: Ensures persistent storage of player playtime data.
  • Dynamic Sync: Periodically fetches all playtime data and sends it to the JavaScript side for advanced usage.
  • Reset Functionality:
    • Reset playtime for a specific player by their license.
    • Reset playtime for all players in the database.
  • Compatibility: Fully compatible with frameworks and standalone setups.

Installation

  1. Download and Extract
  • Download the script files and place them in your server’s resources folder.
  • Make sure to extract the node_modules folder included in the zip file to ensure the bot dependencies are installed.
  1. Configure Database
  • Ensure you have a MySQL database set up.
  • Create a table for playtime using the following schema:
CREATE TABLE `playtime` (
    `license` VARCHAR(255) NOT NULL PRIMARY KEY,
    `name` VARCHAR(255) NOT NULL,
    `playtime` INT NOT NULL DEFAULT 0
);
  1. Add to Server Config
  • Add the resource to your server.cfg :
ensure xdc-playtimetracker
  1. Start Your Server
  • Restart your server to load the resource.

Events and Usage

Discord Commands

  1. Check Playtime
  • Use the /playtime check command with the player’s license to view their playtime details.
  1. Reset Playtime for Specific License
  • Use the /playtime reset [license] command with the player’s license to clear their playtime.
  1. Reset All Playtimes
  • Use the /playtime reset command without a license to clear all stored playtimes.
  1. Dynamic Embeds
  • Playtime details are displayed in rich embeds with player information, license, and minutes played.

Notes

  • Performance Optimized: Efficient loops and database queries ensure minimal impact on server performance.
  • Extensible: Easily integrate with Discord bots.
  • Database Indexing: Ensure the license column is indexed for optimal query performance.

Download

For questions, bugs, or suggestions, feel free to reach out on the Discord.

4 Likes

Some feedback on your code.

Firstly, having both RegisterServerEvent and AddEventHandler means an event can be triggered from the client as well as the server, meaning that your updatePlaytime could triggered by any client. This is problematic because not only can they change their own playtime, but the playtime of others, as you pass a license parameter. I would suggest changing this event to being a function, as it is only called from within this one server file.

Secondly, you have an event called syncPlaytimeToClient that sends a client event called receivePlaytime to all clients, even though you do not have a client script. Consider removing this.

Thirdly, your second thread (or loop) is querying the database every half a second to fetch player data, this is far too often. I would suggest querying the database once when the bot starts (assuming you want to cache the playtime of existing players), and after that, have the Discord Bot query the database to get the playtime of a specific license; you don’t need to go via the FiveM code, can use an npm package like MySQL2 to execute database queries directly from your Discord Bot code.

Finally, as with the first point I raised, both resetPlaytimeForLicense and resetAllPlaytimes can be triggered by any client, I would suggest making these server-only events, or even better, do as I suggested above regarding executing database queries directly from the Discord Bot, and then move these two queries from the FiveM code to the Discord Bot code, and remove the need for the events altogether.

Hope that helps.

sql is annoying, set it up to just use json

really appreciate ur help buddy !

1 Like