Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: update rules service #496

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
1 change: 1 addition & 0 deletions config.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const config = {
gettingHiredChannelId: process.env.DISCORD_GETTING_HIRED_CHANNEL_ID,
botSpamPlaygroundChannelId: '513125912070455296',
FAQChannelId: '823266307293839401',
rulesChannelId: '693244715839127653',
},
roles: {
NOBOTRoleId: '783764176178774036',
Expand Down
12 changes: 12 additions & 0 deletions new-era-commands/slash/update-rules.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
const { SlashCommandBuilder, PermissionFlagsBits } = require("discord.js");
const UpdateRulesService = require("../../services/update-rules/update-rules.service");

module.exports = {
data: new SlashCommandBuilder()
.setName("updaterules")
.setDescription("update rules in the #rules channel")
.setDefaultMemberPermissions(PermissionFlagsBits.ManageMessages),
execute: async (interaction) => {
await UpdateRulesService.handleInteraction(interaction);
},
};
3 changes: 3 additions & 0 deletions services/update-rules/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const UpdateRulesService = require("./update-rules.service");

module.exports = UpdateRulesService;
81 changes: 81 additions & 0 deletions services/update-rules/update-rules.service.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
const { EmbedBuilder } = require("discord.js");
const config = require("../../config");

class UpdateRulesService {
static EmbedDescriptionCharacterLimit = 4096;

// regex for [rule-name]: # (my rule name)
static Delimiter = /(\[rule-name\]: # \(.+\))/;

static async handleInteraction(interaction) {
let rawRules;
try {
rawRules = await UpdateRulesService.fetchRules();
} catch (error) {
console.log(error);
await interaction.reply("Failed to update rules");
return;
}

const rulesEmbeds = UpdateRulesService.createRulesEmbeds(rawRules);
const rulesChannel = interaction.guild.channels.cache.get(
config.channels.rulesChannelId
);
await UpdateRulesService.deletePreviousRules(rulesChannel);
await interaction.reply("i'm doing the thing, wait...");
await UpdateRulesService.sendRules(rulesEmbeds, rulesChannel);
await interaction.editReply("Rules updated");
}

static async fetchRules() {
const response = await fetch(
"https://raw.githubusercontent.com/TheOdinProject/top-meta/main/community-rules.md"
);
const rules = await response.text();
return rules;
}

static createRulesEmbeds(rawRules) {
return UpdateRulesService.segments(
rawRules,
UpdateRulesService.EmbedDescriptionCharacterLimit,
UpdateRulesService.Delimiter
).map((chunk) =>
new EmbedBuilder().setColor("#cc9543").setDescription(chunk)
);
}

static async sendRules(rulesEmbeds, rulesChannel) {
await Promise.all(
rulesEmbeds.map(async (e) => {
await rulesChannel.send({ embeds: [e] });
})
);
}

static async deletePreviousRules(rulesChannel) {
// might need to increase limit if there are more than 20 messages
const prev = await rulesChannel.messages.fetch({ limit: 20 });
prev.forEach((message) => message.delete());
}

static segments(string, length, delimiter) {
const segmentedString = string
// for whatever reason, discord ain't parsing these emojis. fine i'll do it myself
.replaceAll("✅", "✅")
.replaceAll("❌", "❌")
.split(delimiter)
.filter((chunk) => !chunk.match(delimiter));

for (let i = 0; i < segmentedString.length; i += 1) {
if (segmentedString[i].length >= length) {
segmentedString[i] = segmentedString[i].match(
new RegExp(`.{1,${length}}`, "g")
);
}
}
return segmentedString.flat(Infinity);
}
}

module.exports = UpdateRulesService;