twitch_bot/commands/massping.js

49 lines
1.2 KiB
JavaScript
Raw Normal View History

2023-02-01 12:04:14 +01:00
const command = {
name: "massping",
desc: "ping every chatter in the room (this one also blocks the thread until it's done)",
2023-02-02 23:37:42 +01:00
restricted: true,
2023-02-02 23:58:58 +01:00
mod: true,
2023-02-01 14:01:21 +01:00
run: async (client, msg, splitted) => {
let channel = msg.channelName;
if (splitted[2]) channel = splitted[2];
const data = await fetch(
2023-02-06 15:02:34 +01:00
`https://tmi.twitch.tv/group/user/${encodeURIComponent(channel)}/chatters`
)
2023-02-01 12:04:14 +01:00
.then((res) => res.json())
.then((data) => {
return data.chatters;
})
2023-02-11 13:48:05 +01:00
.catch((err) =>
console.error("Error when fetching chatters: " + err.message)
);
2023-02-01 12:04:14 +01:00
let names = [];
function printValues(obj) {
for (var key in obj) {
if (typeof obj[key] === "object") {
printValues(obj[key]);
} else {
names.push(obj[key]);
}
}
}
printValues(data);
let temp = [];
for (let name of names) {
if (temp.join(" ").length > 450) {
2023-02-11 13:48:05 +01:00
client.say(msg.channelName, temp.join(" "));
2023-02-01 12:04:14 +01:00
temp = [];
}
2023-02-01 14:01:21 +01:00
temp.push(name);
2023-02-01 12:04:14 +01:00
if (names.slice(-1)[0] === name) {
2023-02-11 13:48:05 +01:00
client.say(msg.channelName, temp.join(" "));
2023-02-01 12:04:14 +01:00
}
}
},
};
2023-02-01 22:11:48 +01:00
module.exports = { command };