Initial code
This commit is contained in:
commit
1f20a611da
26 changed files with 1050 additions and 0 deletions
55
src/modules/global.ts
Normal file
55
src/modules/global.ts
Normal file
|
|
@ -0,0 +1,55 @@
|
|||
import type { MatrixClient } from "matrix-js-sdk";
|
||||
import type { TRole } from "../store/types.js";
|
||||
import { getRank, getUserById } from "../helpers.js";
|
||||
import { config } from "../config.js";
|
||||
import { state } from "../store/store.js";
|
||||
|
||||
const onAnyMessage = (
|
||||
client: MatrixClient,
|
||||
_text: string,
|
||||
roomId: string,
|
||||
sender: string,
|
||||
) => {
|
||||
const date = Date.now();
|
||||
|
||||
const user = getUserById(sender);
|
||||
if (user.id === ":") {
|
||||
state.users.push({
|
||||
id: sender,
|
||||
role: "USER",
|
||||
experience: 0,
|
||||
lastMessageTimestamp: date,
|
||||
});
|
||||
return onAnyMessage(client, _text, roomId, sender);
|
||||
}
|
||||
|
||||
const rankBefore = getRank(user.experience);
|
||||
|
||||
if (date > user.lastMessageTimestamp + config.app.experience.timeout) {
|
||||
user.experience += config.app.experience.gain;
|
||||
}
|
||||
user.lastMessageTimestamp = date;
|
||||
|
||||
const rankAfter = getRank(user.experience);
|
||||
if (rankAfter.rank > rankBefore.rank) {
|
||||
client.sendHtmlMessage(
|
||||
roomId,
|
||||
"",
|
||||
`${sender} - You are now rank <b>${rankAfter.rank}</b>`,
|
||||
);
|
||||
}
|
||||
};
|
||||
|
||||
const onMissingRole = (
|
||||
client: MatrixClient,
|
||||
userRole: TRole,
|
||||
roomId: string,
|
||||
) => {
|
||||
client.sendHtmlMessage(
|
||||
roomId,
|
||||
"",
|
||||
`You are missing the required role.<br/>Your current role is <b>${userRole}</b>`,
|
||||
);
|
||||
};
|
||||
|
||||
export { onAnyMessage, onMissingRole };
|
||||
Loading…
Add table
Add a link
Reference in a new issue