2023-05-31 03:03:44 +00:00
|
|
|
const {Client, Constants, Channel} = require("@projectdysnomia/dysnomia");
|
2022-10-02 18:11:08 +00:00
|
|
|
const DiscordRPC = require("discord-rpc");
|
2022-10-10 03:15:53 +00:00
|
|
|
const chalk = require("chalk");
|
|
|
|
const fs = require("fs");
|
2023-05-31 03:03:44 +00:00
|
|
|
const os = require("os");
|
2022-10-10 03:15:53 +00:00
|
|
|
|
|
|
|
const rcfile = require("./lib/rcfile");
|
|
|
|
const config = {};
|
|
|
|
|
|
|
|
if (fs.existsSync(rcfile.path)) {
|
2023-05-31 03:03:44 +00:00
|
|
|
console.log(`% Reading ${rcfile.path.replace(os.homedir(), "~")} ...`);
|
2022-10-10 03:15:53 +00:00
|
|
|
rcfile.readFile(config);
|
|
|
|
}
|
2022-10-02 18:11:08 +00:00
|
|
|
|
|
|
|
const CLIENT_ID = "1026163285877325874";
|
2022-08-28 00:14:10 +00:00
|
|
|
|
2022-08-27 23:27:05 +00:00
|
|
|
const token = process.argv[2];
|
2022-10-10 03:15:53 +00:00
|
|
|
if (!config.token && token) {
|
|
|
|
console.log("% Writing token to .comcordrc");
|
|
|
|
config.token = token;
|
|
|
|
rcfile.writeFile(config);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!config.token && !token) {
|
|
|
|
console.log("No token provided.");
|
|
|
|
process.exit(1);
|
|
|
|
}
|
2022-08-28 00:14:10 +00:00
|
|
|
|
2022-08-28 23:59:33 +00:00
|
|
|
process.title = "comcord";
|
|
|
|
|
2022-08-28 22:01:31 +00:00
|
|
|
global.comcord = {
|
2022-10-10 03:15:53 +00:00
|
|
|
config,
|
2022-08-28 22:01:31 +00:00
|
|
|
state: {
|
2022-10-10 03:15:53 +00:00
|
|
|
rpcConnected: false,
|
2022-10-02 16:25:59 +00:00
|
|
|
startTime: Date.now(),
|
2022-08-28 22:01:31 +00:00
|
|
|
currentGuild: null,
|
|
|
|
currentChannel: null,
|
|
|
|
nameLength: 2,
|
|
|
|
inPrompt: false,
|
|
|
|
messageQueue: [],
|
|
|
|
lastChannel: new Map(),
|
2022-10-02 16:25:59 +00:00
|
|
|
afk: false,
|
2022-08-28 22:01:31 +00:00
|
|
|
},
|
|
|
|
commands: {},
|
2022-08-28 00:49:02 +00:00
|
|
|
};
|
2023-05-31 03:03:44 +00:00
|
|
|
const client = new Client(
|
|
|
|
(config.allowUserAccounts == "true" ? "" : "Bot ") + (token ?? config.token),
|
|
|
|
{
|
|
|
|
defaultImageFormat: "png",
|
|
|
|
defaultImageSize: 1024,
|
|
|
|
gateway: {
|
|
|
|
intents: Object.values(Constants.Intents),
|
2022-10-10 03:15:53 +00:00
|
|
|
},
|
2023-05-31 03:03:44 +00:00
|
|
|
allowedMentions: {
|
|
|
|
everyone: false,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
);
|
2022-08-28 22:01:31 +00:00
|
|
|
comcord.client = client;
|
2022-10-02 18:11:08 +00:00
|
|
|
const rpc = new DiscordRPC.Client({transport: "ipc"});
|
|
|
|
comcord.rpc = rpc;
|
2022-08-28 22:01:31 +00:00
|
|
|
|
|
|
|
const {finalizePrompt} = require("./lib/prompt");
|
|
|
|
const {processMessage, processQueue} = require("./lib/messages");
|
2022-10-02 18:11:08 +00:00
|
|
|
const {updatePresence} = require("./lib/presence");
|
2022-08-28 22:01:31 +00:00
|
|
|
|
|
|
|
require("./commands/quit");
|
|
|
|
require("./commands/clear");
|
|
|
|
require("./commands/help");
|
|
|
|
const {sendMode} = require("./commands/send");
|
|
|
|
require("./commands/emote");
|
|
|
|
const {listGuilds} = require("./commands/listGuilds");
|
2022-10-10 03:15:53 +00:00
|
|
|
const {switchGuild} = require("./commands/switchGuild"); // loads listChannels and listUsers
|
2022-08-28 22:01:31 +00:00
|
|
|
require("./commands/switchChannel"); //loads listUsers
|
|
|
|
require("./commands/history"); // includes extended history
|
2022-10-02 16:25:59 +00:00
|
|
|
require("./commands/afk");
|
2022-10-10 03:15:53 +00:00
|
|
|
require("./commands/privateMessages");
|
2022-08-28 22:01:31 +00:00
|
|
|
|
|
|
|
process.stdin.setRawMode(true);
|
|
|
|
process.stdin.resume();
|
|
|
|
process.stdin.setEncoding("utf8");
|
2022-08-27 23:27:05 +00:00
|
|
|
|
|
|
|
client.once("ready", function () {
|
|
|
|
console.log(
|
2023-05-31 03:03:44 +00:00
|
|
|
"Logged in as: " +
|
|
|
|
chalk.yellow(`${client.user.username} (${client.user.id})`)
|
2022-08-27 23:27:05 +00:00
|
|
|
);
|
2022-08-28 22:01:31 +00:00
|
|
|
comcord.state.nameLength = client.user.username.length + 2;
|
2022-08-28 01:53:55 +00:00
|
|
|
|
|
|
|
listGuilds();
|
2022-10-02 16:25:59 +00:00
|
|
|
|
2022-10-10 03:15:53 +00:00
|
|
|
if (config.defaultGuild) {
|
|
|
|
const guild = client.guilds.get(config.defaultGuild);
|
|
|
|
if (guild != null) {
|
|
|
|
if (config.defaultChannel) {
|
|
|
|
comcord.state.currentChannel = config.defaultChannel;
|
|
|
|
comcord.state.lastChannel.set(
|
|
|
|
config.defaultGuild,
|
|
|
|
config.defaultChannel
|
|
|
|
);
|
|
|
|
}
|
|
|
|
switchGuild(guild.name);
|
|
|
|
} else {
|
|
|
|
console.log("% This account is not in the defined default guild.");
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (config.defaultChannel) {
|
|
|
|
console.log("% Default channel defined without defining default guild.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (client.user.bot) {
|
|
|
|
rpc
|
|
|
|
.login({
|
|
|
|
clientId: CLIENT_ID,
|
|
|
|
})
|
|
|
|
.catch(function () {});
|
|
|
|
}
|
2022-08-27 23:27:05 +00:00
|
|
|
});
|
2022-09-04 16:42:35 +00:00
|
|
|
client.on("error", function () {});
|
2022-08-27 23:27:05 +00:00
|
|
|
|
2022-10-02 19:17:31 +00:00
|
|
|
rpc.on("connected", function () {
|
2022-10-10 03:15:53 +00:00
|
|
|
comcord.state.rpcConnected = true;
|
2022-10-02 18:11:08 +00:00
|
|
|
updatePresence();
|
|
|
|
});
|
2022-10-09 19:36:59 +00:00
|
|
|
let retryingRPC = false;
|
2022-10-02 19:17:31 +00:00
|
|
|
rpc.once("ready", function () {
|
2023-05-31 03:03:44 +00:00
|
|
|
rpc.transport.on("error", function () {});
|
2022-10-09 19:36:59 +00:00
|
|
|
rpc.transport.on("close", function () {
|
2022-10-10 03:15:53 +00:00
|
|
|
comcord.state.rpcConnected = false;
|
2022-10-09 19:36:59 +00:00
|
|
|
if (!retryingRPC) {
|
|
|
|
retryingRPC = true;
|
|
|
|
setTimeout(function () {
|
|
|
|
rpc.transport
|
|
|
|
.connect()
|
|
|
|
.then(() => {
|
|
|
|
retryingRPC = false;
|
|
|
|
})
|
|
|
|
.catch((err) => {
|
|
|
|
retryingRPC = false;
|
|
|
|
rpc.transport.emit("close");
|
|
|
|
});
|
|
|
|
}, 5000);
|
2022-10-02 19:17:31 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
2022-10-02 18:11:08 +00:00
|
|
|
rpc.on("error", function () {});
|
|
|
|
|
2022-10-10 03:15:53 +00:00
|
|
|
client.on("messageCreate", async function (msg) {
|
2022-08-28 02:13:25 +00:00
|
|
|
if (msg.author.id === client.user.id) return;
|
|
|
|
|
2023-05-31 03:03:44 +00:00
|
|
|
if (
|
|
|
|
!(msg.channel instanceof Channel) &&
|
|
|
|
msg.author.id != client.user.id &&
|
|
|
|
!msg.guildID
|
|
|
|
) {
|
|
|
|
const newChannel = await client.getDMChannel(msg.author.id);
|
|
|
|
if (msg.channel.id == newChannel.id) msg.channel = newChannel;
|
2022-10-10 03:15:53 +00:00
|
|
|
}
|
|
|
|
|
2023-05-31 03:03:44 +00:00
|
|
|
if (!(msg.channel instanceof Channel)) return;
|
|
|
|
|
2022-10-10 03:15:53 +00:00
|
|
|
if (
|
2023-05-31 03:03:44 +00:00
|
|
|
msg.channel.id == comcord.state.currentChannel ||
|
2022-10-10 03:15:53 +00:00
|
|
|
msg.channel?.recipient != null
|
|
|
|
) {
|
2022-08-28 22:01:31 +00:00
|
|
|
if (comcord.state.inPrompt) {
|
|
|
|
comcord.state.messageQueue.push(msg);
|
2022-08-27 23:27:05 +00:00
|
|
|
} else {
|
2022-10-02 16:17:39 +00:00
|
|
|
processMessage(msg);
|
2022-08-28 02:43:00 +00:00
|
|
|
}
|
|
|
|
}
|
2022-10-10 03:15:53 +00:00
|
|
|
|
|
|
|
if (msg.channel?.recipient != null) {
|
|
|
|
comcord.state.lastDM = msg.author;
|
|
|
|
}
|
2022-08-28 02:43:00 +00:00
|
|
|
});
|
2022-10-10 03:15:53 +00:00
|
|
|
client.on("messageUpdate", async function (msg, old) {
|
2022-08-28 02:43:00 +00:00
|
|
|
if (msg.author.id === client.user.id) return;
|
|
|
|
|
2023-05-31 03:03:44 +00:00
|
|
|
if (
|
|
|
|
!(msg.channel instanceof Channel) &&
|
|
|
|
msg.author.id != client.user.id &&
|
|
|
|
!msg.guildID
|
|
|
|
) {
|
|
|
|
const newChannel = await client.getDMChannel(msg.author.id);
|
|
|
|
if (msg.channel.id == newChannel.id) msg.channel = newChannel;
|
2022-10-10 03:15:53 +00:00
|
|
|
}
|
|
|
|
|
2023-05-31 03:03:44 +00:00
|
|
|
if (!(msg.channel instanceof Channel)) return;
|
|
|
|
|
2022-10-10 03:15:53 +00:00
|
|
|
if (
|
2023-05-31 03:03:44 +00:00
|
|
|
msg.channel.id == comcord.state.currentChannel ||
|
2022-10-10 03:15:53 +00:00
|
|
|
msg.channel?.recipient != null
|
|
|
|
) {
|
2022-10-20 02:23:33 +00:00
|
|
|
if (old && msg.content == old.content) return;
|
2022-08-28 02:43:00 +00:00
|
|
|
|
2022-08-28 22:01:31 +00:00
|
|
|
if (comcord.state.inPrompt) {
|
|
|
|
comcord.state.messageQueue.push(msg);
|
2022-08-28 02:43:00 +00:00
|
|
|
} else {
|
2022-10-02 16:17:39 +00:00
|
|
|
processMessage(msg);
|
2022-08-28 00:14:10 +00:00
|
|
|
}
|
|
|
|
}
|
2022-10-10 03:15:53 +00:00
|
|
|
|
|
|
|
if (msg.channel?.recipient != null) {
|
|
|
|
comcord.state.lastDM = msg.author;
|
|
|
|
}
|
2022-08-28 00:14:10 +00:00
|
|
|
});
|
|
|
|
|
2022-08-28 22:01:31 +00:00
|
|
|
process.stdin.on("data", async function (key) {
|
|
|
|
if (comcord.state.inPrompt) {
|
2022-08-28 03:22:06 +00:00
|
|
|
if (key === "\r") {
|
2022-08-28 22:01:31 +00:00
|
|
|
await finalizePrompt();
|
|
|
|
processQueue();
|
2022-08-28 03:22:06 +00:00
|
|
|
} else {
|
2022-08-28 23:20:01 +00:00
|
|
|
if (key === "\b" || key === "\u007f") {
|
2022-08-28 22:01:31 +00:00
|
|
|
if (comcord.state.promptInput.length > 0) {
|
|
|
|
process.stdout.moveCursor(-1);
|
|
|
|
process.stdout.write(" ");
|
|
|
|
process.stdout.moveCursor(-1);
|
|
|
|
comcord.state.promptInput = comcord.state.promptInput.substring(
|
|
|
|
0,
|
|
|
|
comcord.state.promptInput.length - 1
|
|
|
|
);
|
2022-08-28 03:22:06 +00:00
|
|
|
}
|
|
|
|
} else {
|
2022-09-03 00:21:00 +00:00
|
|
|
key = key.replace("\u001b", "");
|
2022-08-28 22:01:31 +00:00
|
|
|
process.stdout.write(key);
|
|
|
|
comcord.state.promptInput += key;
|
2022-08-28 03:22:06 +00:00
|
|
|
}
|
|
|
|
}
|
2022-08-28 00:14:10 +00:00
|
|
|
} else {
|
2022-08-28 22:01:31 +00:00
|
|
|
if (comcord.commands[key]) {
|
|
|
|
comcord.commands[key].callback();
|
|
|
|
} else {
|
|
|
|
sendMode();
|
2022-08-27 23:27:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2022-10-10 03:15:53 +00:00
|
|
|
if (
|
|
|
|
config.allowUserAccounts == "true" &&
|
|
|
|
!(token ?? config.token).startsWith("Bot ")
|
|
|
|
) {
|
2023-05-31 03:03:44 +00:00
|
|
|
console.log("User account support pending rewrite.");
|
|
|
|
process.exit(1);
|
|
|
|
|
|
|
|
/*if (fetch == null) {
|
2022-10-10 03:15:53 +00:00
|
|
|
console.log("Node v18+ needed for user account support.");
|
|
|
|
process.exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
(async function () {
|
|
|
|
comcord.clientSpoof = require("./lib/clientSpoof");
|
|
|
|
const superProperties = await comcord.clientSpoof.getSuperProperties();
|
|
|
|
|
|
|
|
console.log("% Allowing non-bot tokens to connect");
|
|
|
|
const connectLines = client.connect.toString().split("\n");
|
|
|
|
connectLines.splice(0, 4);
|
|
|
|
connectLines.splice(-1, 1);
|
|
|
|
|
|
|
|
const newConnect = new client.connect.constructor(connectLines.join("\n"));
|
|
|
|
client.connect = newConnect.bind(client);
|
|
|
|
|
|
|
|
console.log("% Injecting headers into request handler");
|
|
|
|
client.rest.handler.options.userAgent = `Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) discord/${superProperties.client_version} Chrome/91.0.4472.164 Electron/13.6.6 Safari/537.36`;
|
|
|
|
client.rest.handler._request = client.rest.handler.request.bind(
|
|
|
|
client.rest.handler
|
|
|
|
);
|
|
|
|
client.rest.handler.request = async function (options) {
|
|
|
|
options.headers = options.headers ?? {};
|
|
|
|
options.headers["X-Super-Properties"] =
|
|
|
|
await comcord.clientSpoof.getSuperPropertiesBase64();
|
|
|
|
|
|
|
|
return await this._request.apply(this, [options]);
|
|
|
|
}.bind(client.rest.handler);
|
|
|
|
|
|
|
|
console.log("% Setting gateway connection properties");
|
|
|
|
client.shards.options.connectionProperties = superProperties;
|
|
|
|
|
|
|
|
console.log("% Injecting application into READY payload");
|
|
|
|
client.shards._spawn = client.shards.spawn.bind(client.shards);
|
|
|
|
client.shards.spawn = function (id) {
|
|
|
|
const res = this._spawn.apply(this, [id]);
|
|
|
|
const shard = this.get(id);
|
|
|
|
if (shard) {
|
|
|
|
shard._onDispatch = shard.onDispatch.bind(shard);
|
|
|
|
shard.onDispatch = async function (packet) {
|
|
|
|
if (packet.t == "READY") {
|
|
|
|
packet.d.application = {id: CLIENT_ID, flags: 565248};
|
|
|
|
}
|
|
|
|
|
|
|
|
const ret = await this._onDispatch.apply(this, [packet]);
|
|
|
|
|
|
|
|
if (packet.t == "READY") {
|
|
|
|
for (const guild of packet.d.guilds) {
|
|
|
|
await this._onDispatch.apply(this, [
|
|
|
|
{
|
|
|
|
t: "GUILD_CREATE",
|
|
|
|
d: guild,
|
|
|
|
},
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}.bind(shard);
|
|
|
|
}
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}.bind(client.shards);
|
|
|
|
|
|
|
|
console.log("% Connecting to gateway now");
|
|
|
|
await client.connect();
|
2023-05-31 03:03:44 +00:00
|
|
|
})();*/
|
2022-10-10 03:15:53 +00:00
|
|
|
} else {
|
|
|
|
client.connect();
|
|
|
|
}
|
2022-08-28 00:49:02 +00:00
|
|
|
|
|
|
|
console.log("COMcord (c)left 2022");
|
|
|
|
console.log("Type 'h' for Commands");
|
2022-09-03 00:21:25 +00:00
|
|
|
|
|
|
|
const dateObj = new Date();
|
|
|
|
let sentTime = false;
|
|
|
|
|
|
|
|
const weekdays = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"];
|
|
|
|
const months = [
|
|
|
|
"Jan",
|
|
|
|
"Feb",
|
|
|
|
"Mar",
|
|
|
|
"Apr",
|
|
|
|
"May",
|
|
|
|
"Jun",
|
|
|
|
"Jul",
|
|
|
|
"Aug",
|
|
|
|
"Sep",
|
|
|
|
"Oct",
|
|
|
|
"Nov",
|
|
|
|
"Dec",
|
|
|
|
];
|
|
|
|
|
|
|
|
setInterval(function () {
|
|
|
|
dateObj.setTime(Date.now());
|
|
|
|
|
|
|
|
const hour = dateObj.getUTCHours(),
|
|
|
|
minutes = dateObj.getUTCMinutes(),
|
|
|
|
seconds = dateObj.getUTCSeconds(),
|
|
|
|
day = dateObj.getUTCDate(),
|
|
|
|
month = dateObj.getUTCMonth(),
|
|
|
|
year = dateObj.getUTCFullYear(),
|
|
|
|
weekDay = dateObj.getUTCDay();
|
|
|
|
|
|
|
|
const timeString = `[${weekdays[weekDay]} ${day
|
|
|
|
.toString()
|
|
|
|
.padStart(2, "0")}-${months[month]}-${year
|
|
|
|
.toString()
|
|
|
|
.substring(2, 4)} ${hour.toString().padStart(2, "0")}:${minutes
|
|
|
|
.toString()
|
|
|
|
.padStart(2, "0")}:${seconds.toString().padStart(2, "0")}]`;
|
|
|
|
|
|
|
|
if (minutes % 15 == 0 && seconds < 2 && !sentTime) {
|
|
|
|
if (comcord.state.inPrompt == true) {
|
|
|
|
comcord.state.messageQueue.push({time: true, content: timeString});
|
|
|
|
} else {
|
|
|
|
console.log(timeString);
|
|
|
|
}
|
2022-12-21 01:17:25 +00:00
|
|
|
comcord.state.nameLength = client.user.username.length + 2;
|
2022-09-03 00:21:25 +00:00
|
|
|
sentTime = true;
|
|
|
|
} else if (seconds > 2 && sentTime) {
|
|
|
|
sentTime = false;
|
|
|
|
}
|
|
|
|
}, 500);
|