Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce reactions #428

Merged
merged 3 commits into from
Aug 15, 2024
Merged
Changes from 1 commit
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
18 changes: 15 additions & 3 deletions src/bot/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,12 @@ const sendMessage = (roomId: string, msg: string, formattedMsg?: string) => {
bot.sendEvent(roomId, null, "m.room.message", msgObject, "").catch((e) => logger.error(e));
};

const sendReaction = (roomId: string, eventId: string, emoji: string) => {
const msgObject: mSDK.IContent = { "m.relates_to": { event_id: eventId, key: emoji, rel_type: "m.annotation" } };

bot.sendEvent(roomId, null, "m.reaction", msgObject, "").catch((e) => logger.error(e));
};

const printHelpMessage = (roomId: string, message = "") =>
sendMessage(
roomId,
Expand All @@ -73,11 +79,12 @@ bot.on(mSDK.RoomEvent.MyMembership, (room: mSDK.Room, membership: string) => {
bot.on(mSDK.RoomEvent.Timeline, (event: mSDK.MatrixEvent) => {
const sender = event.getSender();
const roomId = event.getRoomId();
const eventId = event.getId();
const { body } = event.getContent<{ body: string }>();

if (roomId === undefined) {
if (roomId === undefined || eventId == undefined) {
// Should never happen for a "Room.timeline" event
throw new Error("roomId is not defined");
throw new Error("roomId or eventId is not defined");
}

// only act on messages
Expand Down Expand Up @@ -114,8 +121,10 @@ bot.on(mSDK.RoomEvent.Timeline, (event: mSDK.MatrixEvent) => {
logger.error("⭕ An error occurred when checking the balance", e);
});
} else if (action === "!drip") {
sendReaction(roomId, eventId, "👀");
if (!arg0) {
logger.warn("Address not provided, skipping");
sendReaction(roomId, eventId, "😕");
sendMessage(roomId, `${sender} Please provide an address to be funded.`);
return;
Comment on lines +124 to 128
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds like there's no need to send 👀 reaction, if we're sending a 😕 right after.
Why not move sendReaction(roomId, eventId, "👀"); after synchronous checks?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see what you mean, also thought about it.

In my pedantic mind it's more consistent to always have the same pattern:

  • seen 👀 -> 👍 OK
  • seen 👀 -> 😕 FAIL

Changing this would introduce a 😕 FAIL for a message that "has not been seen" in contrast with others.

Those were just my thoughts, don't know if it makes sense :D

}

Expand All @@ -127,6 +136,7 @@ bot.on(mSDK.RoomEvent.Timeline, (event: mSDK.MatrixEvent) => {
try {
AccountId().enc(address);
} catch (e) {
sendReaction(roomId, eventId, "😕");
sendMessage(roomId, `${sender} provided an incompatible address.`);
return;
}
Expand Down Expand Up @@ -163,9 +173,11 @@ bot.on(mSDK.RoomEvent.Timeline, (event: mSDK.MatrixEvent) => {

const message = formattedSuccessfulDripResponse(dripAmount, sender, res.hash);

sendReaction(roomId, eventId, "👍");
sendMessage(roomId, message.plain, message.formatted);
})
.catch((e) => {
sendReaction(roomId, eventId, "😕");
sendMessage(roomId, "An unexpected error occurred, please check the server logs");
logger.error("⭕ An error occurred when dripping", e);
});
Expand Down
Loading