Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
client.on("room.message", async (roomId, event) => {
if (roomId !== config.managementRoom) return;
if (!event['content']) return;
const content = event['content'];
if (content['msgtype'] === "m.text" && content['body']) {
const prefixes = [COMMAND_PREFIX, this.localpart + ":", this.displayName + ":", await client.getUserId() + ":"];
const prefixUsed = prefixes.find(p => content['body'].startsWith(p));
if (!prefixUsed) return;
// rewrite the event body to make the prefix uniform (in case the bot has spaces in its display name)
event['content']['body'] = COMMAND_PREFIX + content['body'].substring(prefixUsed.length);
LogService.info("Mjolnir", `Command being run by ${event['sender']}: ${event['content']['body']}`);
await client.sendReadReceipt(roomId, event['event_id']);
return handleCommand(roomId, event, this);
}
});
for (const topic of Object.keys(this.listeners)) await this.doBind(topic);
LogService.info("mq", `Asserting dead letter queue ${VoyagerConfig.rabbitmq.deadLetterQueue} exists...`);
await this.channel.assertQueue(VoyagerConfig.rabbitmq.deadLetterQueue, {
durable: true,
messageTtl: 2 * 60 * 1000, // 2 minutes
deadLetterExchange: VoyagerConfig.rabbitmq.exchange,
});
LogService.info("mq", "Binding dead letter exchange to dead letter queue...");
await this.channel.bindQueue(VoyagerConfig.rabbitmq.deadLetterQueue, VoyagerConfig.rabbitmq.deadLetterExchange, "*");
LogService.info("mq", "Listening for events...");
await this.channel.consume(this.queueName, this.onMessage.bind(this));
LogService.info("mq", "RabbitMQ ready");
}
LogService.info("mq", `Asserting queue ${this.queueName} exists...`);
await this.channel.assertQueue(this.queueName, {
exclusive: true,
autoDelete: true,
deadLetterExchange: VoyagerConfig.rabbitmq.deadLetterExchange,
});
for (const topic of Object.keys(this.listeners)) await this.doBind(topic);
LogService.info("mq", `Asserting dead letter queue ${VoyagerConfig.rabbitmq.deadLetterQueue} exists...`);
await this.channel.assertQueue(VoyagerConfig.rabbitmq.deadLetterQueue, {
durable: true,
messageTtl: 2 * 60 * 1000, // 2 minutes
deadLetterExchange: VoyagerConfig.rabbitmq.exchange,
});
LogService.info("mq", "Binding dead letter exchange to dead letter queue...");
await this.channel.bindQueue(VoyagerConfig.rabbitmq.deadLetterQueue, VoyagerConfig.rabbitmq.deadLetterExchange, "*");
LogService.info("mq", "Listening for events...");
await this.channel.consume(this.queueName, this.onMessage.bind(this));
LogService.info("mq", "RabbitMQ ready");
}
autoDelete: true,
deadLetterExchange: VoyagerConfig.rabbitmq.deadLetterExchange,
});
for (const topic of Object.keys(this.listeners)) await this.doBind(topic);
LogService.info("mq", `Asserting dead letter queue ${VoyagerConfig.rabbitmq.deadLetterQueue} exists...`);
await this.channel.assertQueue(VoyagerConfig.rabbitmq.deadLetterQueue, {
durable: true,
messageTtl: 2 * 60 * 1000, // 2 minutes
deadLetterExchange: VoyagerConfig.rabbitmq.exchange,
});
LogService.info("mq", "Binding dead letter exchange to dead letter queue...");
await this.channel.bindQueue(VoyagerConfig.rabbitmq.deadLetterQueue, VoyagerConfig.rabbitmq.deadLetterExchange, "*");
LogService.info("mq", "Listening for events...");
await this.channel.consume(this.queueName, this.onMessage.bind(this));
LogService.info("mq", "RabbitMQ ready");
}
private async doBind(topic: string) {
if (this.boundTopics.indexOf(topic) !== -1) return;
LogService.info("mq", `Binding topic ${topic} on ${VoyagerConfig.rabbitmq.exchange} to queue ${this.queueName}...`);
await this.channel.bindQueue(this.queueName, VoyagerConfig.rabbitmq.exchange, topic);
this.boundTopics.push(topic);
}
public async query(statement: string, args?: any[]): Promise {
LogService.info("PostgresDatabase", `Running query: ${statement}`);
const pgClient = await this.getClient();
try {
return (await pgClient.client.query(statement, args)).rows;
} finally {
if (pgClient.shouldRelease) pgClient.client.release();
}
}
public async sendPayload(topic: string, type: string, payload: any): Promise {
LogService.info("mq", `Sending payload to topic ${topic} of type ${type}`);
const buf = Buffer.from(JSON.stringify(payload));
return this.channel.publish(VoyagerConfig.rabbitmq.exchange, topic, buf, {
persistent: true,
contentType: "application/json",
contentEncoding: "utf8",
type: type,
});
}
function backfill(from: string) {
const qs = {
filter: JSON.stringify(filter),
from: from,
dir: "b",
};
LogService.info("utils", "Backfilling with token: " + token);
return client.doRequest("GET", `/_matrix/client/r0/rooms/${encodeURIComponent(roomId)}/messages`, qs);
}