Skip to content
Open
Show file tree
Hide file tree
Changes from 2 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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,5 @@ typings/
dist/

.idea

package-lock.json
9 changes: 4 additions & 5 deletions src/modules/etc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
listener,
} from 'cookiecord';
import { Message } from 'discord.js';
import { pingPong, dontAskToAsk, reactfcMsg } from './msg';

export class EtcModule extends Module {
constructor(client: CookiecordClient) {
Expand All @@ -13,22 +14,20 @@ export class EtcModule extends Module {

@command({ description: 'See if the bot is alive' })
async ping(msg: Message) {
await msg.channel.send('pong. :ping_pong:');
await msg.channel.send(pingPong);
}

@command({ description: 'Sends a link to <https://dontasktoask.com>' })
async ask(msg: Message) {
await msg.channel.send('https://dontasktoask.com/');
await msg.channel.send(dontAskToAsk);
}

@command({
description:
'Sends a link to <https://github.com/facebook/create-react-app/pull/8177#issue-353062710>',
})
async reactfc(msg: Message) {
await msg.channel.send(
'https://github.com/facebook/create-react-app/pull/8177#issue-353062710',
);
await msg.channel.send(reactfcMsg);
}

@listener({ event: 'message' })
Expand Down
14 changes: 6 additions & 8 deletions src/modules/helpchan.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ import {
dormantChannelLoop,
} from '../env';

import { beAskerToCloseChannel, okHand, onlyRunInHelp } from './msg';

export class HelpChanModule extends Module {
constructor(client: CookiecordClient) {
super(client);
Expand Down Expand Up @@ -143,9 +145,7 @@ export class HelpChanModule extends Module {
return;

if (msg.channel.parentID !== categories.ongoing) {
return await msg.channel.send(
':warning: you can only run this in ongoing help channels.',
);
return await msg.channel.send(onlyRunInHelp);
}

const owner = await HelpUser.findOne({
Expand All @@ -158,9 +158,7 @@ export class HelpChanModule extends Module {
) {
await this.markChannelAsDormant(msg.channel);
} else {
return await msg.channel.send(
':warning: you have to be the asker to close the channel.',
);
return await msg.channel.send(beAskerToCloseChannel);
}
}

Expand Down Expand Up @@ -293,7 +291,7 @@ export class HelpChanModule extends Module {
})
async removelock(msg: Message) {
this.busyChannels.delete(msg.channel.id);
await msg.channel.send(':ok_hand:');
await msg.channel.send(okHand);
}

@command({
Expand All @@ -303,6 +301,6 @@ export class HelpChanModule extends Module {
if (!msg.guild) return;

await this.ensureAskChannels(msg.guild);
await msg.channel.send(':ok_hand:');
await msg.channel.send(okHand);
}
}
35 changes: 35 additions & 0 deletions src/modules/msg.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
const okHand: string = ':ok_hand:';
const pingPong: string = 'pong. :ping_pong:';
const dontAskToAsk: string = 'https://dontasktoask.com/';
const reactfcMsg: string =
'https://github.com/facebook/create-react-app/pull/8177#issue-353062710';
const onlyRunInHelp: string =
':warning: you can only run this in ongoing help channels.';
const beAskerToCloseChannel: string =
':warning: you have to be the asker to close the channel.';
const couldntFindCodeblock: string = ":warning: couldn't find a codeblock!";
const canRemoveFullLink: string =
"Here's a shortened URL of your playground link! You can remove the full link from your message.";
const syntaxWarning: string = ':warning: syntax: !remind <duration> [message]';
const invalidDuration: string = ':warning: invalid duration!';
const cannotSendRepToYou: string = ':x: you cannot send rep to yourself';
const noRepRemain: string = ':warning: no rep remaining! come back later.';
const needValidSymbol: string =
'You need to give me a valid symbol name to look for!';
const noTypescriptCode: string =
':warning: could not find any TypeScript codeblocks in the past 10 messages';

export { okHand };
export { pingPong };
export { dontAskToAsk };
export { reactfcMsg };
export { onlyRunInHelp };
export { beAskerToCloseChannel };
export { couldntFindCodeblock };
export { canRemoveFullLink };
export { syntaxWarning };
export { invalidDuration };
export { cannotSendRepToYou };
export { noRepRemain };
export { needValidSymbol };
export { noTypescriptCode };
8 changes: 3 additions & 5 deletions src/modules/playground.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
findCodeblockFromChannel,
PLAYGROUND_REGEX,
} from '../util/findCodeblockFromChannel';
import { couldntFindCodeblock, canRemoveFullLink } from './msg';

export class PlaygroundModule extends Module {
constructor(client: CookiecordClient) {
Expand All @@ -33,10 +34,7 @@ export class PlaygroundModule extends Module {
msg.channel as TextChannel,
true,
);
if (!code)
return await msg.channel.send(
":warning: couldn't find a codeblock!",
);
if (!code) return await msg.channel.send(couldntFindCodeblock);
}
const embed = new MessageEmbed()
.setURL(PLAYGROUND_BASE + compressToEncodedURIComponent(code))
Expand All @@ -61,7 +59,7 @@ export class PlaygroundModule extends Module {
} else {
// Message also contained other characters
const botMsg = await msg.channel.send(
`${msg.author} Here's a shortened URL of your playground link! You can remove the full link from your message.`,
`${msg.author} ${canRemoveFullLink}`,
{ embed },
);
this.editedLongLink.set(msg.id, botMsg);
Expand Down
12 changes: 5 additions & 7 deletions src/modules/reminders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { getDB } from '../db';
import prettyMs from 'pretty-ms';
import { setTimeout } from 'timers';
import { Reminder } from '../entities/Reminder';
import { invalidDuration, okHand, syntaxWarning } from './msg';

export class ReminderModule extends Module {
constructor(client: CookiecordClient) {
Expand Down Expand Up @@ -39,14 +40,11 @@ export class ReminderModule extends Module {
async remind(msg: Message, args: string) {
// cookiecord can't have args with spaces in them (yet)
const splitArgs = args.split(' ').filter(x => x.trim().length !== 0);
if (splitArgs.length == 0)
return await msg.channel.send(
':warning: syntax: !remind <duration> [message]',
);
if (splitArgs.length == 0) return await msg.channel.send(syntaxWarning);
const maxDur = parse('10yr');
const dur = parse(splitArgs.shift()!); // TS doesn't know about the length check
if (!dur || !maxDur || dur > maxDur)
return await msg.channel.send(':warning: invalid duration!');
return await msg.channel.send(invalidDuration);

const reminder = new Reminder();
reminder.userID = msg.author.id;
Expand All @@ -57,11 +55,11 @@ export class ReminderModule extends Module {

if (splitArgs.length == 0) {
await msg.channel.send(
`:ok_hand: set a reminder for ${prettyMs(dur)}.`,
`${okHand} set a reminder for ${prettyMs(dur)}.`,
);
} else {
await msg.channel.send(
`:ok_hand: set a reminder for ${prettyMs(
`${okHand} set a reminder for ${prettyMs(
dur,
)} to remind you about "${splitArgs.join('')}".`,
);
Expand Down
9 changes: 4 additions & 5 deletions src/modules/rep.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { TS_BLUE } from '../env';

import { RepGive } from '../entities/RepGive';
import { RepUser } from '../entities/RepUser';
import { cannotSendRepToYou, noRepRemain, okHand } from './msg';

export class RepModule extends Module {
constructor(client: CookiecordClient) {
Expand Down Expand Up @@ -95,23 +96,21 @@ export class RepModule extends Module {
@command({ description: 'Give a different user some reputation points' })
async rep(msg: Message, targetMember: GuildMember) {
if (targetMember.id === msg.member?.id)
return msg.channel.send(`:x: you cannot send rep to yourself`);
return msg.channel.send(cannotSendRepToYou);

const senderRU = await this.getOrMakeUser(msg.author);
const targetRU = await this.getOrMakeUser(targetMember.user);

if ((await senderRU.sent()) >= this.MAX_REP)
return await msg.channel.send(
':warning: no rep remaining! come back later.',
);
return await msg.channel.send(noRepRemain);

await RepGive.create({
from: senderRU,
to: targetRU,
}).save();

await msg.channel.send(
`:ok_hand: sent \`${targetMember.displayName}\` 1 rep (${
`${okHand} sent \`${targetMember.displayName}\` 1 rep (${
(await senderRU.sent()) + 1
}/${this.MAX_REP} sent)`,
);
Expand Down
15 changes: 4 additions & 11 deletions src/modules/twoslash.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { command, Module, listener } from 'cookiecord';
import { Message, TextChannel } from 'discord.js';
import { twoslasher } from '@typescript/twoslash';
import { findCodeFromChannel } from '../util/findCodeblockFromChannel';
import { needValidSymbol, noTypescriptCode } from './msg';

const CODEBLOCK = '```';

Expand All @@ -15,20 +16,15 @@ export class TwoslashModule extends Module {
const match = /^[_$a-zA-Z][_$0-9a-zA-Z]*/.exec(content);

if (!match) {
msg.channel.send(
'You need to give me a valid symbol name to look for!',
);
msg.channel.send(needValidSymbol);
return;
}

const symbol = match[0];

const code = await findCodeFromChannel(msg.channel as TextChannel);

if (!code)
return msg.channel.send(
`:warning: could not find any TypeScript codeblocks in the past 10 messages`,
);
if (!code) return msg.channel.send(noTypescriptCode);

const ret = twoslasher(code, 'ts', {
defaultOptions: { noErrorValidation: true },
Expand All @@ -54,10 +50,7 @@ export class TwoslashModule extends Module {
async twoslash(msg: Message) {
const code = await findCodeFromChannel(msg.channel as TextChannel);

if (!code)
return msg.channel.send(
`:warning: could not find any TypeScript codeblocks in the past 10 messages`,
);
if (!code) return msg.channel.send(noTypescriptCode);

return this.twoslashBlock(msg, code);
}
Expand Down