-
Notifications
You must be signed in to change notification settings - Fork 152
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
Metrics #480
Metrics #480
Changes from 8 commits
639623e
c4ae6be
3d9a677
4397227
663eebc
d617346
11a548f
2370b4d
95cb6af
3a90721
04f3007
b0810a8
a78de0b
e57769e
c6e41d9
0267969
8c09564
45d2bf8
9827b98
28ecd27
d02603c
831f922
90ea8c8
de2e92d
dab3561
6ed4eb1
fda7d34
d2642ef
a6cbac2
eb89fc1
37d76af
9a47c09
ef1872d
c3a142a
1b9d062
1391753
0fdb9dc
980a1a2
b336eda
bdc84f7
c9cad35
641edc7
5f245e1
062d5b1
2243030
cff2bc1
55a1248
f64340b
8405046
c3eaaa8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -37,6 +37,7 @@ import * as Discord from "discord.js"; | |
import * as mime from "mime"; | ||
import { IMatrixEvent, IMatrixMediaInfo } from "./matrixtypes"; | ||
import { DiscordCommandHandler } from "./discordcommandhandler"; | ||
import { MetricPeg } from "./metrics"; | ||
|
||
const log = new Log("DiscordBot"); | ||
|
||
|
@@ -288,12 +289,14 @@ export class DiscordBot { | |
}); | ||
client.on("message", async (msg: Discord.Message) => { | ||
try { | ||
MetricPeg.get.registerRequest(msg.id); | ||
await this.waitUnlock(msg.channel); | ||
this.discordMessageQueue[msg.channel.id] = (async () => { | ||
await (this.discordMessageQueue[msg.channel.id] || Promise.resolve()); | ||
try { | ||
await this.OnMessage(msg); | ||
} catch (err) { | ||
MetricPeg.get.requestOutcome(msg.id, true, "fail"); | ||
log.error("Caught while handing 'message'", err); | ||
} | ||
})(); | ||
|
@@ -389,6 +392,7 @@ export class DiscordBot { | |
} | ||
const channel = guild.channels.get(room); | ||
if (channel && channel.type === "text") { | ||
this.ClientFactory.bindMetricsToChannel(channel as Discord.TextChannel); | ||
const lookupResult = new ChannelLookupResult(); | ||
lookupResult.channel = channel as Discord.TextChannel; | ||
lookupResult.botUser = this.bot.user.id === client.user.id; | ||
|
@@ -448,6 +452,7 @@ export class DiscordBot { | |
// NOTE: Don't send replies to discord if we are a puppet. | ||
msg = await chan.send(embed.description, opts); | ||
} else if (hook) { | ||
MetricPeg.get.remoteCall("hook.send"); | ||
msg = await hook.send(embed.description, { | ||
avatarURL: embed!.author!.icon_url, | ||
embeds: embedSet.replyEmbed ? [embedSet.replyEmbed] : undefined, | ||
|
@@ -539,6 +544,7 @@ export class DiscordBot { | |
if (guild) { | ||
const channel = client.channels.get(entry.remote!.get("discord_channel") as string); | ||
if (channel) { | ||
this.ClientFactory.bindMetricsToChannel(channel as Discord.TextChannel); | ||
return channel; | ||
} | ||
throw Error("Channel given in room entry not found"); | ||
|
@@ -738,11 +744,13 @@ export class DiscordBot { | |
if (indexOfMsg !== -1) { | ||
log.verbose("Got repeated message, ignoring."); | ||
delete this.sentMessages[indexOfMsg]; | ||
MetricPeg.get.requestOutcome(msg.id, true, "dropped"); | ||
return; // Skip *our* messages | ||
} | ||
const chan = msg.channel as Discord.TextChannel; | ||
if (msg.author.id === this.bot.user.id) { | ||
// We don't support double bridging. | ||
MetricPeg.get.requestOutcome(msg.id, true, "dropped"); | ||
return; | ||
} | ||
// Test for webhooks | ||
|
@@ -751,13 +759,15 @@ export class DiscordBot { | |
.filterArray((h) => h.name === "_matrix").pop(); | ||
if (webhook && msg.webhookID === webhook.id) { | ||
// Filter out our own webhook messages. | ||
MetricPeg.get.requestOutcome(msg.id, true, "dropped"); | ||
return; | ||
} | ||
} | ||
|
||
// check if it is a command to process by the bot itself | ||
if (msg.content.startsWith("!matrix")) { | ||
await this.discordCommandHandler.Process(msg); | ||
MetricPeg.get.requestOutcome(msg.id, true, "success"); | ||
return; | ||
} | ||
|
||
|
@@ -766,14 +776,13 @@ export class DiscordBot { | |
let rooms; | ||
try { | ||
rooms = await this.channelSync.GetRoomIdsFromChannel(msg.channel); | ||
if (rooms === null) { throw Error(); } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. new lines for if-condition due to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think I was being lazy, sorry. |
||
} catch (err) { | ||
log.verbose("No bridged rooms to send message to. Oh well."); | ||
MetricPeg.get.requestOutcome(msg.id, true, "dropped"); | ||
return null; | ||
} | ||
try { | ||
if (rooms === null) { | ||
return null; | ||
} | ||
const intent = this.GetIntentFromDiscordMember(msg.author, msg.webhookID); | ||
// Check Attachements | ||
await Util.AsyncForEach(msg.attachments.array(), async (attachment) => { | ||
|
@@ -854,7 +863,9 @@ export class DiscordBot { | |
await afterSend(res); | ||
} | ||
}); | ||
MetricPeg.get.requestOutcome(msg.id, true, "success"); | ||
} catch (err) { | ||
MetricPeg.get.requestOutcome(msg.id, true, "fail"); | ||
log.verbose("Failed to send message into room.", err); | ||
} | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,14 +16,12 @@ limitations under the License. | |
|
||
import { DiscordBridgeConfigAuth } from "./config"; | ||
import { DiscordStore } from "./store"; | ||
import { Client as DiscordClient } from "discord.js"; | ||
import { Client as DiscordClient, TextChannel } from "discord.js"; | ||
import { Log } from "./log"; | ||
import { Util } from "./util"; | ||
import { MetricPeg } from "./metrics"; | ||
|
||
const log = new Log("ClientFactory"); | ||
|
||
const READY_TIMEOUT = 30000; | ||
|
||
export class DiscordClientFactory { | ||
private config: DiscordBridgeConfigAuth; | ||
private store: DiscordStore; | ||
|
@@ -110,4 +108,12 @@ export class DiscordClientFactory { | |
return this.botClient; | ||
} | ||
} | ||
|
||
public bindMetricsToChannel(channel: TextChannel) { | ||
// tslint:disable-next-line:only-arrow-functions | ||
channel.send = function() { | ||
MetricPeg.get.remoteCall("channel.send"); | ||
return channel.send.apply(channel, arguments); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. doesn't this recurse? What if we call There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes hello I'm dumb, I wonder how we do this sanely. |
||
}; | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
shouldn't we always bind the channel on message processing? Or even better, right when the message enters via the event listeners?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nope, because we only call the send function when sending stuff that came via matrix. Though I guess we should also bind in the event listeners too.