Skip to content
This repository was archived by the owner on Jul 20, 2024. It is now read-only.

Commit a423be2

Browse files
committed
Interactive terminal feature added. v2.1 released.
1 parent e67a6ae commit a423be2

File tree

3 files changed

+1069
-10
lines changed

3 files changed

+1069
-10
lines changed

index.js

Lines changed: 165 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,169 @@
11
const DiscordRPC = require("discord-rpc");
2-
const {clientId,staticActivityOptions} = require('./config.json');
3-
DiscordRPC.register(clientId);
2+
const fs = require('fs');
43
const rpc = new DiscordRPC.Client({transport: 'ipc'});
4+
const inquirer = require("inquirer");
55

6-
rpc.on('ready', () => {
7-
if (!rpc) return;
8-
rpc.setActivity(staticActivityOptions).then(drpc => console.log(`${drpc.name} is running`))
9-
})
6+
fs.access("config.json", async (err) => {
7+
if (err) {
8+
console.log("File doesnt exists. Let's create one.")
9+
await createConfigFile("config.json")
10+
console.log("config.json file successfully created. Restarting in 5 seconds...")
11+
setTimeout(async () => {
12+
await startRpc('./config.json')
13+
}, 5000);
14+
} else {
15+
await startRpc('./config.json')
16+
}
17+
});
18+
async function createConfigFile(fileName) {
19+
await inquirer.prompt([{
20+
name: "clientid",
21+
type: "input",
22+
message: "What is your client id?",
23+
}, {
24+
name: "details",
25+
type: "input",
26+
message: "What is your first text (detail) ?",
27+
}, {
28+
name: "state",
29+
type: "input",
30+
message: "What is your second text (state) ?",
31+
}, {
32+
name: "images",
33+
type: "list",
34+
message: "Do you want buttons ?",
35+
choices: ["No Image","Large Image","Small Image","Large and Small Images"]
36+
}, {
37+
name: "buttons",
38+
type: "list",
39+
message: "Do you want buttons ?",
40+
choices: ["No buttons","1 Button","2 Buttons"]
41+
}]).then(async(answer) => {
42+
switch (answer.images) {
43+
case "Large Image":
44+
await inquirer.prompt([{
45+
name: "largeImageKey",
46+
type: "input",
47+
message: "What is your large image key?",
48+
},{
49+
name: "largeImageText",
50+
type: "input",
51+
message: "What is your large image text?",
52+
}]).then((imageAnswers) => {
53+
answer.images = imageAnswers
54+
})
55+
break;
56+
case "Small Image":
57+
await inquirer.prompt([{
58+
name: "smallImageKey",
59+
type: "input",
60+
message: "What is your small image key?",
61+
},{
62+
name: "smallImageText",
63+
type: "input",
64+
message: "What is your small image text?",
65+
}]).then((imageAnswers) => {
66+
answer.images = imageAnswers
67+
})
68+
break;
69+
case "Large and Small Images":
70+
await inquirer.prompt([{
71+
name: "largeImageKey",
72+
type: "input",
73+
message: "What is your large image key?",
74+
},{
75+
name: "largeImageText",
76+
type: "input",
77+
message: "What is your large image text?",
78+
},{
79+
name: "smallImagekey",
80+
type: "input",
81+
message: "What is your small image key?",
82+
},{
83+
name: "smallImageText",
84+
type: "input",
85+
message: "What is your small image text?",
86+
}]).then((imageAnswers) => {
87+
answer.images = imageAnswers
88+
})
89+
break;
90+
}
1091

11-
rpc.login({ clientId }).catch(console.error);
92+
switch (answer.buttons) {
93+
case "1 Button":
94+
await inquirer.prompt([{
95+
name: "label",
96+
type: "input",
97+
message: "What is your button text?",
98+
},{
99+
name: "url",
100+
type: "input",
101+
message: "What is your button url?",
102+
}]).then((buttonAnswers) => {
103+
let buttons = [{
104+
label: buttonAnswers.label,
105+
url: buttonAnswers.url
106+
}]
107+
answer.buttons = buttons
108+
})
109+
break;
110+
case "2 Buttons":
111+
await inquirer.prompt([{
112+
name: "label1",
113+
type: "input",
114+
message: "What is your button 1 text?",
115+
},{
116+
name: "url1",
117+
type: "input",
118+
message: "What is your button 1 url?",
119+
},{
120+
name: "label2",
121+
type: "input",
122+
message: "What is your button 2 text?",
123+
},{
124+
name: "url2",
125+
type: "input",
126+
message: "What is your button 2 url?",
127+
}]).then((buttonAnswers) => {
128+
let firstButton = {label: buttonAnswers.label1,url: buttonAnswers.url1}
129+
let secondButton = {label: buttonAnswers.label2,url: buttonAnswers.url2}
130+
let buttons = [firstButton,secondButton]
131+
answer.buttons = buttons
132+
})
133+
break;
134+
}
135+
let configDatas = {
136+
clientId: answer.clientid,
137+
staticActivityOptions: {
138+
details: answer.details,
139+
state: answer.state
140+
}
141+
}
142+
if(typeof(answer.images) == 'object'){
143+
Object.keys(answer.images).forEach(key => {
144+
configDatas.staticActivityOptions[key] = answer.images[key]
145+
})
146+
}
147+
if(answer.buttons != "No buttons"){
148+
configDatas.staticActivityOptions["buttons"] = []
149+
let configbuttons = []
150+
answer.buttons.forEach(button => {
151+
configbuttons.push(button)
152+
});
153+
configDatas.staticActivityOptions["buttons"] = configbuttons
154+
}
155+
const content = JSON.stringify(configDatas);
156+
fs.writeFileSync(fileName, content);
157+
});
158+
}
159+
160+
async function startRpc(configFile) {
161+
const {clientId,staticActivityOptions} = require(configFile);
162+
DiscordRPC.register(clientId);
163+
rpc.on('ready', () => {
164+
if (!rpc) return;
165+
rpc.setActivity(staticActivityOptions).then(drpc => console.log(`${drpc.name} is running`))
166+
})
167+
168+
rpc.login({clientId}).catch(console.error);
169+
}

0 commit comments

Comments
 (0)