Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions modules/welcomer/configs/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,16 @@
"description-de": "Rollen, die Booster haben sollen",
"params-de": {},
"default-de": []
},
{
"field_name": "delete-welcome-message",
"humanname-en": "Delete welcome message",
"humanname-de": "Willkommensnachricht löschen",
"default": true,
"type": "boolean",
"description-en": "Should their welcome message be deleted, if a user leaves the server within 7 days",
"description-de": "Soll die Willkommensnachricht eines Nutzers, der den Server innerhalb von 7 Tagen wieder verlässt gelöscht werden",
"params-de": {}
}
]
}
23 changes: 22 additions & 1 deletion modules/welcomer/events/guildMemberAdd.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ module.exports.run = async function (client, guildMember) {
if (!client.botReadyAt) return;
if (guildMember.guild.id !== client.guild.id) return;
const moduleConfig = client.configurations['welcomer']['config'];
const moduleModel = client.models['welcomer']['User'];
if (guildMember.user.bot && moduleConfig['not-send-messages-if-member-is-bot']) return;

const moduleChannels = client.configurations['welcomer']['channels'];
Expand All @@ -26,7 +27,7 @@ module.exports.run = async function (client, guildMember) {
}
if (!message) message = channelConfig.message;

await channel.send(embedType(message || 'Message not found',
const sentMessage = await channel.send(embedType(message || 'Message not found',
{
'%mention%': guildMember.toString(),
'%servername%': guildMember.guild.name,
Expand All @@ -37,5 +38,25 @@ module.exports.run = async function (client, guildMember) {
'%createdAt%': formatDate(guildMember.user.createdAt)
}
));

const memberModel = await moduleModel.findOne({
where: {
userId: guildMember.id
}
});
if (memberModel) {
await memberModel.update({
channelID: sentMessage.channelId,
messageID: sentMessage.id,
timestamp: new Date()
});
} else {
await moduleModel.create({
userID: guildMember.id,
channelID: sentMessage.channelId,
messageID: sentMessage.id,
timestamp: new Date()
});
}
}
};
43 changes: 42 additions & 1 deletion modules/welcomer/events/guildMemberRemove.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ module.exports.run = async function (client, guildMember) {
if (!client.botReadyAt) return;
if (guildMember.guild.id !== client.guild.id) return;
const moduleConfig = client.configurations['welcomer']['config'];
const moduleModel = client.models['welcomer']['User'];
if (guildMember.user.bot && moduleConfig['not-send-messages-if-member-is-bot']) return;

const moduleChannels = client.configurations['welcomer']['channels'];
Expand Down Expand Up @@ -33,4 +34,44 @@ module.exports.run = async function (client, guildMember) {
}
));
}
};
const memberModel = await moduleModel.findOne({
where: {
userId: guildMember.id
}
});
if (memberModel && moduleConfig['delete-welcome-message']) {
const channel = await guildMember.guild.channels.fetch(memberModel.channelID).catch(() => {});
if (await timer(client, guildMember.id)) {
try {
await (await channel.messages.fetch(memberModel.messageID)).delete();
} catch (e) {}
}
await moduleModel.destroy({
where: {
userId: guildMember.id
}
});


}
};

/**
** Function to handle the time stuff
* @private
* @param client Client of the bot
* @param {userId} userId Id of the User
* @returns {Promise<boolean>}
*/
async function timer(client, userId) {
const model = client.models['welcomer']['User'];
const timeModel = await model.findOne({
where: {
userId: userId
}
});
if (timeModel) {
// check timer duration
return timeModel.timestamp.getTime() + 604800000 >= Date.now();
}
}
26 changes: 26 additions & 0 deletions modules/welcomer/models/User.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
const {DataTypes, Model} = require('sequelize');

module.exports = class User extends Model {
static init(sequelize) {
return super.init({
id: {
autoIncrement: true,
type: DataTypes.INTEGER,
primaryKey: true
},
userID: DataTypes.STRING,
channelID: DataTypes.STRING,
messageID: DataTypes.STRING,
timestamp: DataTypes.DATE
}, {
tableName: 'welcomer_User',
timestamps: true,
sequelize
});
}
};

module.exports.config = {
'name': 'User',
'module': 'welcomer'
};
1 change: 1 addition & 0 deletions modules/welcomer/module.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"description-en": "Simple module to say \"Hi\" to new members, give them roles automatically and say \"thanks\" to users who boosted",
"description-de": "Einfaches Modul zum Begrüßen von neuen Usern, zum automatischen Vergeben von Rollen beim Joinen und zum Bedanken bei Boosts.",
"events-dir": "/events",
"models-dir": "/models",
"config-example-files": [
"configs/channels.json",
"configs/random-messages.json",
Expand Down