mirror of
https://github.com/toast-ts/Daggerbot-TS.git
synced 2024-11-17 12:21:00 -05:00
Add tag system
This commit is contained in:
parent
066f769b55
commit
74ffcdba89
@ -1,5 +1,5 @@
|
|||||||
import Discord, {Client, WebhookClient, GatewayIntentBits, Partials} from 'discord.js';
|
import Discord, {Client, WebhookClient, GatewayIntentBits, Partials} from 'discord.js';
|
||||||
import {readFileSync, readdirSync, promises} from 'node:fs';
|
import {readFileSync, readdirSync} from 'node:fs';
|
||||||
import {exec} from 'node:child_process';
|
import {exec} from 'node:child_process';
|
||||||
import mongoose from 'mongoose';
|
import mongoose from 'mongoose';
|
||||||
import {formatTimeOpt, Tokens, Config, repeatedMessages, MPServerCache} from './typings/interfaces';
|
import {formatTimeOpt, Tokens, Config, repeatedMessages, MPServerCache} from './typings/interfaces';
|
||||||
@ -7,6 +7,7 @@ import bannedWords from './models/bannedWords.js';
|
|||||||
import userLevels from './models/userLevels.js';
|
import userLevels from './models/userLevels.js';
|
||||||
import suggestion from './models/suggestion.js';
|
import suggestion from './models/suggestion.js';
|
||||||
import punishments from './models/punishments.js';
|
import punishments from './models/punishments.js';
|
||||||
|
import tags from './models/tagSystem.js';
|
||||||
import bonkCount from './models/bonkCount.js';
|
import bonkCount from './models/bonkCount.js';
|
||||||
import MPServer from './models/MPServer.js';
|
import MPServer from './models/MPServer.js';
|
||||||
import xjs from 'xml-js';
|
import xjs from 'xml-js';
|
||||||
@ -46,6 +47,7 @@ export default class TClient extends Client {
|
|||||||
MPServer: MPServer;
|
MPServer: MPServer;
|
||||||
MPServerCache: MPServerCache;
|
MPServerCache: MPServerCache;
|
||||||
suggestion: suggestion;
|
suggestion: suggestion;
|
||||||
|
tags: tags;
|
||||||
repeatedMessages: repeatedMessages;
|
repeatedMessages: repeatedMessages;
|
||||||
statsGraph: number;
|
statsGraph: number;
|
||||||
|
|
||||||
@ -85,6 +87,7 @@ export default class TClient extends Client {
|
|||||||
this.MPServer = new MPServer(this);
|
this.MPServer = new MPServer(this);
|
||||||
this.MPServerCache = {players: [], status: null, name: null} as MPServerCache;
|
this.MPServerCache = {players: [], status: null, name: null} as MPServerCache;
|
||||||
this.suggestion = new suggestion(this);
|
this.suggestion = new suggestion(this);
|
||||||
|
this.tags = new tags(this);
|
||||||
this.repeatedMessages = {};
|
this.repeatedMessages = {};
|
||||||
this.setMaxListeners(45);
|
this.setMaxListeners(45);
|
||||||
this.statsGraph = -60;
|
this.statsGraph = -60;
|
||||||
|
133
src/commands/tag.ts
Normal file
133
src/commands/tag.ts
Normal file
@ -0,0 +1,133 @@
|
|||||||
|
import Discord from 'discord.js';
|
||||||
|
import TClient from '../client.js';
|
||||||
|
export default {
|
||||||
|
async autocomplete(client: TClient, interaction: Discord.AutocompleteInteraction){
|
||||||
|
const array = (await client.tags?._content.find())?.map(x=>x._id).filter(c=>c.startsWith(interaction.options.getFocused()));
|
||||||
|
await interaction?.respond(array?.map(c=>({name: c, value: c})));
|
||||||
|
// If you question all those '?.', let me tell you: Discord.JS is fricking stupid and I am too stressed to find a solution for it.
|
||||||
|
},
|
||||||
|
async run(client: TClient, interaction: Discord.ChatInputCommandInteraction<'cached'>){
|
||||||
|
if (!client.isStaff(interaction.member) && !client.config.whitelist.includes(interaction.member.id)) return client.youNeedRole(interaction, 'dcmod');
|
||||||
|
const tagData = async()=>await client.tags._content.findOne({_id: interaction.options.getString('name')});
|
||||||
|
const tagMeta = {
|
||||||
|
isEmbedTrue: async()=>(await tagData()).embedBool,
|
||||||
|
title: async()=>(await tagData())._id,
|
||||||
|
message: async()=>(await tagData()).message,
|
||||||
|
creatorName: async()=>(await client.users.fetch((await tagData()).user._id)).displayName
|
||||||
|
};
|
||||||
|
({
|
||||||
|
send: async()=>{
|
||||||
|
let targetField = '';
|
||||||
|
const targetMember = interaction.options.getMember('target_user');
|
||||||
|
if (targetMember) targetField = `*This tag is for <@${targetMember.id}>*`;
|
||||||
|
//console.log(targetMember)
|
||||||
|
const embedTemplate = new client.embed().setColor(client.config.embedColor).setTitle(await tagMeta.title()).setDescription(await tagMeta.message()).setFooter({text: `Tag creator: ${await tagMeta.creatorName()}`});
|
||||||
|
const messageTemplate = [
|
||||||
|
targetField ? targetField : '',
|
||||||
|
`**${await tagMeta.title()}**`,
|
||||||
|
await tagMeta.message(),
|
||||||
|
'',
|
||||||
|
`Tag creator: **${await tagMeta.creatorName()}**`
|
||||||
|
].join('\n');
|
||||||
|
if (await tagMeta.isEmbedTrue()) return interaction.reply({content: targetField ? targetField : null, embeds: [embedTemplate], allowedMentions:{parse:['users']}});
|
||||||
|
else return interaction.reply({content: messageTemplate, allowedMentions:{parse:['users']}})
|
||||||
|
},
|
||||||
|
create: async()=>await client.tags._content.create({
|
||||||
|
_id: interaction.options.getString('name'),
|
||||||
|
message: interaction.options.getString('message'),
|
||||||
|
embedBool: interaction.options.getBoolean('embed'),
|
||||||
|
user: {
|
||||||
|
_id: interaction.member.id,
|
||||||
|
name: interaction.user.username
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.then(()=>interaction.reply('Tag is now created and available to use.'))
|
||||||
|
.catch(err=>interaction.reply(`There was an error while trying to create your tag:\n\`\`\`${err}\`\`\``)),
|
||||||
|
delete: async()=>await client.tags._content.findByIdAndDelete(interaction.options.getString('name')).then(()=>interaction.reply('Tag successfully deleted.')).catch(err=>interaction.reply(`Failed to delete the tag:\n\`\`\`${err}\`\`\``)),
|
||||||
|
edit: async()=>await client.tags._content.findByIdAndUpdate(interaction.options.getString('name'), {
|
||||||
|
$set: {
|
||||||
|
message: interaction.options.getString('new-message'),
|
||||||
|
embedBool: interaction.options.getBoolean('embed')
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.then(()=>interaction.reply('Tag successfully updated, enjoy!'))
|
||||||
|
.catch(err=>interaction.reply(`Tag couldn\'t be updated:\n\`\`\`${err}\`\`\``))
|
||||||
|
} as any)[interaction.options.getSubcommand() ?? interaction.options.getSubcommandGroup()]();
|
||||||
|
},
|
||||||
|
data: new Discord.SlashCommandBuilder()
|
||||||
|
.setName('tag')
|
||||||
|
.setDescription('Send user the resources/FAQ provided in the tag')
|
||||||
|
.addSubcommand(x=>x
|
||||||
|
.setName('send')
|
||||||
|
.setDescription('Send a resource tag')
|
||||||
|
.addStringOption(x=>x
|
||||||
|
.setName('name')
|
||||||
|
.setDescription('Name of an existing tag to send')
|
||||||
|
.setAutocomplete(true)
|
||||||
|
.setRequired(true))
|
||||||
|
.addUserOption(x=>x
|
||||||
|
.setName('target_user')
|
||||||
|
.setDescription('Directly mention the target with this tag')))
|
||||||
|
.addSubcommandGroup(x=>x
|
||||||
|
.setName('management')
|
||||||
|
.setDescription('Add a new tag or delete/edit your current tag')
|
||||||
|
.addSubcommand(x=>x
|
||||||
|
.setName('create')
|
||||||
|
.setDescription('Create a new tag')
|
||||||
|
.addStringOption(x=>x
|
||||||
|
.setName('name')
|
||||||
|
.setDescription('Name of your tag, must be within 3-25 characters')
|
||||||
|
.setMinLength(3)
|
||||||
|
.setMaxLength(25)
|
||||||
|
.setRequired(true))
|
||||||
|
.addStringOption(x=>x
|
||||||
|
.setName('message')
|
||||||
|
.setDescription('Message to be included in your tag; e.g, you\'re giving the user some instructions')
|
||||||
|
.setMinLength(6)
|
||||||
|
.setMaxLength(2048)
|
||||||
|
.setRequired(true))
|
||||||
|
.addBooleanOption(x=>x
|
||||||
|
.setName('embed')
|
||||||
|
.setDescription('Toggle this option if you want your message to be inside the embed or not')
|
||||||
|
.setRequired(true)))
|
||||||
|
.addSubcommand(x=>x
|
||||||
|
.setName('delete')
|
||||||
|
.setDescription('Delete a tag')
|
||||||
|
.addStringOption(x=>x
|
||||||
|
.setName('name')
|
||||||
|
.setDescription('Name of the tag to be deleted')
|
||||||
|
.setAutocomplete(true)
|
||||||
|
.setRequired(true)))
|
||||||
|
.addSubcommand(x=>x
|
||||||
|
.setName('edit')
|
||||||
|
.setDescription('Edit an existing tag')
|
||||||
|
.addStringOption(x=>x
|
||||||
|
.setName('name')
|
||||||
|
.setDescription('Name of the tag to be edited')
|
||||||
|
.setAutocomplete(true)
|
||||||
|
.setRequired(true))
|
||||||
|
.addStringOption(x=>x
|
||||||
|
.setName('new-message')
|
||||||
|
.setDescription('Replace the current tag\'s message with a new one')
|
||||||
|
.setRequired(true))
|
||||||
|
.addBooleanOption(x=>x
|
||||||
|
.setName('embed')
|
||||||
|
.setDescription('Toggle this option on an existing tag to be updated with embed or not')
|
||||||
|
.setRequired(true))))
|
||||||
|
}
|
||||||
|
|
||||||
|
// createTag
|
||||||
|
/*if (interaction.options.getString('name') === await tagMeta.tagTitle()) return interaction.reply('This tag already exists!');
|
||||||
|
else {
|
||||||
|
await client.tags._content.create({
|
||||||
|
_id: interaction.options.getString('name'),
|
||||||
|
message: interaction.options.getString('message'),
|
||||||
|
embedBool: interaction.options.getBoolean('embed'),
|
||||||
|
user: {
|
||||||
|
_id: interaction.member.id,
|
||||||
|
name: interaction.user.username
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.then(()=>interaction.reply('Tag is now created and available to use.'))
|
||||||
|
.catch(err=>interaction.reply(`There was an error while trying to create your tag:\n\`\`\`${err}\`\`\``))
|
||||||
|
}*/
|
@ -1,21 +1,29 @@
|
|||||||
import Discord from 'discord.js';
|
import Discord from 'discord.js';
|
||||||
import TClient from '../client.js';
|
import TClient from '../client.js';
|
||||||
export default {
|
export default {
|
||||||
run(client:TClient, interaction:Discord.BaseInteraction){
|
async run(client:TClient, interaction:Discord.BaseInteraction){
|
||||||
if (!interaction.inGuild() || !interaction.inCachedGuild()) return;
|
if (!interaction.inGuild() || !interaction.inCachedGuild()) return;
|
||||||
if (interaction.isChatInputCommand()){
|
if (interaction.isChatInputCommand()){
|
||||||
const commandFile = client.commands.get(interaction.commandName);
|
const commandFile = client.commands.get(interaction.commandName);
|
||||||
console.log(client.logTime(), `${interaction.user.username} used /${interaction.commandName} ${interaction.options.getSubcommand(false) ?? ''} in #${interaction.channel.name}`);
|
console.log(client.logTime(), `${interaction.user.username} used /${interaction.commandName} ${interaction.options.getSubcommandGroup(false) ?? ''} ${interaction.options.getSubcommand(false) ?? ''} in #${interaction.channel.name}`);
|
||||||
if (!client.config.botSwitches.commands && !client.config.whitelist.includes(interaction.user.id)) return interaction.reply({content: `I am currently operating in development mode.\nPlease notify <@${client.config.whitelist[0]}> if this is a mistake.`, ephemeral: true});
|
if (!client.config.botSwitches.commands && !client.config.whitelist.includes(interaction.user.id)) return interaction.reply({content: `I am currently operating in development mode.\nPlease notify <@${client.config.whitelist[0]}> if this is a mistake.`, ephemeral: true});
|
||||||
if (commandFile){
|
if (commandFile){
|
||||||
try{
|
try{
|
||||||
commandFile.command.default.run(client, interaction);
|
commandFile.command.default.run(client, interaction);
|
||||||
|
commandFile.command.default.autocomplete ? commandFile.command.default.autocomplete(interaction) : undefined;
|
||||||
commandFile.uses ? commandFile.uses++ : commandFile.uses = 1;
|
commandFile.uses ? commandFile.uses++ : commandFile.uses = 1;
|
||||||
} catch (error){
|
} catch (error){
|
||||||
console.log(`An error occurred while running command "${interaction.commandName} ${interaction.options.getSubcommand(false) ?? ''}"`, error, error.stack);
|
console.log(`An error occurred while running command "${interaction.commandName} ${interaction.options.getSubcommand(false) ?? ''}"`, error, error.stack);
|
||||||
return interaction.reply('An error occurred while executing that command.');
|
return interaction.reply('An error occurred while executing that command.');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
} else if (interaction.isAutocomplete()){
|
||||||
|
const AC = client.commands.get(interaction.commandName);
|
||||||
|
try {
|
||||||
|
await AC.command.default.autocomplete(client, interaction)
|
||||||
|
} catch (error){
|
||||||
|
return console.log('An error occurred while running autocomplete:\n', error)
|
||||||
|
}
|
||||||
} else if (interaction.isButton()){
|
} else if (interaction.isButton()){
|
||||||
if (interaction.customId.startsWith('reaction-') && client.config.botSwitches.buttonRoles){
|
if (interaction.customId.startsWith('reaction-') && client.config.botSwitches.buttonRoles){
|
||||||
const RoleID = interaction.customId.replace('reaction-','');
|
const RoleID = interaction.customId.replace('reaction-','');
|
||||||
|
22
src/models/tagSystem.ts
Normal file
22
src/models/tagSystem.ts
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
import TClient from '../client.js';
|
||||||
|
import mongoose from 'mongoose';
|
||||||
|
|
||||||
|
const Schema = mongoose.model('tags', new mongoose.Schema({
|
||||||
|
_id: {type: String, required:true},
|
||||||
|
message: {type: String, required:true},
|
||||||
|
embedBool: {type: Boolean, required:true},
|
||||||
|
user: {required:true, type: new mongoose.Schema({
|
||||||
|
name: {type: String, required:true},
|
||||||
|
_id: {type: String, required:true}
|
||||||
|
}, {versionKey: false})}
|
||||||
|
}, {versionKey: false}));
|
||||||
|
|
||||||
|
export default class tags extends Schema {
|
||||||
|
client: TClient;
|
||||||
|
_content: typeof Schema;
|
||||||
|
constructor(client:TClient){
|
||||||
|
super();
|
||||||
|
this.client = client;
|
||||||
|
this._content = Schema;
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user