· 5 years ago · Jan 06, 2021, 07:00 PM
1const Discord = require("discord.js");
2const client = new Discord.Client();
3const fs = require("fs");
4const https = require("https");
5const express = require("express");
6const app = express();
7
8// Credentials stored as environment variables for security & ease of changing
9// Unity API key is in your publisher dashboard
10const discord_token = process.env.TOKEN; // Discord API token
11const uashost = process.env.UASHOST; // Asset Store API Domain (currently api.assetstore.unity3d.com)
12const uaspath = process.env.UASPATH; // Asset Store API Endpoint, including credentials - this one is pretty hacky, you may want to fix it but for using as-is it's formatted like this: "/publisher/v1/invoice/verify.json?key=[REPLACE THIS WITH YOUR UNITY API]&invoice="
13
14const assetnames = ["LipSync", "LipSync Pro", "Eye Controller"]; // Obviously put your own asset names in here
15
16client.on("ready", () => {
17 console.log("Connected!");
18});
19
20client.on("message", message => {
21 if (message.author.bot) return;
22
23 if (message.channel.type != "dm") return;
24
25 var guild = client.guilds.cache.get("0000000000000000"); // This should be the ID of your Discord Server
26
27 try {
28 var memberPromise = guild.members.fetch(message.author.id);
29
30 // I used "Verified" as the role name, you may want to use something different
31 memberPromise.then(member => {
32 if (member.roles.cache.find(r => r.name === "Verified")) {
33 message.channel.send(
34 "I'd love to help you, but you've already been verified - that's as useful as I get!"
35 );
36 return;
37 }
38
39 if (isNaN(message.cleanContent)) {
40 message.channel.send(
41 "That's not a number! Send me just the numerical section of your order number and I'll try and process it."
42 );
43 } else {
44 console.log(message.cleanContent)
45 var options = {
46 host: uashost,
47 path: uaspath + message.cleanContent,
48 method: "GET"
49 };
50
51 console.log(uashost + uaspath + message.cleanContent);
52
53 var req = https.request(options, res => {
54 console.log(`STATUS: ${res.statusCode}`);
55 res.setEncoding("utf8");
56 res.on("data", chunk => {
57 var data = JSON.parse(chunk);
58 if (Array.isArray(data.invoices) && data.invoices.length > 0) {
59 var i;
60 for (i = 0; i < data.invoices.length; i++) {
61 if (assetnames.includes(data.invoices[i].package)) {
62 message.channel.send(
63 `Awesome, you bought a copy of ${data.invoices[i].package} in this order! I'll add your Verified role now so you can get at the rest of the Discord server.`
64 );
65
66 var role = guild.roles.cache.find(role => role.name === "Verified");
67 member.roles.add(role);
68 return;
69 }
70 }
71 message.channel.send(
72 "I checked that number for you, but the order it relates to doesn't seem to contain any Rogo Digital or Cinetools assets. Are you sure that's the right order?"
73 );
74 } else {
75 message.channel.send(
76 "Hmm... I checked that number for you but it doesn't seem to be valid. Double check it and try again.\nIf it still doesn't work send a message to one of my human friends - anyone with the Dev role will be able to help you out!"
77 );
78 }
79 });
80 });
81
82 req.on("error", error => {
83 console.log(error);
84 message.channel.send(
85 "Sorry, something went wrong with my network connection... Try me again later or message a Dev or Admin!"
86 );
87 });
88
89 req.end();
90 }
91 });
92 } catch (e) {
93 console.log(e);
94 message.channel.send(
95 "I just hit an unexpected error when looking you up. Have you joined the Rogo Digital Assets server?"
96 );
97 return;
98 }
99});
100
101client.login(discord_token);
102