summaryrefslogtreecommitdiff
path: root/discordjs/src/index.mjs
blob: f0a4bae73d814f05668c0d2b12d55e7f1234a60b (plain)
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
// Require the necessary discord.js classes
import { Client, GatewayIntentBits } from 'discord.js';
import { request } from "undici";

// Symbol definition
const SYMBOL_FOR_CREATE = Symbol.for("CREATE");
const SYMBOL_FOR_UPDATE = Symbol.for("UPDATE");

// Create a new client instance
const client = new Client({ intents:
  [
    GatewayIntentBits.Guilds,
    GatewayIntentBits.MessageContent,
    GatewayIntentBits.GuildMessages,
    GatewayIntentBits.DirectMessages
  ]
});

// `autofeur_db` service
export const DB = process.env.DB || "http://localhost:3000";

/**
 * Completes a grapheme using the `autofeur_db` service.
 * @param grapheme Grapheme to complete
 * @returns Completed grapheme
 */
export const completeWord = (grapheme) => {
  let resp = request(`${DB}?grapheme=${encodeURIComponent(grapheme)}`);
  return resp.then((x) => {
    if (x.statusCode === 200) {
      return x.body.text();
    }
  });
};

/**
 * Cleans a sentence for usage with this program, strips unwanted chars
 * @param sentence Raw discord sentence
 * @returns The last word without any specials characters
 */
const sanitizeWord = (sentence) => {
  let lastWord = sentence
    .trim()
    .split(" ")
    .slice(-1)[0]
    .replaceAll(/(?:https?|ftp):\/\/[\n\S]+/g, "")
    .replaceAll(/\:([a-z]|[A-Z])+\:/g, "")
    .replaceAll(/(\?|\!|\.|\,|\;)/g, "")
    .replaceAll(/([^A-z])/g, "");
  return lastWord;
};
const anyTrivialCharRegex = /([a-z]|[A-Z])+/g;
const countChars = (str) => ((str || '').match(anyTrivialCharRegex) || []).length;

const specialChannels = [
  "1248226018406301696",
  "995243987948544090"
]

const ignoredChannels = [
  "1055130476395909210"
]

let messageReplyCounter = 0;
const messageAction = async (message, ctx) => {
  if (message.author.bot) return;

  messageReplyCounter += 1;
  console.log("counter is at", messageReplyCounter);

  let currentTimestamp = Date.now();
  let lastMessageTimestamp = (await message
    .channel
    .messages
    .fetch({
      limit: 2,
      cache : false
    }))
    .last()
    .createdTimestamp;
  
  let shouldReplyByTimestamp = currentTimestamp - lastMessageTimestamp >= 3600000;
  let shouldReplyByCounter =
    messageReplyCounter >= Math.floor(Math.random() * 75) + 35;
  let shouldReply = (
    (ctx === SYMBOL_FOR_CREATE && shouldReplyByTimestamp) ||
    shouldReplyByCounter ||
    specialChannels.includes(message.channelId) ||
    message.guild == null ||
    message.mentions.has(client.user)
  );

  if (shouldReply && !ignoredChannels.includes(message.channelId)) {
    let oldCounter = messageReplyCounter;
    if (shouldReplyByTimestamp || shouldReplyByCounter) {
      messageReplyCounter = 0;
    }
    const cleanText = sanitizeWord(message.cleanContent);
    if (countChars(cleanText) > 0) {
      let response = await completeWord(cleanText);

      // Ignore if there is no completion
      if ((response || response === "")) {
        message.reply(response);
      }
    } else if (shouldReplyByCounter) {
      messageReplyCounter = oldCounter;
    }
  }
  
  if (message.content.includes("@everyone") && !ignoredChannels.includes(message.channelId)) {
    message.reply("https://cdn.mpgn.dev/pascontent-gabe.gif");
  }
  if (message.content.includes(" allo ")) {
    message.reply("a l'huile")
  }
  if (message.content.includes("<:quoi:1061204752542748742>")) {
    message.reply("<:quoi:1061204752542748742>")
  }
};

client.on("messageCreate", message =>
  messageAction(message, SYMBOL_FOR_CREATE)
);
client.on("messageUpdate", (_, message) =>
  messageAction(message, SYMBOL_FOR_UPDATE)
);

client.login(process.env.TOKEN);