Browse Source

Improve message handling

Fix the checking of conditions in order to execute commands.
master
TheoryOfNekomata 2 years ago
parent
commit
cc2e430080
4 changed files with 23 additions and 25 deletions
  1. +10
    -7
      src/commands/pin.ts
  2. +4
    -1
      src/commands/save.ts
  3. +8
    -8
      src/index.ts
  4. +1
    -9
      src/utils/common.ts

+ 10
- 7
src/commands/pin.ts View File

@@ -4,14 +4,11 @@ import messages from '../messages.json';
import { transferPin } from '../utils/common';

const pin = async (
message: Message, mainTextChannel: TextChannel, pinTextChannel: TextChannel, silent = false,
message: Message,
mainTextChannel: TextChannel,
pinTextChannel: TextChannel,
silent = false,
): Promise<void> => {
const isReply = message.type === 'REPLY';
if (!isReply) {
await message.reply(messages.NOTHING_TO_PIN);
return;
}

const authorMember = await message.guild?.members.fetch(message.author);
if (!authorMember) {
await message.reply(messages.INVALID_PERMISSIONS);
@@ -24,6 +21,12 @@ const pin = async (
return;
}

const isReply = message.type === 'REPLY';
if (!isReply) {
await message.reply(messages.NOTHING_TO_PIN);
return;
}

const reply = await message.fetchReference();
await transferPin(reply, pinTextChannel, silent);
await message.reply(messages.PIN_COMPLETE);


+ 4
- 1
src/commands/save.ts View File

@@ -4,7 +4,10 @@ import messages from '../messages.json';
import { transferPin } from '../utils/common';

const save = async (
message: Message, mainTextChannel: TextChannel, pinTextChannel: TextChannel, silent = true,
message: Message,
mainTextChannel: TextChannel,
pinTextChannel: TextChannel,
silent = true,
): Promise<void> => {
const authorMember = await message.guild?.members.fetch(message.author);
if (!authorMember) {


+ 8
- 8
src/index.ts View File

@@ -29,10 +29,10 @@ const listen = async (channelIDMap: Map<string, string>) => {
const mainTextChannel = mainChannel as TextChannel;
const pinTextChannel = pinChannel as TextChannel;

const clientUser = client.user;
if (!clientUser) {
if (!client.user) {
return;
}
const clientUser = client.user;

if (message.type === 'CHANNEL_PINNED_MESSAGE') {
const reply = await message.fetchReference();
@@ -47,11 +47,6 @@ const listen = async (channelIDMap: Map<string, string>) => {
return;
}

const mentioned = message.mentions.users.has(clientUser.id);
if (!mentioned) {
return;
}

switch (message.content) {
case getCommandPattern(clientUser, 'pin'):
await pin(message, mainTextChannel, pinTextChannel);
@@ -63,7 +58,12 @@ const listen = async (channelIDMap: Map<string, string>) => {
break;
}

await message.reply(messages.UNKNOWN_COMMAND);
if (
message.content.startsWith(`<@!${clientUser.id}>`)
|| message.content.startsWith(`<@${clientUser.id}>`)
) {
await message.reply(messages.UNKNOWN_COMMAND);
}
});
});



+ 1
- 9
src/utils/common.ts View File

@@ -9,15 +9,7 @@ export const transferPin = async (
: reply.author.toString()}:
${reply.content}`,
embeds: Array.from(reply.attachments.values())
.map((a) => ({
image: {
height: a.height as number,
url: a.url,
width: a.width as number,
proxy_url: a.proxyURL,
},
})),
files: Array.from(reply.attachments.values()),
});
};



Loading…
Cancel
Save