Skip to content

Instantly share code, notes, and snippets.

@jgengo
Created March 20, 2021 16:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jgengo/47c8591f965048301943245b6c2640f8 to your computer and use it in GitHub Desktop.
Save jgengo/47c8591f965048301943245b6c2640f8 to your computer and use it in GitHub Desktop.
discord.js on message
client.on("message", (msg) => {
const isDM = msg.channel.type == 'dm'
if (isDM) {
// add users in db if never seen.
if (msg.author.id !== client.user.id) {
db.get("SELECT id from users where id=?", [msg.author.id], (err, row)=> {
if (err) console.error(err.message)
if (row.length == 0) {
db.run("INSERT INTO users (id, name) values (?, ?)", msg.author.id, msg.author.username)
}
})
}
// add new messages informations into the database
const words = msg.content.trim().split(/ +/g)
db.run("INSERT INTO logs (author_id, recipient_id, words_count, chars_count) values (?,?,?,?)",
msg.author.id,
msg.channel.recipient.id,
words.length,
msg.content.length)
console.log(`${msg.author.username} to ${msg.channel.recipient.username} wrote ${words.length} words`)
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment