Skip to content

Commit

Permalink
Added react messages to welcome channel for notification roles.
Browse files Browse the repository at this point in the history
  • Loading branch information
McJeffr committed May 7, 2022
1 parent 8f2ca71 commit e11afbe
Show file tree
Hide file tree
Showing 6 changed files with 130 additions and 2 deletions.
2 changes: 2 additions & 0 deletions config/default.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ module.exports = {
deletedLogsRoom: '615737464643911680',
trickcordTreatRoom: '766318346424680460',
broadcastRoom: '380086729526345739',
welcomeRoom: '148149183226970113',
trickcordTreatDeleteTime: ONE_MINUTE_IN_MS,
games: {
aliases: {
Expand Down Expand Up @@ -172,6 +173,7 @@ module.exports = {
minez: '793166203782692884',
wir: '793166266096418837',
slaughter: '857759646807097345',
annihilation: '972561980336525362',
},
clearers: [
{
Expand Down
6 changes: 4 additions & 2 deletions locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -116,5 +116,7 @@
"@{username}#{discriminator} was banned after excessive pinging": "@{username}#{discriminator} was banned after excessive pinging",
"Locking this room, making it not close automatically after inactivity.": "Locking this room, making it not close automatically after inactivity.",
"Unlocking this room, making it automatically close after inactivity.": "Unlocking this room, making it automatically close after inactivity.",
"Could not (un)lock channel at this time. Try again in a bit!": "Could not (un)lock channel at this time. Try again in a bit!"
}
"Could not (un)lock channel at this time. Try again in a bit!": "Could not (un)lock channel at this time. Try again in a bit!",
"React to the messages below to enable notifications for your favorite gamemodes!\n_You can disable notifications again using `!notifyremove <gamemode>`_": "React to the messages below to enable notifications for your favorite gamemodes!\n_You can disable notifications again using `!notifyremove <gamemode>`_",
"React to get notifications for `{gamemode}`": "React to get notifications for `{gamemode}`"
}
8 changes: 8 additions & 0 deletions src/Helper/BotDeterminer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module.exports = {
isBot: function (discordClient, id) {
return id === discordClient.user.id;
},
isPostedByBot: function (discordClient, message) {
return message.author.id === discordClient.user.id;
},
};
18 changes: 18 additions & 0 deletions src/Helper/ChannelRetriever.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
module.exports = {
retrieveChannel: function (guild, channelId) {
const discordChannel = guild.channels.cache.get(channelId);
if (discordChannel) {
return discordChannel;
}
},
retrieveChannels: function (guild, channelIds) {
const channels = [];
channelIds.forEach((channelId) => {
const discordChannel = this.retrieveChannel(guild, channelId);
if (discordChannel) {
channels.push(discordChannel);
}
});
return channels;
},
};
18 changes: 18 additions & 0 deletions src/Helper/RoleRetriever.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
module.exports = {
retrieveRole: function (guild, roleId) {
const role = guild.roles.cache.get(roleId);
if (role) {
return role;
}
},
retrieveRoles: function (guild, roleIds) {
const roles = [];
roleIds.forEach((roleId) => {
const role = this.retrieveRole(guild, roleId);
if (role) {
roles.push(role);
}
});
return roles;
},
};
80 changes: 80 additions & 0 deletions src/Utility/RolePoster.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
const BotModule = require("../BotModule");
const BotDeterminer = require("../Helper/BotDeterminer");
const ChannelRetriever = require("../Helper/ChannelRetriever");
const RoleRetriever = require("../Helper/RoleRetriever");

const localizedMessages = {
notificationRoleInfo:
"React to the messages below to enable notifications for your favorite gamemodes!\n_You can disable notifications again using `!notifyremove <gamemode>`_",
notificationRole: "React to get notifications for `{gamemode}`",
};

module.exports = BotModule.extend({
config: null,
dependencies: {
config: "config",
i18n: "i18n",
},
initialize: function (dependencyGraph) {
this._super(dependencyGraph);

this.discordClient.once("ready", async () => {
/* Fetch all configuration */
const guild = this.discordClient.guilds.cache.first();
const welcomeChannelId = this.config.welcomeRoom;
if (!welcomeChannelId) {
return;
}
const welcomeChannel = ChannelRetriever.retrieveChannel(guild, welcomeChannelId);
if (!welcomeChannel) {
return;
}
const notificationRoleConfig = this.config.notificationRoles;
if (!notificationRoleConfig) {
return;
}
const notificationRoleIds = Object.keys(notificationRoleConfig).map((name) => notificationRoleConfig[name]);
const nameMappedNotificationRoleIds = Object.keys(notificationRoleConfig).reduce((acc, name) => {
acc[notificationRoleConfig[name]] = name;
return acc;
}, {});
const notificationRoles = RoleRetriever.retrieveRoles(guild, notificationRoleIds);
if (!notificationRoles) {
return;
}

/* Delete all previous messages posted by the bot in the welcome channel */
const messages = await welcomeChannel.messages.fetch({ limit: 50 });
await Promise.all(
messages.map(async (message) => {
if (BotDeterminer.isPostedByBot(this.discordClient, message)) {
await message.delete();
}
})
);

/* Post new messages for every notification role with a reaction listener */
await welcomeChannel.send(this.i18n.__mf(localizedMessages.notificationRoleInfo));
await welcomeChannel.send("** **");
const roleMessages = [];
for (const role of notificationRoles) {
const message = await welcomeChannel.send(
this.i18n.__mf(localizedMessages.notificationRole, { gamemode: nameMappedNotificationRoleIds[role.id] })
);
await message.react("📨");
roleMessages.push({ messageId: message.id, role });
}
this.discordClient.on("messageReactionAdd", async (event, user) => {
const messageId = event.message.id;
const roleMessage = roleMessages.find((roleMessage) => roleMessage.messageId === messageId);
if (!roleMessage || BotDeterminer.isBot(this.discordClient, user.id)) {
return;
}
const member = await guild.members.fetch(user.id);
await member.roles.add(roleMessage.role);
await event.remove(); // Because there is no easy way to remove a single user's reaction in discord.js
await event.message.react("📨");
});
});
},
});

0 comments on commit e11afbe

Please sign in to comment.