-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.js
196 lines (147 loc) · 5.92 KB
/
bot.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/* This is Revolt bot.js example for use with NodeJS.
========= Program startup sequence and execution =========
3. General Bot start
4. General Bot information
5. Bot Status Change
6. Reply to Guild Messages
_________Program Startup_____________
0. Import Libraries
1. Command Line Token Insertion Support `node bot.js YOUR_BOT_TOKEN`
2. Start the bot and login to Revolt
======================================================================== */
/* ----------------------- 0. Import Libraries ------------------------- */
const { Client } = require("revolt.js");
let client = new Client();
/* ----------------------- 3. General Bot start ------------------------- */
client.on("ready", async () => {
// Checks if bot joined any servers yet. Opens a browser tab in Windows 10; Uses bot user id, opens bot invite url
if (client.servers.size < 1) {
const { spawn } = require('node:child_process')
const command = spawn('explorer', ["https://app.revolt.chat/bot/" + client.user._id])
}
/* ----------------------- 4. General Bot information ------------------------- */
console.log(`________________________________________`);
console.log();
console.log(`Running Revolt Bot.`);
console.log("Official repository: https://github.com/publicdomain-nocopyright/Portable-NodeJS-Revolt-Bot-Example");
console.log(`________________________________________`);
console.log(`Running: ${__filename}`);
console.log(" |-NodeJS version: " + process.version);
console.log(` |-revolt.js version: ${require(__dirname + '/node_modules/revolt.js/package.json').version}`);
console.log(` |-revolt.js repository: ${require(__dirname + '/node_modules/revolt.js/package.json').repository}`);
console.info(` |-Client API URL: ${client.api.baseURL}`)
console.info(` |-Client API version: ${client.configuration.revolt}`)
console.info(`User ID: ${client.user.id}!`);
console.info(`Logged in as ${client.user.username}!`);
console.info(`Bot online: ${client.user.online}!`);
console.info(`Bot is currently in ${client.servers.size()} ${client.servers.size() > 1 ? 'servers' : 'server'}.`);
console.log(`Invite into more servers: ` + "https://app.revolt.chat/bot/" + client.user.id );
/* ----------------------- 5. Bot Status Change ------------------------- */
client.user.edit({
status: {
text: "Listening to you.",
presence: "Idle",
},
});
// Bot set self nickname on all the servers.
//Split Bot name into two words before displaying
//const splitWord = client.user.username.replace(/([a-z])([A-Z])/g, '$1 $2'); // nickname: `${splitWord}`
client.servers.forEach(async (values, keys, objects) => {
await values.member.edit({
nickname: `${values.name}`
});
});
});
/* ----------------------- 6. Reply to Guild Messages ------------------------- */
// Wait for messages and respond with a message in the guilds where this Bot Exists.
client.on("messageCreate", async (message) => {
if (message.content === "Hello") {
await message.channel.sendMessage("World");
// Bot Needs permission "ManageMessages"
//await message.delete();
return;
}
// Support for @BotUsername in the chat
// TODO: Add permissions check
if (message.authorId !== client.user.id){
if (message.content.includes(`<@${client.user.id}> ` + "write")) {
const includedText = message.content.split(`<@${client.user.id}> ` + "write")[1];
await message.channel.sendMessage(includedText);
return;
}
if (message.content.includes(client.user.id)) {
await message.channel.sendMessage("Hey");
console.log("Hey");
return;
}
}
});
/* -------------------- 1. Command Line Token Insertion Support --------------------- */
// SYNTAX: `node bot.js YOUR_BOT_TOKEN`
// NOTE: Ignored if bot_token.txt token exists | Inserted into bot_token.txt if file is empty
let bot_token;
(function initializeBotToken() {
bot_token = process.argv[2];
// Create bot_token.txt file if does not exist.
const fs = require('fs');
if (!fs.existsSync("./bot_token.txt")) {
const stream = fs.createWriteStream('./bot_token.txt');
stream.write("");
stream.end();
}
// Read Bot Token from bot_token.txt file
if (fs.statSync('./bot_token.txt').size == 0) {
console.log("NOTE: " + "./bot_token.txt file is empty")
} else {
console.log("NOTE: " + "./bot_token.txt file is not empty")
bot_token = fs.readFileSync('./bot_token.txt','utf8');
console.log("NOTE: " + "Using bot_token from ./bot_token.txt file")
}
// Check if bot_token is not undefined
console.log("Bot token entered: ", bot_token);
if (bot_token == undefined) {
console.log("NOTE " + bot_token + " bot token undefined?");
process.exit(0);
}
})();
/* ------------------------ 2. Start the bot and login to Revolt------------------------ */
client.loginBot(bot_token);
const readline = require('readline');
// Create a readline interface
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
function restartScript() {
const spawn = require('child_process').spawn;
const node = process.argv[0]; // Get the path to the Node.js executable
const script = process.argv[1]; // Get the path to the current script
spawn(node, [script], { stdio: 'inherit' });
//process.exit();
}
// Start your bot's work
startBot();
// Function to start your bot's work
function startBot() {
// Your bot's code and logic goes here
// Example: Display a message every second
}
// Handle user input in the command prompt
rl.on('line', (input) => {
// Process the user input as needed
// Example: Stop the bot if the user enters "stop"
if (input === "stop") {
console.log("Stopping the bot...");
rl.close(); // Close the readline interface
}
// Example: Stop the bot if the user enters "stop"
if (input === "restart") {
console.log("restarting...");
restartScript();
}
});
// Handle the readline close event
rl.on('close', () => {
console.log("Exiting...");
//process.exit(0); // Exit the Node.js process
});