Created
August 31, 2021 20:14
-
-
Save c-cube/c2558b42d6fc934e7d90d4e28161d7e2 to your computer and use it in GitHub Desktop.
stupid deno irc bot
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import {Client} from "https://deno.land/x/[email protected]/mod.ts"; | |
import { readAll } from "https://deno.land/[email protected]/io/mod.ts"; | |
import { cheerio } from "https://deno.land/x/[email protected]/mod.ts"; | |
const nick = 'ElFamosoCoucouuu'; | |
const client = new Client({ | |
nick, | |
channels: ['##arch-fr-free'], | |
}); | |
client.on('raw', msg => { | |
const t = new Date(); | |
console.log(`got: ${msg.command} from ${msg.prefix} params ${msg.params} at: ${t}`); | |
}); | |
client.on('connected', _msg => { | |
console.log('connected!'); | |
}); | |
client.on('join', msg => { | |
if (msg.origin.nick === nick) { | |
return; | |
} | |
const responses = ['bienviendue NICK', "chut c'est NICK", '\\o/ NICK']; | |
const resp = responses[Math.floor(Math.random() * responses.length)]; | |
client.privmsg(msg.channel, resp.replace('NICK', msg.origin.nick)); | |
}); | |
client.on('privmsg:channel', msg => { | |
if (msg.origin.nick === 'barul' && Math.random() > 0.8) { | |
client.privmsg(msg.channel, 'salut chef'); | |
} else if (msg.text.indexOf(nick + ':') === 0) { | |
client.privmsg(msg.channel, "ouiiiii?"); | |
} | |
}); | |
/// Find URIs in string | |
function findURIs(s: string): string[] { | |
const urls = /https?:\/\/[^ $]*/.exec(s); | |
if (urls) { | |
return urls; | |
} else { | |
return []; | |
} | |
} | |
/// Preview for a URL | |
interface UrlPreview { | |
url: string, | |
title?: string, | |
} | |
client.on('privmsg:channel', async (msg) => { | |
const urls = findURIs(msg.text); | |
// download results in parallel | |
const res: Promise<UrlPreview>[] = urls.map(async (url) => { | |
try { | |
const req = await fetch(url); | |
if (req.ok) { | |
const text = await req.text(); | |
// extract title | |
const dom = cheerio.load(text); | |
const title = dom('h1').text().split('\n')[0]; // first line only | |
return {url, title}; | |
} else { | |
return {url}; | |
} | |
} catch(e) { | |
console.log(`error when fetching ${url}: ${Deno.inspect(e)}`); | |
return {url} | |
} | |
}); | |
for await (const r of res) { | |
if (r.title) { | |
console.log(`for ${r.url}: title is ${r.title}`); | |
client.privmsg(msg.channel, r.title); | |
} else { | |
console.log(`no title found for ${r.url}`); | |
} | |
} | |
}); | |
client.on('privmsg:channel', async (msg) => { | |
if (msg.text === '>uptime') { | |
try { | |
const p = Deno.run({cmd: ["uptime"], stdout: "piped"}); | |
const out = await readAll(p.stdout); | |
const dec = new TextDecoder(); | |
const outStr = dec.decode(out); | |
client.privmsg(msg.channel, outStr); | |
} catch(e) { | |
console.log(`error in uptime: ${Deno.inspect(e)}`); | |
} | |
} | |
}); | |
await client.connect("irc.libera.chat", 7000, true); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment