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

Change pin behavior and resolve channel names in archive #56

Merged
merged 5 commits into from
Jul 5, 2023
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
12 changes: 1 addition & 11 deletions api/src/discord/commands/workingOn.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,6 @@
import {
ApplicationCommandOptionType,
ApplicationCommandType,
CategoryChannel,
ChannelType,
Client,
CommandInteraction,
TextChannel,
} from "discord.js";
import { ApplicationCommandType, Client, CommandInteraction } from "discord.js";
import { Command } from "../command";
import { getCTFNamesFromDatabase, getCtfFromDatabase } from "../database/ctfs";
import {
getTaskByCtfIdAndNameFromDatabase,
userStartsWorkingOnTask,
userStopsWorkingOnTask,
} from "../database/tasks";
Expand Down
4 changes: 2 additions & 2 deletions api/src/discord/database/tasks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ export interface Task {
tags: string[] | undefined;
title: string;
description: string;
ctfId: bigint;
ctf_id: bigint;
flag: string;
}

function buildTask(row: any): Task {
return {
id: row.id as bigint,
ctfId: row.ctf_id as bigint,
ctf_id: row.ctf_id as bigint,
title: row.title as string,
description: row.description as string,
tags: undefined,
Expand Down
47 changes: 30 additions & 17 deletions api/src/discord/utils/channels.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,11 @@ import {
getTaskFromId,
getUserIdsWorkingOnTask,
} from "../database/tasks";
import { sendMessageToChannel } from "./messages";
import {
getTaskTitleFromTopic,
sendMessageToChannel,
topicDelimiter,
} from "./messages";
import {
isChannelOfCtf,
isTaskChannelOf,
Expand Down Expand Up @@ -121,7 +125,7 @@ async function createCategoryChannel(

async function createTaskChannel(
guild: Guild,
task: Task | TaskInput | string,
task: Task,
category: CategoryChannel
) {
const taskName = typeof task === "string" ? task : task.title;
Expand All @@ -130,7 +134,7 @@ async function createTaskChannel(
name: taskName,
type: ChannelType.GuildText,
parent: category.id,
topic: taskName,
topic: taskName + topicDelimiter + " " + (await getTaskLink(task)),
});
}

Expand Down Expand Up @@ -288,7 +292,7 @@ export async function createChannelForTaskInCtf(
) {
// query CTF if not provided
if (ctf == null) {
ctf = await getCtfFromDatabase(task.ctfId);
ctf = await getCtfFromDatabase(task.ctf_id);
if (ctf == null) return;
}

Expand All @@ -305,27 +309,35 @@ export async function createChannelForTaskInCtf(
return handleCreateAndNotify(guild, task, ctf, category, announce);
}

async function pinTaskLinkToChannel(
channel: TextChannel,
task: Task,
ctf: CTF
) {
if (config.pad.domain == "") return;
async function getTaskLink(task: Task, ctf: CTF | null = null) {
if (config.pad.domain == "") return "";

if (ctf == null) {
ctf = await getCtfFromDatabase(task.ctf_id);
if (ctf == null) return "";
}

const ssl = config.pad.useSSL == "false" ? "" : "s";

const url = `http${ssl}://${config.pad.domain}/#/ctf/${ctf.id}-${safeSlugify(
return `http${ssl}://${config.pad.domain}/#/ctf/${ctf.id}-${safeSlugify(
ctf.title
)}/task/${task.id}`;
}

async function pinTaskLinkToChannel(
channel: TextChannel,
task: Task,
ctf: CTF
) {
const url = await getTaskLink(task, ctf);
if (url == "") return;

const message = await sendMessageToChannel(
channel,
`CTFNote task: ${url}`,
true
);
if (message == null) return;

await message.pin();
}

async function handleCreateAndNotify(
Expand Down Expand Up @@ -356,7 +368,7 @@ export async function createChannelForNewTask(
newTask: Task,
announce = false
) {
const ctf = await getCtfFromDatabase(newTask.ctfId);
const ctf = await getCtfFromDatabase(newTask.ctf_id);
if (ctf == null) return;

let movingType = CategoryType.NEW;
Expand Down Expand Up @@ -384,7 +396,7 @@ export async function getTaskChannel(
ctf: CTF | null
) {
if (ctf == null) {
ctf = await getCtfFromDatabase(task.ctfId);
ctf = await getCtfFromDatabase(task.ctf_id);
if (ctf == null) return null;
}

Expand Down Expand Up @@ -415,8 +427,9 @@ export async function getCurrentTaskChannelFromDiscord(
getCtfNameFromCategoryName(category.name)
);
if (ctf == null) return null;
if (interaction.channel.topic == null) return null;

const name = interaction.channel.topic;
const name = getTaskTitleFromTopic(interaction.channel.topic);
if (name == null) return null;

const task = await getTaskByCtfIdAndNameFromDatabase(ctf.id, name);
Expand All @@ -438,7 +451,7 @@ export async function moveChannel(
}

if (ctf == null) {
ctf = await getCtfFromDatabase(task.ctfId);
ctf = await getCtfFromDatabase(task.ctf_id);
if (ctf == null) return;
}
const taskChannel = await getTaskChannel(guild, task, ctf);
Expand Down
4 changes: 3 additions & 1 deletion api/src/discord/utils/comparison.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
import { CTF } from "../database/ctfs";
import { Task } from "../database/tasks";
import { getCtfNameFromCategoryName } from "./channels";
import { getTaskTitleFromTopic } from "./messages";

export function isCategoryOfCtf(
channel: CategoryChannel | undefined | null,
Expand Down Expand Up @@ -38,8 +39,9 @@ export function isTaskChannelOf(
const taskTitle = typeof task === "string" ? task : task.title;

if (channel.type !== ChannelType.GuildText) return false;
if (channel.topic == null) return false;

return channel.topic == taskTitle;
return getTaskTitleFromTopic(channel.topic) == taskTitle;
}

export function isRoleOfCtf(role: Role | string, ctf: CTF | string) {
Expand Down
46 changes: 33 additions & 13 deletions api/src/discord/utils/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
Collection,
Guild,
Message,
MessageFlags,
Snowflake,
TextBasedChannel,
TextChannel,
Expand Down Expand Up @@ -43,7 +44,7 @@ export async function sendMessageToChannel(
}

if (silent) {
options.flags = [4096];
options.flags = [MessageFlags.SuppressNotifications];
}

return await channel.send(options).catch((err) => {
Expand All @@ -64,7 +65,7 @@ export async function sendMessageToTask(
}

if (ctf == null) {
ctf = await getCtfFromDatabase(task.ctfId);
ctf = await getCtfFromDatabase(task.ctf_id);
}
if (ctf == null) return null;

Expand Down Expand Up @@ -188,20 +189,30 @@ export async function convertMessagesToPadFormat(messages: Message<boolean>[]) {
if (content.startsWith("```")) content = "\n" + content;
if (content.startsWith("> ")) content = content + "\n"; // need an extra line break for quotes

// resolve mentions to usernames
// resolve mentions to usernames or channelnames
const mentions = content.match(/<(?:[^\d>]+|:[A-Za-z0-9]+:)\w+>/g);
if (mentions != null) {
mentions.forEach((user) => {
const id = user.replace(/\D/g, "");
mentions.forEach((mention) => {
const id = mention.replace(/\D/g, "");
const discordUser = message.guild?.members.cache.get(id);
if (discordUser == null) return;

content = content.replace(
user,
discordUser.user.discriminator != "0"
? `@${discordUser.user.username}#${discordUser.user.discriminator}`
: `@${discordUser.user.username}`
);
const discordChannel = message.guild?.channels.cache.get(id);

if (discordUser != null) {
const nickname =
discordUser.nickname != null
? ` (${discordUser.nickname})`
: ``;
content = content.replace(
mention,
discordUser.user.discriminator != "0"
? `@${discordUser.user.username}#${discordUser.user.discriminator}${nickname}`
: `@${discordUser.user.username}${nickname}`
);
}

if (discordChannel != null) {
content = content.replace(mention, `#${discordChannel.name}`);
}
});
}

Expand Down Expand Up @@ -267,3 +278,12 @@ export async function createPadWithoutLimit(

return await createPad(`${ctfTitle} Discord archive`, firstPadContent);
}

export const topicDelimiter = " /-/";

export function getTaskTitleFromTopic(topic: string) {
const splitted = topic.split(topicDelimiter);
const r = splitted.pop();
if (r == null) return topic;
return splitted.join(topicDelimiter);
}
2 changes: 1 addition & 1 deletion api/src/plugins/discordHooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ const discordMutationHook = (_build: Build) => (fieldContext: Context<any>) => {
channel
.edit({
name: title,
topic: title,
topic: channel.topic?.replace(task.title, title),
})
.catch((err) => console.error("Failed to rename channel.", err));
}
Expand Down