Advertisement
Guest User

Untitled

a guest
Jan 28th, 2020
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 16.72 KB | None | 0 0
  1. const Discord=require('discord.js');
  2. const bot=new Discord.Client();
  3. const config=require('./config.json');
  4. const code=require('./code.json');
  5. const sql = require("sqlite");
  6. sql.open("./dataBase.sqlite");
  7.  
  8. // log our bot in
  9. bot.login(config.token);
  10.  
  11. bot.on('ready', () => {
  12. console.info(GetTimestamp()+'-- DISCORD ROLE BOT IS READY --');
  13.  
  14. // CREATE DATABASE TABLE IF NEEDED
  15. CreateDB()
  16. });
  17.  
  18. // ##########################################################################
  19. // ############################# SERVER LISTENER ############################
  20. // ##########################################################################
  21.  
  22. // DATABASE TIMER FOR TEMPORARY ROLES
  23. setInterval(function(){
  24. let timeNow=new Date().getTime();
  25. let dbTime="";
  26. let daysLeft="";
  27. let notify="";
  28.  
  29. sql.all(`SELECT * FROM temporary_roles`).then(rows => {
  30. if (!rows) {
  31. return console.info("No one is in the DataBase");
  32. }
  33. else {
  34. for(rowNumber="0"; rowNumber<rows.length; rowNumber++){
  35. dbTime=rows[rowNumber].endDate;
  36. notify=rows[rowNumber].notified;
  37. daysLeft=(dbTime*1)-(timeNow*1);
  38.  
  39. let rName=bot.guilds.get(config.serverID).roles.find(rName => rName.name === rows[rowNumber].temporaryRole);
  40. member=bot.guilds.get(config.serverID).members.get(rows[rowNumber].userID);
  41.  
  42. // CHECK IF THEIR ACCESS HAS EXPIRED
  43. if(daysLeft<1){
  44. if(!member){
  45. member.user.username="<@"+rows[rowNumber].userID+">"; member.id="";
  46. }
  47.  
  48. // REMOVE ROLE FROM MEMBER IN GUILD
  49. member.removeRole(rName).catch(console.error);
  50.  
  51. bot.channels.get(config.mainChannelID).send("⚠ "+member.user.username+" has **lost** their role of: **"
  52. +rows[rowNumber].temporaryRole+"** - their **temporary** access has __EXPIRED__ 😭 ").catch(console.error);
  53.  
  54. // REMOVE DATABASE ENTRY
  55. sql.get(`DELETE FROM temporary_roles WHERE userID="${rows[rowNumber].userID}"`).catch(console.error);
  56.  
  57. console.log(GetTimestamp()+"[ADMIN] [TEMPORARY-ROLE] \""+member.user.username+"\" ("+member.id+") have lost their role: "+rows[rowNumber].temporaryRole+"... time EXPIRED");
  58. }
  59.  
  60. // CHECK IF THEIR ONLY HAVE 5 DAYS LEFT
  61. if(daysLeft<432000000 && notify=="0"){
  62. if(!member){
  63. member.user.username="<@"+rows[rowNumber].userID+">"; member.id="";
  64. }
  65.  
  66. // NOTIFY THE USER IN DM THAT THEY WILL EXPIRE
  67. member.send("Hello "+member.user.username+"! Your role of **"+rows[rowNumber].temporaryRole+"** on "+bot.guilds.get(config.serverID)+" will be removed in less than 5 days. "
  68. +"If you would like to keep the role, please notify an admin. "
  69. +"You can use the `!help` command on the server for more information.").catch(error => {
  70. console.error(GetTimestamp()+"Failed to send a DM to user: "+member.id);
  71. });
  72.  
  73. // NOTIFY THE ADMINS OF THE PENDING EXPIRY
  74. bot.channels.get(config.mainChannelID).send("⚠ "+member.user.username+" will lose their role of: **"+rows[rowNumber].temporaryRole+"** in less than 5 days").catch(console.error);
  75.  
  76. // UPDATE THE DB TO REMEMBER THAT THEY WERE NOTIFIED
  77. sql.get(`UPDATE temporary_roles SET notified=1 WHERE userID="${rows[rowNumber].userID}"`);
  78.  
  79. console.log(GetTimestamp()+"[ADMIN] [TEMPORARY-ROLE] \""+member.user.username+"\" ("+member.id+") has been notified that they will lose their role in less than 5 days");
  80. }
  81. }
  82. }
  83. }).catch(console.error);
  84. //console.log(GetTimestamp()+"[ADMIN] Stored accounts checked for expiry and nofication.");
  85. },60000);
  86. // 86400000 = 1day
  87. // 3600000 = 1hr
  88. // 60000 = 1min
  89.  
  90. // ##########################################################################
  91. // ############################## TEXT MESSAGE ##############################
  92. // ##########################################################################
  93. bot.on('message', message => {
  94.  
  95. // MAKE SURE ITS A COMMAND
  96. if(!message.content.startsWith(config.cmdPrefix)){
  97. return
  98. }
  99.  
  100. //STOP SCRIPT IF DM/PM
  101. if(message.channel.type=="dm"){
  102. return
  103. }
  104.  
  105. // GET CHANNEL INFO
  106. let g=message.guild;
  107. let c=message.channel;
  108. let m=message.member;
  109. let msg=message.content;
  110. msg=msg.toLowerCase();
  111.  
  112. // GET TAGGED USER
  113. let mentioned="";
  114. if(message.mentions.users.first()){
  115. mentioned=message.mentions.users.first();
  116. }
  117.  
  118. // REMOVE LETTER CASE (MAKE ALL LOWERCASE)
  119. let command=msg.toLowerCase();
  120. command=command.split(" ")[0];
  121. command=command.slice(config.cmdPrefix.length);
  122.  
  123. // GET ARGUMENTS
  124. let args=msg.split(" ").slice(1);
  125. skip="no";
  126.  
  127. // GET ROLES FROM CONFIG
  128. let AdminR=g.roles.find(role => role.name === config.adminRoleName);
  129. if(!AdminR){
  130. AdminR={"id":"111111111111111111"};
  131. console.info("[ERROR] [CONFIG] I could not find admin role: "+config.adminRoleName);
  132. }
  133. let ModR=g.roles.find(role => role.name === config.modRoleName);
  134. if(!ModR){
  135. ModR={"id":"111111111111111111"};
  136. console.info("[ERROR] [CONFIG] I could not find mod role: "+config.modRoleName);
  137. }
  138.  
  139. // ############################################################################
  140. // ################################ COMMANDS ##################################
  141. // ############################################################################
  142.  
  143.  
  144. // ######################### COMMANDS/HELP ###########################
  145. if(command==="commands" || command==="help") {
  146. if(args[0]==="mods") {
  147. if(m.roles.has(ModR.id) || m.roles.has(AdminR.id)) {
  148. cmds="`!temprole @mention <DAYS> <ROLE-NAME>` \\\u00BB to assign a temporary roles\n"
  149. +"`!temprole check @mention` \\\u00BB to check the time left on a temporary role assignment\n"
  150. +"`!temprole remove @mention` \\\u00BB to remove a temporary role assignment\n"
  151. +"`!temprole add @mention <DAYS>` \\\u00BB to add more time to a temporary role assignment\n";
  152. return c.send(cmds).catch(console.error);
  153. }
  154. else {
  155. return message.reply("you are **NOT** allowed to use this command! \ntry using: `!commads`").catch(console.error);
  156. }
  157. }
  158. if(!args[0]) {
  159. cmds="`!check` \\\u00BB to check the time left on your subscription\n";
  160. if(config.mapMain.enabled==="yes"){
  161. cmds+="`!map` \\\u00BB a link to our web map\n"
  162. }
  163. if(config.paypal.enabled==="yes"){
  164. cmds+="`!subscribe`/`!paypal` \\\u00BB for a link to our PayPal\n"
  165. }
  166. }
  167. return c.send(cmds).catch(console.error);
  168. }
  169.  
  170. // ######################### PAYPAL/SUBSCRIBE ########################
  171. if(command==="paypal" || command==="subscribe") {
  172. if(config.paypal.enabled==="yes"){
  173. let embedMSG={
  174. 'color': 0xFF0000,
  175. 'title': 'Click HERE to Subscribe',
  176. 'url': config.paypal.url,
  177. 'thumbnail': {'url': config.paypal.img},
  178. 'description': 'Thank you! \nYour support is greatly appreciated.'
  179. };
  180. return c.send({embed: embedMSG}).catch(console.error);
  181. }
  182. }
  183.  
  184. // ############################## TEMPORARY ROLES ##############################
  185. if(command.startsWith("temprole") || command==="tr" || command==="trole"){
  186.  
  187. // ROLES ARE CASE SENSITIVE TO RESET MESSAGE AND ARGUMENTS
  188. msg=message.content;
  189. args=msg.split(" ").slice(1);
  190.  
  191. if(m.roles.has(ModR.id) || m.roles.has(AdminR.id) || m.id===config.ownerID){
  192. if(!args[0]){
  193. return message.reply("syntax:\n `!temprole @mention <DAYS> <ROLE-NAME>`,\n or `!temprole remove @mention`\n or `!temprole check @mention`");
  194. }
  195. if(args[0] && !mentioned){
  196. return message.reply("please `@mention` a person you want me to give/remove `!temprole` to...");
  197. }
  198. if(!args[1] && mentioned){
  199. return message.reply("imcomplete data, please try: \n `!temprole @mention <DAYS> <ROLE-NAME>`,\n or `!temprole remove @mention`\n or `!temprole check @mention`");
  200. }
  201. else {
  202. let dateMultiplier=86400000;
  203. mentioned=message.mentions.members.first();
  204.  
  205. // CHECK DATABASE FOR ROLES
  206. if(args[0]==="check"){
  207. mentioned=message.mentions.members.first();
  208. sql.get(`SELECT * FROM temporary_roles WHERE userID="${mentioned.id}"`).then(row => {
  209. if(!row){
  210. return message.reply("⚠ [ERROR] "+mentioned.user.username+" is __NOT__ in the `DataBase`");
  211. }
  212. else {
  213. let startDateVal=new Date();
  214. startDateVal.setTime(row.startDate);
  215. startDateVal=(startDateVal.getMonth()+1)+"/"+startDateVal.getDate()+"/"+startDateVal.getFullYear();
  216.  
  217. let endDateVal=new Date();
  218. endDateVal.setTime(row.endDate);
  219.  
  220. finalDate=(endDateVal.getMonth()+1)+"/"+endDateVal.getDate()+"/"+endDateVal.getFullYear();
  221. return c.send("✅ "+mentioned.user.username+" will lose the role: **"+row.temporaryRole+"** on: `"+finalDate+"`! They were added on: `"+startDateVal+"`");
  222. }
  223. }).catch(console.error); return
  224. }
  225.  
  226. // REMOVE MEMBER FROM DATABASE
  227. if(args[0]==="remove"){
  228. mentioned=message.mentions.members.first();
  229. sql.get(`SELECT * FROM temporary_roles WHERE userID="${mentioned.id}"`).then(row => {
  230. if(!row){
  231. return c.send("⚠ [ERROR] "+mentioned.user.username+" is __NOT__ in the `DataBase`");
  232. }
  233. else {
  234. let theirRole=g.roles.find(theirRole => theirRole.name === row.temporaryRole);
  235. mentioned.removeRole(theirRole).catch(console.error);
  236. sql.get(`DELETE FROM temporary_roles WHERE userID="${mentioned.id}"`).then(row => {
  237. return c.send("⚠ "+mentioned.user.username+" has **lost** their role of: **"+theirRole.name+"** and has been removed from the `DataBase`");
  238. });
  239. }
  240. }).catch(console.error); return
  241. }
  242.  
  243. // ADD TIME TO A USER
  244. if(args[0]==="add"){
  245. if(args[1] && !mentioned){
  246. return message.reply("please `@mention` a person you want me to add time to...");
  247. }
  248. if(!args[2]){
  249. return message.reply("for how **many** days do you want "+mentioned.user.username+" to have to have this role?");
  250. }
  251. else {
  252. mentioned=message.mentions.members.first();
  253. sql.get(`SELECT * FROM temporary_roles WHERE userID="${mentioned.id}"`).then(row => {
  254. if(!row){
  255. return c.send("⚠ [ERROR] "+mentioned.user.username+" is __NOT__ in the `DataBase`");
  256. }
  257. else {
  258. let startDateVal=new Date();
  259. startDateVal.setTime(row.startDate);
  260. startDateVal=(startDateVal.getMonth()+1)+"/"+startDateVal.getDate()+"/"+startDateVal.getFullYear();
  261.  
  262. let endDateVal=new Date();
  263. let finalDate=(parseInt(row.endDate)+parseInt((args[2])*(dateMultiplier)));
  264.  
  265. sql.get(`UPDATE temporary_roles SET endDate="${finalDate}", notified=0 WHERE userID="${mentioned.id}"`).then(row => {
  266. endDateVal.setTime(finalDate);
  267. finalDate=(endDateVal.getMonth()+1)+"/"+endDateVal.getDate()+"/"+endDateVal.getFullYear();
  268. return c.send("✅ "+mentioned.user.username+" has had time added until: `"+finalDate+"`! They were added on: `"+startDateVal+"`");
  269. });
  270. }
  271. }).catch(console.error); return
  272. }
  273. }
  274.  
  275.  
  276. // CHECK AMOUNT OF DAYS WERE ADDED
  277. if(!args[1]){
  278. return message.reply("for how **many** days do you want "+mentioned.user.username+" to have to have this role?");
  279. }
  280.  
  281. if(!args[2]){
  282. return message.reply("what role do you want to assign to "+mentioned.user.username+"?");
  283. }
  284.  
  285. // ROLES WITH SPACES - NEW
  286. let daRoles="";
  287. if(!args[3]){
  288. daRoles=args[2]
  289. }else{
  290. daRoles="";
  291. for(var x=2;x<args.length;x++){
  292. daRoles+=args[x]+" ";
  293. }
  294. daRoles=daRoles.slice(0,-1);
  295. }
  296.  
  297. if(!parseInt(args[1])){
  298. return message.reply("Error: second value has to be **X** number of days, IE:\n`!"+command+" @"+mentioned.user.username+" 90 "+daRoles+"`");
  299. }
  300.  
  301. // CHECK ROLE EXIST
  302. let rName=g.roles.find(rName => rName.name === daRoles);
  303. if(!rName){
  304. return message.reply("I couldn't find such role, please check the spelling and try again.");
  305. }
  306.  
  307. // ADD MEMBER TO DATASE, AND ADD THE ROLE TO MEMBER
  308. sql.get(`SELECT * FROM temporary_roles WHERE userID="${mentioned.id}"`).then(row => {
  309. mentioned=message.mentions.members.first();
  310. if (!row) {
  311. let curDate=new Date().getTime();
  312. let finalDateDisplay=new Date();
  313. let finalDate=((args[1])*(dateMultiplier));
  314. finalDate=((curDate)+(finalDate));
  315. finalDateDisplay.setTime(finalDate);
  316. finalDateDisplay=(finalDateDisplay.getMonth()+1)+"/"+finalDateDisplay.getDate()+"/"+finalDateDisplay.getFullYear();
  317.  
  318. sql.run("INSERT INTO temporary_roles (userID, temporaryRole, startDate, endDate, addedBy, notified) VALUES (?, ?, ?, ?, ?, 0)",
  319. [mentioned.id, daRoles, curDate, finalDate, m.id]);
  320. let theirRole=g.roles.find(theirRole => theirRole.name === daRoles);
  321. mentioned.addRole(theirRole).catch(console.error);
  322. console.log(GetTimestamp()+"[ADMIN] [TEMPORARY-ROLE] \""+mentioned.user.username+"\" ("+mentioned.id+") was given role: "+daRoles+" by: "+m.user.username+" ("+m.id+")");
  323. return c.send("🎉 "+mentioned.user.username+" has been given a **temporary** role of: **"+daRoles+"**, enjoy! They will lose this role on: `"+finalDateDisplay+"`");
  324. }
  325. else {
  326. return message.reply("this user already has a **temporary** role... try using `!temprole remove @"+mentioned.user.username+"` if you want to **change** their role.");
  327. }
  328. }).catch(console.error);
  329. }
  330. }
  331. else {
  332. message.delete();
  333. return message.reply("you are **NOT** allowed to use this command!").catch(console.error);
  334. }
  335. }
  336.  
  337. // ############################## CHECK ##############################
  338. if(command==="check"){
  339.  
  340. let dateMultiplier=86400000;
  341.  
  342. // CHECK DATABASE FOR ROLES
  343. mentioned=m;
  344. sql.get(`SELECT * FROM temporary_roles WHERE userID="${mentioned.id}"`).then(row => {
  345. if(!row){
  346. return message.reply("⚠ [ERROR] "+mentioned+" is __NOT__ in my `DataBase`");
  347. }
  348. else {
  349. let startDateVal=new Date();
  350. startDateVal.setTime(row.startDate);
  351. startDateVal=(startDateVal.getMonth()+1)+"/"+startDateVal.getDate()+"/"+startDateVal.getFullYear();
  352.  
  353. let endDateVal=new Date();
  354. endDateVal.setTime(row.endDate);
  355.  
  356. finalDate=(endDateVal.getMonth()+1)+"/"+endDateVal.getDate()+"/"+endDateVal.getFullYear();
  357. return c.send("✅ You will lose the role: **"+row.temporaryRole+"** on: `"+finalDate+"`! The role was added on: `"+startDateVal+"`");
  358. }
  359. }).catch(console.error); return
  360. }
  361.  
  362. // ############################## test ##############################
  363. if(command==="use"){
  364. editedmessage = message.content.slice (4);
  365. if(args[1] === code.tokens.token1){
  366. return c.send("!tr" + m.id + "> 30 test").catch(console.error);
  367. return c.send(code.tokens.token1)
  368.  
  369. client.code [tokens] = {
  370. token1: "use"
  371. }
  372. fs.writeFile ("./code.json", JSON.stringify (client.code, null, 4), err => {
  373. if(err) throw err;
  374. c.send("written")
  375. });
  376. }
  377. //else if(args[0] === "test") {
  378. // return c.send("!tr <@" + m.id + "> 90 test").catch(console.error);
  379. //}else if(args[0] === "12test"){
  380. // return c.send("!tr <@" + m.id + "> 999999 test").catch(console.error);
  381. //}else{
  382. // return c.send("ERROR").catch(console.error);;
  383. //}
  384. }
  385.  
  386. // ############################## ADD ##############################
  387. if(command==="add"){
  388. editedmessage = message.content.slice (4);
  389.  
  390. }
  391.  
  392. // ######################### MAP ###################################
  393. if(command==="map") {
  394. if(config.mapMain.enabled==="yes"){
  395. return c.send("Our official webmap: \n<"+config.mapMain.url+">").catch(console.error);
  396. }
  397. }
  398. });
  399.  
  400. function GetTimestamp()
  401. {
  402. let now = new Date();
  403.  
  404. return "["+now.toLocaleString()+"]";
  405. }
  406.  
  407. function RestartBot(type)
  408. {
  409. if(type == 'manual'){ process.exit(1); }
  410. else{
  411. console.error(GetTimestamp()+"Unexpected error, bot stopping, likely websocket");
  412. process.exit(1);
  413. }
  414. return;
  415. }
  416.  
  417. function CreateDB()
  418. {
  419. // CREATE DATABASE TABLE
  420. sql.run("CREATE TABLE IF NOT EXISTS temporary_roles (userID TEXT, temporaryRole TEXT, startDate TEXT, endDate TEXT, addedBy TEXT, notified TEXT, code TEXT)").catch(console.error);
  421. return;
  422. }
  423.  
  424.  
  425. bot.on('error', function(err) {
  426. if(typeof err == 'object')
  427. {
  428. err = JSON.stringify(err);
  429. }
  430. console.error(GetTimestamp()+'Uncaught exception: '+err);
  431. RestartBot();
  432. return;
  433. });
  434.  
  435. process.on('uncaughtException', function(err) {
  436. if(typeof err == 'object')
  437. {
  438. err = JSON.stringify(err);
  439. }
  440. console.error(GetTimestamp()+'Uncaught exception: '+err);
  441. RestartBot();
  442. return;
  443. });
  444.  
  445. process.on('unhandledRejection', function(err) {
  446. if(typeof err == 'object')
  447. {
  448. err = JSON.stringify(err);
  449. }
  450. console.error(GetTimestamp()+'Uncaught exception: '+err);
  451. RestartBot();
  452. return;
  453. });
  454.  
  455. bot.on('disconnect', function(closed) {
  456. console.error(GetTimestamp()+'Disconnected from Discord');
  457. return;
  458. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement