Skip to content

Commit

Permalink
Add module to detect and warn about common code formatting issues
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremy-rifkin committed Nov 9, 2024
1 parent 054315a commit 0b43eeb
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 19 deletions.
38 changes: 19 additions & 19 deletions src/components/code.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,25 @@ export default class Code extends BotComponent {
);
}

async code(command: TextBasedCommand) {
const is_c = [this.wheatley.channels.c_help.id, this.wheatley.channels.c_help_text.id].includes(
this.wheatley.top_level_channel(await command.get_channel()),
static make_code_formatting_embeds(wheatley: Wheatley, channel: Discord.TextBasedChannel): Discord.APIEmbedField[] {
const is_c = [wheatley.channels.c_help.id, wheatley.channels.c_help_text.id].includes(
wheatley.top_level_channel(channel),
);
return [
{
name: "Markup",
inline: true,
value: build_description(`\\\`\\\`\\\`${is_c ? "c" : "cpp"}`, `int main() {}`, `\\\`\\\`\\\``),
},
{
name: "Result",
inline: true,
value: build_description(`\`\`\`${is_c ? "c" : "cpp"}`, `int main() {}`, `\`\`\``),
},
];
}

async code(command: TextBasedCommand) {
if (!command.is_slash()) {
// text, check for common monke errors
const message = command.get_message_object();
Expand All @@ -56,22 +71,7 @@ export default class Code extends BotComponent {
new Discord.EmbedBuilder()
.setColor(colors.wheatley)
.setTitle("How to Format Code on Discord")
.addFields(
{
name: "Markup",
inline: true,
value: build_description(
`\\\`\\\`\\\`${is_c ? "c" : "cpp"}`,
`int main() {}`,
`\\\`\\\`\\\``,
),
},
{
name: "Result",
inline: true,
value: build_description(`\`\`\`${is_c ? "c" : "cpp"}`, `int main() {}`, `\`\`\``),
},
)
.addFields(...Code.make_code_formatting_embeds(this.wheatley, await command.get_channel()))
.setFooter({
text: "Note: Back-tick (`) not quotes (')",
}),
Expand Down
35 changes: 35 additions & 0 deletions src/components/formatting-error-detection.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import * as Discord from "discord.js";

import { strict as assert } from "assert";

import { M } from "../utils/debugging-and-logging.js";
import { colors } from "../common.js";
import { BotComponent } from "../bot-component.js";
import { parse_out } from "../utils/strings.js";
import Code from "./code.js";

export default class FormattingErrorDetection extends BotComponent {
static override get is_freestanding() {
return true;
}

override async on_message_create(message: Discord.Message) {
if (message.author.bot) {
return;
}
const non_code_content = parse_out(message.content);
if (non_code_content.includes(`'''`) || non_code_content.includes(`"""`) || non_code_content.includes("```")) {
assert(!message.channel.isDMBased());
await message.channel.send({
content: `<@${message.author.id}>`,
embeds: [
new Discord.EmbedBuilder()
.setColor(colors.wheatley)
.setTitle("It looks like you may have code formatting errors in your message")
.addFields(...Code.make_code_formatting_embeds(this.wheatley, message.channel))
.setDescription("Note: Make sure to use __**back-ticks**__ (`) and not quotes (')"),
],
});
}
}
}

0 comments on commit 0b43eeb

Please sign in to comment.