Skip to content

Commit

Permalink
refactor: separate duplication check and original message check
Browse files Browse the repository at this point in the history
  • Loading branch information
austinhuang0131 committed Feb 4, 2024
1 parent 8d6dc72 commit acda0cd
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 9 deletions.
3 changes: 2 additions & 1 deletion packages/bolt-revolt/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,8 @@ export default class RevoltPlugin extends BoltPlugin {
}
case 'delete': {
const channel = await this.bot.channels.fetch(data.data.channel);
await channel.deleteMessages([data.data.id]);
const msg = await channel.fetchMessage(data.data.id);
await msg.delete();
return { ...data.data.bridgePlatform, id: data.data.id };
}
}
Expand Down
4 changes: 2 additions & 2 deletions packages/bolt/lib/bolt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,11 +93,11 @@ export class Bolt extends EventEmitter<BoltPluginEvents> {
private registerPluginEvents() {
// TODO: move all code below to bridge folder
this.on('messageCreate', async msg => {
if (await getBoltBridgedMessage(this, msg.id)) return;
if (await getBoltBridgedMessage(this, true, msg.id)) return;
bridgeBoltMessage(this, 'create', msg);
});
this.on('messageUpdate', async msg => {
if (await getBoltBridgedMessage(this, msg.id)) return;
if (await getBoltBridgedMessage(this, true, msg.id)) return;
bridgeBoltMessage(this, 'update', msg);
});
this.on('messageDelete', async msg => {
Expand Down
6 changes: 3 additions & 3 deletions packages/bolt/lib/bridge/bridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export async function bridgeBoltMessage(
const platforms: (BoltBridgePlatform | BoltBridgeSentPlatform)[] | false =
type === 'create'
? bridge.platforms.filter(i => i.channel !== message.channel)
: await getBoltBridgedMessage(bolt, message.id);
: await getBoltBridgedMessage(bolt, false, message.id);

if (!platforms || platforms.length < 1) return;

Expand All @@ -46,7 +46,7 @@ export async function bridgeBoltMessage(
: (platform as BoltBridgeSentPlatform).thread?.id
: undefined;

const replyto = await getBoltBridgedMessage(bolt, message.replyto?.id);
const replyto = await getBoltBridgedMessage(bolt, false, message.replyto?.id);

if (bridge.settings?.realnames && 'author' in message) {
message.author.username = message.author.rawname;
Expand Down Expand Up @@ -110,7 +110,7 @@ export async function bridgeBoltMessage(
if (type !== 'delete') {
for (const i of data) {
// since this key is used to prevent echo, 15 sec expiry should be enough
await bolt.redis?.set(`message-${i.id}`, JSON.stringify(data), {
await bolt.redis?.set(`message-temp-${i.id}`, JSON.stringify(data), {
ex: 15
});
}
Expand Down
4 changes: 2 additions & 2 deletions packages/bolt/lib/bridge/utils.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { Bolt } from '../bolt.ts';
import { BoltBridgeDocument, BoltBridgeSentPlatform } from './types.ts';

export async function getBoltBridgedMessage(bolt: Bolt, id?: string) {
return JSON.parse((await bolt.redis?.get(`message-${id}`)) || 'false') as
export async function getBoltBridgedMessage(bolt: Bolt, bCheck: Boolean, id?: string) {
return JSON.parse((await bolt.redis?.get(`message${bCheck ? "-temp" : ""}-${id}`)) || 'false') as
| BoltBridgeSentPlatform[]
| false;
}
Expand Down
2 changes: 1 addition & 1 deletion packages/bolt/lib/commands/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export class BoltCommands {
this.bolt = bolt;
this.registerCommands(...defaultcommands);
bolt.on('messageCreate', async msg => {
if (await getBoltBridgedMessage(bolt, msg.id)) return;
if (await getBoltBridgedMessage(bolt, true, msg.id)) return;
if (msg.content?.startsWith('!bolt')) {
let [_, name, ...arg] = msg.content.split(' ');
this.runCommand({
Expand Down

0 comments on commit acda0cd

Please sign in to comment.