A Discord bot that tracks songs played, and allow user interaction with that data.
This is assuming you know your way around a NodeJS project, and can set up a Discord bot token and whatnot.
Here's what you need:
.envfile in root directory: see.env-example. Populate with approperiate content and rename it to.env.- Naturally, you need to run a local instance of MongoDB if you're not using a remote connection (as specified in
.envfile). - Congifure
/data/config.jsonto your liking:ownerUIDis bot owner Discord UID.prefixis the bots prefix.minimum_playtimeis time (seconds) required to play in order for the song to be recorded in to database. If0, then all songs will be.songFeed -> guildis your guild ID to post song feed in.songFeed -> channelis the channel inside your guild to post song feed in.songFeedEnabledtrueto enable song feed — Required that you set up channel and guild ID (that above). Set tofalseto disable.
- Configure
/data/permissions.json:fallback_datais permissions that will be checked if you have none in your database. If match in database, database permission will be used.- A permission block consist of a permission level, description (optional), and a a users ID:
"<USER ID>": { "permission":<LEVEL>, "desc":"<DESCRIPTION>" },ACCESSis the permissions used by bot when checking permission to execute commands.Rolesis used when referencing a permission, e.g. in commands to change permissions.- Permissions are bitfields.
- Database structure:
- Users collection:
- Contains information on users, and used as whitelist for users to track.
- A users document consist of:
_id: String, // user ID username: String, // User's username (not tag) aliases: [String], // Known aliases, including current/past usernames playtime: Number, // # seconds of music played in total tracking: Boolean, // Enable/disable tracking of user's songs, playtime, etc., permission: Int32 /* Permission is optional. Int32 is a Mongoose plugin `mongoose-int32` entity. Basically a type of number. Used as permission override to default: ACCESS.user and/or content in permissions file */
- Belongs to a collection by the name
users.
- Songs collection:
- The collection containing played all songs.
- The structure/object is referred to as
SongSchema - A SongSchema document consist of:
artists: [String], // One or more contributing artists song: String, // Song title image: String, // "Spotify:<image ID>", used as: https://i.scdn.co/image/<image ID> start: Date, // Date song started playing end: Date, // Date song ended playing user_id: String, // A users UID user_name: String // A users username (not tag)
- Belongs to a collection by the name
songsPlayed. Done.
- Users collection:
This assumes you do not know jack about coding/setting up bots, etc., but want to run this bot on your computer or whatever. You need four things: NodeJS, MongoDB, bot token, and set up the bot. Follow the steps next sections.
- Install MongoDB on your computer. See MongoDB's guides for Windows, Mac, or Linux.
- Select "complete" package.
- You want to install it as a service, and just leave stuff default in the installation wizard — unless you know what you're changing.
- If asked about installing "Compass" too, it's a "GUI" to connect and interact with your Database, which you may want — else you'll have to rely on CLI.
- After installation, ensure MongoDB is running and you can connect. Test this by opening your Command prompt/terminal and type
mongo.- If it says "mongo is not recognized (...)" (windows) or "mongo: command not found" (Mac/Linux), try restarting computer. If that didn't work, try re-installing and/or doublecheck you followed MongoDB's steps properly.
- If it says it cannot connect, it's likely due to the server is not running. Start with the command
mongodin Command prompt/Terminal, and open a new Command prompt/Terminal and use themongocommand to connect to it. CTRL+C / CMD+C to exit/stop either.
- Go to NodeJS download and install the "LTS" version for your operative system.
- Go through the installation. Leave defaults.
- After installation, ensure NodeJS is running and you can use it. Test this by opening your Command prompt/terminal and type
node -v, which should output version installed.
- Click the green "Clone or download" button at the top (go to index if you don't see it) — click "Download as ZIP".
- Extract and put the folder where you want to keep the bot. You can stop bot and move the folder location later without issues if you have to.
- Navigate in to the folder, where the
bot.jsfile is - If you don't see any
.env-examplefile, try enabling hidden files:- Windows: In file explorer, click "Folder" (or equivilent in your language) at the top, then "Show hidden files" under the "Show/hide" section.
- Mac: In Finder, press CMD+Shift+Dot to toggle hidden files visibility
- Rename
.env-exampleto.env - Open the
.envfile with a text editor - Replace
your_bot_token_herewith the bot token you get from the next section, and save. The rest can stay as it is. - Navigate to the
datafolder, openconfig.jsonwith a text editor. - Go through each setting and replace with equivilent content. Be careful that text fields require to have the
"on both sides. Config explained:ownerUIDis your Discord user ID. How to get Discord UID.prefixis the bots prefix. Default is!!, change it if you want to.minimum_playtimeis time (seconds) required to play in order for the song to be recorded in to database. If0, then all songs will be.skippedis time (seconds) required to play in order for the bot to bother posting the song in the feeds channel (if you enable that).songFeed -> guildis your guild ID to post song feed in. How to get Discord UID.songFeed -> channelis the channel ID, inside your guild to post song feed in. How to get Discord UID.songFeedEnabledtrueto enable song feed — Required that you set up channel and guild ID (that above). Set tofalseto disable.- Save changes.
- In the same folder, open the
permissions.jsonfile with your text editor, and change the content accordingly:fallback_datais permissions that will be checked if you have none/no overrides in your database.- If match in database, database permission will be used.
- A permission block consist of a permission level, description (optional), and a users ID:
"<USER ID>": { "permission":<LEVEL>, "desc":"<DESCRIPTION>" },- You want to set yourself up here, with the permission level
28. ACCESSis the permissions used by bot when checking permission to execute commands.Rolesis used when referencing a permission, e.g. in commands to change permissions.- Permissions are exact. A
vipneed theuserpermission level too, in order to useuserlevel commands — so:user + vipis4 + 8, which is12.
- Go to the Discord Developer portal — log in with your Discord if you have to
- In the "Applications" panel, select "New Application"
- Name your Application (App name/avatar won't be the bot name, nor avatar), and leave "Team" as your account unless, unless you know what a team is and how to use it.
- In your newly created application, navigate to "Bot" tab.
- Create new bot. Now you will pick a bot name. This name will be the name of the bot, but you can change it later too if you want. In Bot you also pick the bot's avatar.
- Once created, click "Copy" button by the "Token" part, put it in to the
.envfile as described in previous section. Keep it private and secure, this is used to log in to your bot. Example how the line will look:BOT_TOKEN=287vnz8fvgbankm3.d8asy2nkem1023b.dn39dbafgnkj381 - Invite the bot to your guild:
- In the Discord Developer portal, go to OAuth2 section
- In the "Scope" switchboard, check "bot". Check necessary permissions:
- "Administrator" alone is enough if you're admin and lazy
- If not, I recommend: send messages, embed links, attach files, read message history, manage messages, add reactions
- When done, copy the generated link between the "Scopes" and "Bot permissions" switchboards. Paste it in to browser to initiate bot invite. Select your guild.
By now you should be good to go:
Open command prompt/terminal, write node (space included), drag and drop the bot.js file in to the terminal to make it generate a path to the file, then click enter. Your bot should now start up and be ready for use.
To stop, press CTRL+C / CMD+C in the command prompt/terminal. To start it again, you can use arrow up and hit enter, or write "node " again and drop in the file.