added flags and fixed user context menus

pull/85/head
TheCodedProf 3 years ago
parent 680c497467
commit c016f9f1ff
No known key found for this signature in database
GPG Key ID: 803E7CCB5577E6A2

@ -1,5 +1,5 @@
import { LoadingEmbed } from "../../utils/defaults.js";
import type { HistorySchema } from "../../utils/database.js";
import type { HistorySchema, FlagColors } from "../../utils/database.js";
import Discord, {
CommandInteraction,
GuildMember,
@ -10,7 +10,10 @@ import Discord, {
ButtonStyle,
TextInputStyle,
APIMessageComponentEmoji,
SlashCommandSubcommandBuilder
SlashCommandSubcommandBuilder,
StringSelectMenuBuilder,
StringSelectMenuOptionBuilder,
ContextMenuCommandInteraction
} from "discord.js";
import EmojiEmbed from "../../utils/generateEmojiEmbed.js";
import getEmojiByName from "../../utils/getEmojiByName.js";
@ -326,35 +329,38 @@ async function showHistory(member: Discord.GuildMember, interaction: CommandInte
return timedOut ? 0 : 1;
}
const callback = async (interaction: CommandInteraction): Promise<unknown> => {
export const noteMenu = async (member: GuildMember, interaction: CommandInteraction | ContextMenuCommandInteraction): Promise<unknown> => {
let m: Message;
const member = interaction.options.getMember("user") as Discord.GuildMember;
await interaction.reply({
embeds: LoadingEmbed,
ephemeral: true,
fetchReply: true
});
let note;
let firstLoad = true;
let timedOut = false;
while (!timedOut) {
note = await client.database.notes.read(member.guild.id, member.id);
if (firstLoad && !note) {
await showHistory(member, interaction);
const colors: Record<string, Discord.ColorResolvable> = {
none: "#424242",
red: "#F27878",
yellow: "#EDC575",
green: "#68D49E",
blue: "#6576CC",
purple: "#D46899",
gray: "#C4C4C4"
}
firstLoad = false;
m = (await interaction.editReply({
embeds: [
new EmojiEmbed()
.setEmoji("MEMBER.JOIN")
.setEmoji(`ICONS.FLAGS.${(note?.flag ?? "none").toUpperCase()}`)
.setTitle("Mod notes for " + member.user.username)
.setDescription(note ? note : "*No note set*")
.setStatus("Success")
.setDescription(note?.note ? note.note : "*No note set*")
.setColor(colors[note?.flag ?? "none"]!)
],
components: [
new ActionRowBuilder<Discord.ButtonBuilder>().addComponents([
new ButtonBuilder()
.setLabel(`${note ? "Modify" : "Create"} note`)
.setLabel(`${note?.note ? "Modify" : "Create"} note`)
.setStyle(ButtonStyle.Primary)
.setCustomId("modify")
.setEmoji(getEmojiByName("ICONS.EDIT", "id")),
@ -363,6 +369,49 @@ const callback = async (interaction: CommandInteraction): Promise<unknown> => {
.setStyle(ButtonStyle.Primary)
.setCustomId("history")
.setEmoji(getEmojiByName("ICONS.HISTORY", "id"))
]),
new ActionRowBuilder<Discord.StringSelectMenuBuilder>().addComponents([
new StringSelectMenuBuilder()
.setCustomId("flag")
.setPlaceholder("Select a flag")
.addOptions(
new StringSelectMenuOptionBuilder()
.setLabel("None")
.setDefault(!note?.flag)
.setValue("none")
.setDescription("Clear")
.setEmoji(getEmojiByName("ICONS.FLAGS.NONE", "id")),
new StringSelectMenuOptionBuilder()
.setLabel("Red")
.setDefault(note?.flag === "red")
.setValue("red")
.setEmoji(getEmojiByName("ICONS.FLAGS.RED", "id")),
new StringSelectMenuOptionBuilder()
.setLabel("Yellow")
.setDefault(note?.flag === "yellow")
.setValue("yellow")
.setEmoji(getEmojiByName("ICONS.FLAGS.YELLOW", "id")),
new StringSelectMenuOptionBuilder()
.setLabel("Green")
.setDefault(note?.flag === "green")
.setValue("green")
.setEmoji(getEmojiByName("ICONS.FLAGS.GREEN", "id")),
new StringSelectMenuOptionBuilder()
.setLabel("Blue")
.setDefault(note?.flag === "blue")
.setValue("blue")
.setEmoji(getEmojiByName("ICONS.FLAGS.BLUE", "id")),
new StringSelectMenuOptionBuilder()
.setLabel("Purple")
.setDefault(note?.flag === "purple")
.setValue("purple")
.setEmoji(getEmojiByName("ICONS.FLAGS.PURPLE", "id")),
new StringSelectMenuOptionBuilder()
.setLabel("Gray")
.setDefault(note?.flag === "gray")
.setValue("gray")
.setEmoji(getEmojiByName("ICONS.FLAGS.GRAY", "id")),
)
])
]
})) as Message;
@ -382,60 +431,73 @@ const callback = async (interaction: CommandInteraction): Promise<unknown> => {
timedOut = true;
continue;
}
if (i.customId === "modify") {
await i.showModal(
new Discord.ModalBuilder()
.setCustomId("modal")
.setTitle("Editing moderator note")
.addComponents(
new ActionRowBuilder<Discord.TextInputBuilder>().addComponents(
new Discord.TextInputBuilder()
.setCustomId("note")
.setLabel("Note")
.setMaxLength(4000)
.setRequired(false)
.setStyle(TextInputStyle.Paragraph)
.setValue(note ? note : " ")
if (i.isButton()) {
if (i.customId === "modify") {
await i.showModal(
new Discord.ModalBuilder()
.setCustomId("modal")
.setTitle("Editing moderator note")
.addComponents(
new ActionRowBuilder<Discord.TextInputBuilder>().addComponents(
new Discord.TextInputBuilder()
.setCustomId("note")
.setLabel("Note")
.setMaxLength(4000)
.setRequired(false)
.setStyle(TextInputStyle.Paragraph)
.setValue(note?.note ? note.note : " ")
)
)
)
);
await interaction.editReply({
embeds: [
new EmojiEmbed()
.setTitle("Mod notes for " + member.user.username)
.setDescription("Modal opened. If you can't see it, click back and try again.")
.setStatus("Success")
.setEmoji("GUILD.TICKET.OPEN")
],
components: [
new ActionRowBuilder<Discord.ButtonBuilder>().addComponents([
new ButtonBuilder()
.setLabel("Back")
.setEmoji(getEmojiByName("CONTROL.LEFT", "id"))
.setStyle(ButtonStyle.Primary)
.setCustomId("back")
])
]
});
let out;
try {
out = await modalInteractionCollector(m, interaction.user);
} catch (e) {
timedOut = true;
continue;
}
if (out === null || out.isButton()) {
continue;
} else {
let toAdd = out.fields.getTextInputValue("note").trim() || null;
if (toAdd === "") toAdd = null;
await client.database.notes.create(member.guild.id, member.id, toAdd);
);
await interaction.editReply({
embeds: [
new EmojiEmbed()
.setTitle("Mod notes for " + member.user.username)
.setDescription("Modal opened. If you can't see it, click back and try again.")
.setStatus("Success")
.setEmoji("GUILD.TICKET.OPEN")
],
components: [
new ActionRowBuilder<Discord.ButtonBuilder>().addComponents([
new ButtonBuilder()
.setLabel("Back")
.setEmoji(getEmojiByName("CONTROL.LEFT", "id"))
.setStyle(ButtonStyle.Primary)
.setCustomId("back")
])
]
});
let out;
try {
out = await modalInteractionCollector(m, interaction.user);
} catch (e) {
timedOut = true;
continue;
}
if (out === null || out.isButton()) {
continue;
} else {
let toAdd = out.fields.getTextInputValue("note").trim() || null;
if (toAdd === "") toAdd = null;
await client.database.notes.create(member.guild.id, member.id, toAdd);
}
} else if (i.customId === "history") {
await i.deferUpdate();
if (!(await showHistory(member, interaction))) return;
}
} else if (i.customId === "history") {
} else if (i.isStringSelectMenu()) {
await i.deferUpdate();
if (!(await showHistory(member, interaction))) return;
let flag: string | null = i.values[0]!;
if (flag === "none") flag = null;
await client.database.notes.flag(member.guild.id, member.id, flag as FlagColors | null);
}
}
}
const callback = async (interaction: CommandInteraction): Promise<void> => {
const member = interaction.options.getMember("user") as Discord.GuildMember;
await noteMenu(member, interaction);
};
const check = (interaction: CommandInteraction) => {

@ -51,6 +51,7 @@ export default {
CATEGORY: "1064943289708597348"
},
FLAGS: {
NONE: "1099782406417940520",
RED: "1082719687219101800",
YELLOW: "1082719684060794890",
GREEN: "1082719681326108763",

@ -0,0 +1,17 @@
import { ContextMenuCommandBuilder, GuildMember, PermissionFlagsBits, UserContextMenuCommandInteraction } from "discord.js";
import { noteMenu } from "../../commands/mod/about.js";
const command = new ContextMenuCommandBuilder().setName("Flag User").setDefaultMemberPermissions(PermissionFlagsBits.ManageMessages);
const callback = async (interaction: UserContextMenuCommandInteraction) => {
const guild = interaction.guild!;
let member = interaction.targetMember as GuildMember | null;
if (!member) member = await guild.members.fetch(interaction.targetId);
await noteMenu(member, interaction);
};
const check = async (_interaction: UserContextMenuCommandInteraction) => {
return true;
};
export { command, callback, check };

@ -4,13 +4,10 @@ import { userAbout } from "../../commands/user/about.js";
const command = new ContextMenuCommandBuilder().setName("User info");
const callback = async (interaction: UserContextMenuCommandInteraction) => {
try {
console.log("getting user info")
const guild = interaction.guild!;
let member = interaction.targetMember as GuildMember | null;
if (!member) member = await guild.members.fetch(interaction.targetId);
await userAbout(guild, member as GuildMember, interaction);
} catch (e) { console.log(e) }
const guild = interaction.guild!;
let member = interaction.targetMember as GuildMember | null;
if (!member) member = await guild.members.fetch(interaction.targetId);
await userAbout(guild, member as GuildMember, interaction);
};
const check = async (_interaction: UserContextMenuCommandInteraction) => {

@ -187,15 +187,11 @@ async function registerCommandHandler() {
client.on("interactionCreate", async (interaction: Interaction) => {
if (interaction.isUserContextMenuCommand()) {
const commandName = "contextCommands/user/" + interaction.commandName;
console.log("trying " + commandName)
try {
console.log(client.commands[commandName])
await execute(
client.commands[commandName]![0]?.check,
client.commands[commandName]![0]?.callback,
interaction
);
} catch (e) { console.log(e) }
await execute(
client.commands[commandName]![0]?.check,
client.commands[commandName]![0]?.callback,
interaction
);
return;
} else if (interaction.isMessageContextMenuCommand()) {
const commandName = "contextCommands/message/" + interaction.commandName;

@ -696,18 +696,25 @@ export class ModNotes {
this.modNotes = database.collection<ModNoteSchema>("modNotes");
}
async flag(guild: string, user: string, flag: FlagColors | null) {
const modNote = await this.modNotes.findOne({ guild: guild, user: user });
modNote
? await this.modNotes.updateOne({ guild: guild, user: user }, { $set: { flag: flag } }, collectionOptions)
: await this.modNotes.insertOne({ guild: guild, user: user, note: null, flag: flag }, collectionOptions);
}
async create(guild: string, user: string, note: string | null) {
// console.log("ModNotes create");
const modNote = await this.modNotes.findOne({ guild: guild, user: user });
modNote
? await this.modNotes.updateOne({ guild: guild, user: user }, { $set: { note: note } }, collectionOptions)
: await this.modNotes.insertOne({ guild: guild, user: user, note: note }, collectionOptions);
: await this.modNotes.insertOne({ guild: guild, user: user, note: note, flag: null }, collectionOptions);
}
async read(guild: string, user: string) {
// console.log("ModNotes read");
const entry = await this.modNotes.findOne({ guild: guild, user: user });
return entry?.note ?? null;
return entry ?? null;
}
async delete(guild: string) {
@ -1036,10 +1043,13 @@ export interface HistorySchema {
amount: string | null;
}
export type FlagColors = "red" | "yellow" | "green" | "blue" | "purple" | "gray";
export interface ModNoteSchema {
guild: string;
user: string;
note: string | null;
flag: FlagColors | null;
}
export interface PremiumSchema {

Loading…
Cancel
Save