Advertisement
tusKOr661

Galaxy V1 (NodeJS)

Apr 27th, 2022 (edited)
1,719
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // @Bot Version 1
  2. // Procedurally coded bot in a true scripting format with a basic permission-based rank system for command authority
  3. //
  4.  
  5. /*
  6. 1)
  7.     Check `ls`
  8.     Should NOT see `Desktop`, `Documents`, `Downloads`, etc
  9.     If you DO see these folders
  10.         1.5) Use `cd Desktop`
  11.  
  12. 2)
  13.     Use `nano bot.js` to create the file
  14.     Input the required source code
  15.     Then exit and save the file following the prompts at the bottom of the terminal
  16.  
  17. 3)
  18.     Install the required modules:
  19.         `npm install discord.js`
  20.         `npm install express`
  21.         `npm install mysql`
  22.    
  23. 4)
  24.     Run the bot
  25.     `pm2 start bot.js`
  26.    
  27. 5)
  28.     Monitor for errors
  29.     `pm2 monit`
  30. */
  31.  
  32. const TOKEN = "Enter token here";
  33. const MYSQL_USERNAME = "Enter username here";
  34. const MYSQL_PASSWORD = "Enter password here";
  35.  
  36. const Galaxy = {
  37.     "Roles": {
  38.         "Root": {
  39.             "Rank": 3,
  40.             "Permissions": [
  41.                 {"type": "other", "value": "OWNER"},
  42.                 {"type": "other", "value": "CREATOR"}
  43.             ],
  44.         },
  45.        
  46.         "Administrator": {
  47.             "Rank": 2,
  48.             "Permissions": [
  49.                 {"type": "permission", "value": "ADMINISTRATOR"}
  50.             ],
  51.         },
  52.        
  53.         "Moderator": {
  54.             "Rank": 1,
  55.             "Permissions": [
  56.                 {"type": "permission", "value": "KICK_MEMBERS"},
  57.                 {"type": "permission", "value": "BAN_MEMBERS"}
  58.             ]
  59.         },
  60.        
  61.         "User": {
  62.             "Rank": 0,
  63.             "Permissions": [
  64.                 {"type": "permission", "value": "SEND_MESSAGES"}
  65.             ]
  66.         }
  67.     },
  68.    
  69.     "Settings": {
  70.         "Prefix": ";",
  71.         "CommandErrors": {
  72.             "NotifyRankExceeds": false, //Notify the speaker if their rank is exceeded by command's requirement
  73.             "NotifyGeneralError": true, //Notify the speaker of command execution errors
  74.             "NotifyParsedError": true //Notify the speaker of passed errors thrown by Error("Galaxy: error message here")
  75.         }
  76.     },
  77.    
  78.     "Commands": {},
  79. }
  80.  
  81. function createCommand(name, rank, list, description, callback){
  82.     Galaxy.Commands[name] = {rank, list, description, callback}
  83. };
  84.  
  85. function writeErrorMessage(message, channel, member) {
  86.     return channel.send({
  87.         content: `<\x40${member.id}>, `,
  88.         embeds: [
  89.             {
  90.                 title: "Error",
  91.                 description: message,
  92.                 color: 0xFF0000,
  93.                 footer: {
  94.                    
  95.                 }
  96.             }
  97.         ]
  98.     });
  99. };
  100.  
  101. function onChatted(message){
  102.     var guild = message.guild,
  103.         author = message.author,
  104.         member = message.member,
  105.         channel = message.channel;
  106.  
  107.     if (!message.content.startsWith(Galaxy.Settings.Prefix) || author.bot)
  108.         return;
  109.    
  110.     var content = message.content.substring(Galaxy.Settings.Prefix.length);
  111.     var match = content.match(/(\S+)\s*(.*)/);
  112.     var role = getRole(member, guild);
  113.    
  114.     for (var name in Galaxy.Commands) {
  115.         let command = Galaxy.Commands[name];
  116.        
  117.         for (var i in command.list) {
  118.             let identifier = command.list[i];
  119.  
  120.             if (match[1].toLowerCase() == identifier.toLowerCase()) {
  121.                 if (role.rank < command.rank) {
  122.                     if (Galaxy.Settings.CommandErrors.NotifyRankExceeds)
  123.                         writeErrorMessage("You do not have the required rank for this command!", channel, member);
  124.                    
  125.                     return;
  126.                 };
  127.                
  128.                 try {
  129.                     let rtn = command.callback(match[2], member, channel, guild, message);
  130.                     if (rtn instanceof Promise) {
  131.                         rtn.catch(error => {
  132.                             let errorString = error.toString();
  133.                             if (errorString.startsWith("[Galaxy]: ")) {
  134.                                 if (Galaxy.Settings.CommandErrors.NotifyParsedError)
  135.                                     writeErrorMessage(errorString, channel, member);
  136.                             } else if (Galaxy.Settings.CommandErrors.NotifyGeneralError)
  137.                                 writeErrorMessage(errorString, channel, member);
  138.                         });
  139.                     };
  140.                 } catch (error) {
  141.                     let errorString = error.toString();
  142.                     if (errorString.startsWith("[Galaxy]: ")) {
  143.                         if (Galaxy.Settings.CommandErrors.NotifyParsedError)
  144.                             writeErrorMessage(errorString, channel, member);
  145.                     } else if (Galaxy.Settings.CommandErrors.NotifyGeneralError)
  146.                         writeErrorMessage(errorString, channel, member);
  147.                 };
  148.             };
  149.         };
  150.     };
  151. };
  152.  
  153. function getRole(user, guild){
  154.     for (var name in Galaxy.Roles) {
  155.         let role = Galaxy.Roles[name];
  156.         for (var i in role.Permissions) {
  157.             let permission = role.Permissions[i];
  158.             let pass = false;
  159.            
  160.             if (permission.type == "other") {
  161.                 if (permission.value == "CREATOR") {
  162.                     pass = user.id == "";
  163.                 } else if (permission.value == "OWNER") {
  164.                     pass = user.id == guild.ownerId;
  165.                 }
  166.             } else {
  167.                 pass = user.permissions.has(permission.value);
  168.             };
  169.            
  170.             if (pass)
  171.                 return ({
  172.                     "name": name,
  173.                     "rank": role.Rank
  174.                 });
  175.         };
  176.     };
  177.    
  178.     return ({
  179.         "name": "User",
  180.         "rank": 0
  181.     });
  182. };
  183.  
  184. const mysql = require('mysql');
  185. const sql = mysql.createPool ({
  186.     connectionLimit : 50,  
  187.     database: "db0",
  188.     host: "localhost",
  189.     user: MYSQL_USERNAME,
  190.     password: MYSQL_PASSWORD
  191. });
  192.  
  193. function getString(id){
  194.     return new Promise(function(a, b){
  195.         sql.query('select * from strings where id = ?', [id], function(err, results){
  196.             if (results[0])
  197.                 a(results[0].str);
  198.             else
  199.                 a();
  200.         });
  201.     });
  202. };
  203.  
  204. function setString(id, string){
  205.     return new Promise(async function(a, b){
  206.         let result = await getString(id);
  207.         if (!result) {
  208.             sql.query('insert into strings(id, str) values(?, ?)', [id, string], function(err, results){ a() });
  209.         } else {
  210.             sql.query('update strings set str = ? where id = ?', [string, id], function(err, results){ a() });
  211.         };
  212.     });
  213. };
  214.  
  215. // Command initialisation
  216.  
  217. createCommand("Ping", 0, ["ping"], "Pings you", function(message, speaker, channel) {
  218.     channel.send({
  219.         content: `<\x40${speaker.id}>, ` + (message.length == 0 ? "Pong": message)
  220.     });
  221. });
  222.  
  223. createCommand("PrintString", 2, ["printstring", "getstring"], "Get $users string", async function(message, speaker, channel, guild){
  224.     let id = message.match(/(\d+)/)[1];
  225.     if (id != null) {
  226.         let member = await guild.members.fetch(id);
  227.         if (member != null) {
  228.             channel.send(`<\x40${speaker.id}>, <\x40${member.id}>'s string is [${await getString(member.id)}]!`);
  229.         } else {
  230.             throw("Galaxy: cannot find member!");
  231.         };
  232.     } else
  233.         throw("Galaxy: no member specified!");
  234. });
  235.  
  236. createCommand("SetString", 3, ["setstring"], "Set $user's string to $message", async function(message, speaker, channel, guild){
  237.     let split = message.match(/^(\S+)\s+(.+)/);
  238.     if (!split) {
  239.         throw("Galaxy: incorrect command input");  
  240.     } else {
  241.         let id = split[1].match(/(\d+)/)[1];
  242.         if (id != null) {
  243.             let member = await guild.members.fetch(id);
  244.             if (member != null) {
  245.                 setString(member.id, split[2]);
  246.                 channel.send(`<\x40${speaker.id}>, <\x40${member.id}>'s string is set to [${await getString(member.id)}]!`);
  247.             } else {
  248.                 throw("Galaxy: cannot find member!");
  249.             };
  250.         } else
  251.             throw("Galaxy: no member specified!");
  252.     };
  253. });
  254.  
  255. // Bot initialisation
  256.  
  257. const { Client, Intents } = require('discord.js');
  258. const client = new Client({ intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES] });
  259.  
  260. client.on("messageCreate", onChatted);
  261. client.login(TOKEN);
  262.  
  263. // Website initialisation
  264.  
  265. const express = require('express')
  266. const app = express()
  267.  
  268. app.get('/', function (req, res) {
  269.  res.send('Hello World')
  270. })
  271.  
  272. app.listen(3000)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement