some error handling and youtube trending command

This commit is contained in:
Joonas 2023-01-31 18:04:15 +02:00
parent 6ebba9ed82
commit 8bb96f9bd1

View file

@ -35,10 +35,24 @@ client.on("close", async (error) => {
});
client.on("PRIVMSG", async (msg) => {
if (msg.senderUsername === "juuuuuuuuuunas") return;
const splitted = msg.messageText.split(" ");
if (splitted[1] === "") {
splitted.splice(1, 1);
}
console.log(splitted);
if (splitted[0] === "%") {
switch (splitted[1]) {
case "pyramid":
case "pyramid": {
if (isNaN(splitted[2])) {
client.say(
msg.channelName,
`${msg.displayName}, Second arg needs to be a valid number PoroSad`
);
break;
}
let arr = [];
for (let i = 0; i < parseInt(splitted[2]); i++) {
arr.push(splitted.slice(3).join(" "));
@ -54,8 +68,9 @@ client.on("PRIVMSG", async (msg) => {
.catch((err) => console.log(err));
}
break;
}
case "ping":
case "ping": {
await client
.say(
msg.channelName,
@ -63,18 +78,28 @@ client.on("PRIVMSG", async (msg) => {
)
.catch((err) => console.log(err));
break;
}
case "fill": {
if (!splitted[2]) {
client.say(
msg.channelName,
`${msg.displayName}, Supply an arg PoroSad`
);
break;
}
case "fill":
let str = "";
while (true) {
if (str.length + splitted[2].length >= 450) break;
str += `${splitted[2]} `;
if (str.length + splitted.slice(2).join(" ").length >= 450) break;
str += `${splitted.slice(2).join(" ")} `;
}
await client.me(msg.channelName, str).catch((err) => console.log(err));
break;
}
case "weather":
case "weather": {
const data = await fetch(`https://wttr.in/${splitted[2]}?format=j1`)
.then((res) => res.json())
.catch((err) => console.log(err));
@ -86,11 +111,38 @@ client.on("PRIVMSG", async (msg) => {
)
.catch((err) => console.log(err));
break;
}
default:
case "trending": {
if (!splitted[2]) {
await client.say(
msg.channelName,
`${msg.displayName}, Please append a region PoroSad`
);
break;
}
const data = await fetch(
`https://api-piped.mha.fi/trending?region=${splitted[2]}`
)
.then((res) => res.json())
.catch((err) => console.log(err));
await client
.say(
msg.channelName,
`Currently trending in YouTube at region ${splitted[2]}: Title: ${data[0]?.title} Link: youtu.be${data[0]?.url} Uploaded: ${data[0]?.uploadedDate}`
)
.catch((err) => console.log(err));
break;
}
default: {
await client
.say(msg.channelName, `${msg.displayName} Not a command`)
.catch((err) => console.log(err));
}
}
}
});