File size: 1,605 Bytes
ec83114
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
const { PermissionsBitField } = require("discord.js");

async function cleanDiscordMessageContent(messageContent: any) {
  messageContent = messageContent.replace(/<@!?\d+>/g, "");
  messageContent = messageContent.replace(/<#\d+>/g, "");
  messageContent = messageContent.replace(/<@&\d+>/g, "");
  messageContent = messageContent.replace(/<a?:\w+:\d+>|[\x00-\x19]/g, "");
  messageContent = messageContent.replace(/[^ -~]+/g, "");
  return messageContent.trim();
}

module.exports = async (
  client: any,
  message: any,
  chatClient: any,
  characterName: string
) => {
  if (message.author.bot || !message.content) {
    return;
  }
  if (!(message.channel.id === process.env.MSGCHANNEL)) {
    return;
  }

  try {
    let msgContent = await cleanDiscordMessageContent(message.content);
    if (!chatClient) {
      return;
    }
    try {
      if (
        !message.member
          .permissionsIn(message.channel)
          .has(PermissionsBitField.Flags.KickMembers)
      ) {
        message?.member?.setNickname(`Talking to: ${characterName}`);
      }
    } catch (error) {
      console.log(error);
    }
    await message.channel.permissionOverwrites.edit(
      message.channel.guild.roles.everyone,
      { SendMessages: false }
    );
    const response = await chatClient.sendAndAwaitResponse(
      `${msgContent}`,
      true
    );
    await message.channel.send(`${response.text}`);
    await message.channel.permissionOverwrites.edit(
      message.channel.guild.roles.everyone,
      { SendMessages: true }
    );

    return;
  } catch (error) {
    console.error(error);
  }
};