Created
March 20, 2021 16:06
-
-
Save jgengo/47c8591f965048301943245b6c2640f8 to your computer and use it in GitHub Desktop.
discord.js on message
This file contains 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
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