notes cmd

merge-requests/15/head
Matthew 2020-07-23 08:32:54 -04:00
parent ba3b71d0b1
commit 42520eecfa
No known key found for this signature in database
GPG Key ID: 210AF32ADE3B5C4B
6 changed files with 170 additions and 3 deletions

View File

@ -2,7 +2,7 @@ import eris from 'eris';
import mongoose from 'mongoose'; import mongoose from 'mongoose';
import { promises as fs } from 'fs'; import { promises as fs } from 'fs';
import { Collection, Command, LocalStorage, Util, ServerManagement, Event } from '.'; import { Collection, Command, LocalStorage, Util, ServerManagement, Event } from '.';
import { File, FileInterface, Member, MemberInterface, Moderation, ModerationInterface, PagerNumber, PagerNumberInterface, Rank, RankInterface, Redirect, RedirectInterface, Stat, StatInterface } from '../models'; import { File, FileInterface, Member, MemberInterface, Moderation, ModerationInterface, Note, NoteInterface, PagerNumber, PagerNumberInterface, Rank, RankInterface, Redirect, RedirectInterface, Stat, StatInterface } from '../models';
import { Config } from '../../types'; // eslint-disable-line import { Config } from '../../types'; // eslint-disable-line
export default class Client extends eris.Client { export default class Client extends eris.Client {
@ -18,14 +18,14 @@ export default class Client extends eris.Client {
public serverManagement: ServerManagement; public serverManagement: ServerManagement;
public db: { File: mongoose.Model<FileInterface>, Member: mongoose.Model<MemberInterface>, Moderation: mongoose.Model<ModerationInterface>, PagerNumber: mongoose.Model<PagerNumberInterface>, Rank: mongoose.Model<RankInterface>, Redirect: mongoose.Model<RedirectInterface>, Stat: mongoose.Model<StatInterface>, local: { muted: LocalStorage } }; public db: { File: mongoose.Model<FileInterface>, Member: mongoose.Model<MemberInterface>, Moderation: mongoose.Model<ModerationInterface>, Note: mongoose.Model<NoteInterface>, PagerNumber: mongoose.Model<PagerNumberInterface>, Rank: mongoose.Model<RankInterface>, Redirect: mongoose.Model<RedirectInterface>, Stat: mongoose.Model<StatInterface>, local: { muted: LocalStorage } };
constructor(token: string, options?: eris.ClientOptions) { constructor(token: string, options?: eris.ClientOptions) {
super(token, options); super(token, options);
this.commands = new Collection<Command>(); this.commands = new Collection<Command>();
this.events = new Collection<Event>(); this.events = new Collection<Event>();
this.intervals = new Collection<NodeJS.Timeout>(); this.intervals = new Collection<NodeJS.Timeout>();
this.db = { File, Member, Moderation, PagerNumber, Rank, Redirect, Stat, local: { muted: new LocalStorage('muted') } }; this.db = { File, Member, Moderation, Note, PagerNumber, Rank, Redirect, Stat, local: { muted: new LocalStorage('muted') } };
} }
public async loadDatabase() { public async loadDatabase() {

38
src/commands/addnote.ts Normal file
View File

@ -0,0 +1,38 @@
import { Message } from 'eris';
import { Command, Client } from '../class';
export default class AddNote extends Command {
constructor(client: Client) {
super(client);
this.name = 'addnote';
this.description = 'Adds a note to a member.';
this.usage = `${this.client.config.prefix}addnote <member> <text> [category: comm | cs | edu]`;
this.permissions = 1;
this.guildOnly = true;
this.enabled = true;
}
public async run(message: Message, args: string[]) {
try {
if (!args[0] || args.length < 1) return this.client.commands.get('help').run(message, [this.name]);
const member = this.client.util.resolveMember(args[0], this.mainGuild);
if (!member) return this.error(message.channel, 'The member you specified could not be found.');
const note: { userID?: string, date: Date, category?: string, text?: string } = {
userID: member.user.id,
date: new Date(),
};
if (args[args.length - 1] !== 'edu' && args[args.length - 1] !== 'comm' && args[args.length - 1] !== 'cs') {
note.category = '';
note.text = args.slice(1).join(' ');
} else {
note.category = args[args.length - 1];
note.text = args.slice(0, args.length - 1).join(' ');
}
const saved = await (new this.client.db.Note(note).save());
return this.success(message.channel, `Successfully created Note # \`${saved._id}\`.`);
} catch (err) {
return this.client.util.handleError(err, message, this);
}
}
}

26
src/commands/delnote.ts Normal file
View File

@ -0,0 +1,26 @@
import { Message } from 'eris';
import { Command, Client } from '../class';
export default class DelNote extends Command {
constructor(client: Client) {
super(client);
this.name = 'delnote';
this.description = 'Deletes a note.';
this.usage = `${this.client.config.prefix}delnote <note id>`;
this.permissions = 1;
this.guildOnly = true;
this.enabled = true;
}
public async run(message: Message, args: string[]) {
try {
if (!args[0]) return this.client.commands.get('help').run(message, [this.name]);
const note = await this.client.db.Note.findOne({ _id: args[0] }).lean().exec().catch(() => {});
if (!note) return this.error(message.channel, 'Could not locate that note.');
await this.client.db.Note.deleteOne({ _id: note._id });
return this.success(message.channel, `Note # \`${note._id}\` has been deleted.`);
} catch (err) {
return this.client.util.handleError(err, message, this);
}
}
}

85
src/commands/notes.ts Normal file
View File

@ -0,0 +1,85 @@
import { Message, Member, User } from 'eris';
import { createPaginationEmbed } from 'eris-pagination';
import { Command, Client, RichEmbed } from '../class';
export default class Notes extends Command {
constructor(client: Client) {
super(client);
this.name = 'notes';
this.description = 'Pulls up notes for a member, with an optional categorical filter.';
this.usage = `${this.client.config.prefix}notes <member> [filter: comm | cs | edu]`;
this.permissions = 1;
this.guildOnly = true;
this.enabled = true;
}
public async run(message: Message, args: string[]) {
try {
if (!args[0]) return this.client.commands.get('help').run(message, [this.name]);
let member: Member | User = this.client.util.resolveMember(args[0], this.mainGuild);
if (!member) member = await this.client.getRESTUser(args[0]);
if (!member) return this.error(message.channel, 'User specified could not be found.');
const notes = await this.client.db.Note.find({ userID: member.id });
if (!notes || notes?.length < 1) return this.error(message.channel, 'No notes exist for this user.');
const noteArray: [{ name: string, value: string, inline: boolean }?] = [];
if (args[1] === 'comm' || args[1] === 'cs' || args[1] === 'edu') {
switch (args[1]) {
case 'comm':
for (const note of notes.sort((a, b) => b.date.getTime() - a.date.getTime()).filter((r) => r.category === 'comm')) {
noteArray.push({
name: `${note._id}${note.category === '' ? '' : `, ${note.category}`} | ${note.date.toLocaleString('en-us')} ET`,
value: note.text,
inline: true,
});
}
break;
case 'cs':
for (const note of notes.sort((a, b) => b.date.getTime() - a.date.getTime()).filter((r) => r.category === 'cs')) {
noteArray.push({
name: `${note._id}${note.category === '' ? '' : `, ${note.category}`} | ${note.date.toLocaleString('en-us')} ET`,
value: note.text,
inline: true,
});
}
break;
case 'edu':
for (const note of notes.sort((a, b) => b.date.getTime() - a.date.getTime()).filter((r) => r.category === 'edu')) {
noteArray.push({
name: `${note._id}${note.category === '' ? '' : `, ${note.category}`}} | ${note.date.toLocaleString('en-us')} ET`,
value: note.text,
inline: true,
});
}
break;
default:
break;
}
} else {
for (const note of notes.sort((a, b) => b.date.getTime() - a.date.getTime())) {
noteArray.push({
name: `${note._id}${note.category === '' ? '' : `, ${note.category}`} | ${note.date.toLocaleString('en-us')} ET`,
value: note.text,
inline: true,
});
}
}
const noteSplit = this.client.util.splitFields(noteArray);
const cmdPages: RichEmbed[] = [];
noteSplit.forEach((split) => {
const embed = new RichEmbed();
embed.setColor('#0000FF');
embed.setAuthor(`${member.username}#${member.discriminator}`, member.avatarURL);
embed.setFooter(this.client.user.username, this.client.user.avatarURL);
embed.setTimestamp();
split.forEach((c) => embed.addField(c.name, c.value, c.inline));
return cmdPages.push(embed);
});
if (cmdPages.length === 1) return message.channel.createMessage({ embed: cmdPages[0] });
return createPaginationEmbed(message, cmdPages);
} catch (err) {
return this.client.util.handleError(err, message, this);
}
}
}

17
src/models/Note.ts Normal file
View File

@ -0,0 +1,17 @@
import { Document, Schema, model } from 'mongoose';
export interface NoteInterface extends Document {
userID: string,
date: Date,
category: 'comm' | 'cs' | 'edu' | '',
text: string,
}
const Note: Schema = new Schema({
userID: String,
date: Date,
category: String,
text: String,
});
export default model<NoteInterface>('Note', Note);

View File

@ -1,6 +1,7 @@
export { default as File, FileInterface } from './File'; export { default as File, FileInterface } from './File';
export { default as Member, MemberInterface } from './Member'; export { default as Member, MemberInterface } from './Member';
export { default as Moderation, ModerationInterface } from './Moderation'; export { default as Moderation, ModerationInterface } from './Moderation';
export { default as Note, NoteInterface } from './Note';
export { default as PagerNumber, PagerNumberInterface, PagerNumberRaw } from './PagerNumber'; export { default as PagerNumber, PagerNumberInterface, PagerNumberRaw } from './PagerNumber';
export { default as Rank, RankInterface } from './Rank'; export { default as Rank, RankInterface } from './Rank';
export { default as Redirect, RedirectInterface, RedirectRaw } from './Redirect'; export { default as Redirect, RedirectInterface, RedirectRaw } from './Redirect';