When you manually time-out a user using the discord timeout button (right click), the reason only stays in the audit log. This can be inconvenient for the moderators and the user as you can not easily find out the reason of the timeout. This snippet will DM the user upon their time-out, and create a log in the Logs-channel! The DM and the log will consist of a beautiful epoch timestamp, this took long to make. The moderator name will only show in the logs and will not be DMed.
// authenticates you with the API standard library
// type `await lib.` to display API autocomplete
const lib = require('lib')({token: process.env.STDLIB_SECRET_TOKEN});
let updateduser = context.params.event.user.id
let result = await lib.discord.guilds['@0.2.4'].auditLogs.list({
guild_id: `${context.params.event.guild_id}`,
limit: 1
});// we will only grab the latest audit log entry
if (!updateduser === result.users[0].id) return;//in the rare event that the bot is slower than the human, end process
let mod = result.users[1].id
let reason = result.audit_log_entries[0].reason
console.log(reason)
if (reason === undefined) return; //end if no reason since they can find out the timeout themselves
console.log(result.audit_log_entries[0].changes[0].key)
let testvalue = result.audit_log_entries[0].changes[0].key
if (testvalue === `communication_disabled_until`){
let time = Date.parse(result.audit_log_entries[0].changes[0].new_value)//we parse the discord timestamp to msepoch
console.log(time)
let divided = time/1000//we parse the msepoch to epoch
console.log(divided)
let dpts = divided.toString()
console.log(dpts)
if (dpts.includes(`.`)){//we check if it is a decimal and remove the numbers after the dot if so.
let nd = dpts.split(`.`)
console.log(nd)
console.log(nd[0])
let epoch = ``
try {
await lib.discord.users['@0.2.1'].dms.create({
recipient_id: `${updateduser}`,
"content": "",
"tts": false,
"embeds": [
{
"type": "rich",
"title": "",
"description": `You have been timed out in ${process.env.SERVERNAME}. The timeout will expire ${epoch}`,
"color": 0xff0000,
"footer": {
"text": `Reason: ${reason}`
}
}
]
});//we dm the user
} catch (e) {}//ignore errors such as dm closed
await lib.discord.channels['@0.3.0'].messages.create({
"channel_id": `${process.env.LOGCHANNEL}`,
"content": "**Log:**",
"tts": false,
"embeds": [
{
"type": "rich",
"title": "",
"description": `<@${updateduser}> has been timed out by <@${mod}> The timeout will expire ${epoch}`,
"color": 0xff0000,
"footer": {
"text": `Reason: ${reason}`
}
}
]
});//we make a log
return
} else {
let epoch = ``
try {
await lib.discord.users['@0.2.1'].dms.create({
recipient_id: `${updateduser}`,
"content": "",
"tts": false,
"embeds": [
{
"type": "rich",
"title": "",
"description": `You have been timed out in ${process.env.SERVERNAME}. The timeout will expire ${epoch}`,
"color": 0xff0000,
"footer": {
"text": `Reason: ${reason}`
}
}
]
});//we dm the user
}catch (e) {}//ignore errors such as dm closed
await lib.discord.channels['@0.3.0'].messages.create({
"channel_id": `${process.env.LOGCHANNEL}`,
"content": "**Log:**",
"tts": false,
"embeds": [
{
"type": "rich",
"title": "",
"description": `<@${updateduser}> has been timed out by <@${mod}> The timeout will expire ${epoch}`,
"color": 0xff0000,
"footer": {
"text": `Reason: ${reason}`
}
}
]
});//we make a log
}
}