Skip to content

Commit

Permalink
Merge pull request #188 from zhiyan114/master
Browse files Browse the repository at this point in the history
Expanding Span tracing slowly..
  • Loading branch information
zhiyan114 authored Oct 2, 2024
2 parents 5542f10 + 65d13aa commit 553d625
Show file tree
Hide file tree
Showing 6 changed files with 248 additions and 179 deletions.
6 changes: 5 additions & 1 deletion src/events/helper/DiscordCommandHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,11 +106,15 @@ export class DiscordCommandHandler {
name: `Discord Command: ${command.metadata.name}`,
op: `discord.cmd.${command.metadata.name}`,
parentSpan: null,
}, async () => {
}, async (span) => {
try {
await command.execute(interaction);
}
catch(ex) {
span.setStatus({
code: 2,
message: "Command Execution Error"
});
const id = captureException(ex, {tags: {handled: "no"}});
await this.client.redis.set(`userSentryErrorID:${interaction.user.id}`, id, "EX", 1800);

Expand Down
42 changes: 27 additions & 15 deletions src/events/helper/DiscordConfirmBtn.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,40 @@
import { ButtonInteraction, GuildMember } from "discord.js";
import { DiscordClient } from "../../core/DiscordClient";
import { DiscordUser } from "../../utils/DiscordUser";
import { startSpan } from "@sentry/node";

export async function VertificationHandler(client: DiscordClient, interaction: ButtonInteraction) {
if(interaction.user.bot)
return interaction.reply({ content: "PRIVILEGE INSUFFICIENT!", ephemeral: true });

const user = new DiscordUser(client, interaction.user);
const member = interaction.member as GuildMember | null;
if(!member) return;
await startSpan({
name: "Discord Verification Handler",
op: "discord.verify",
parentSpan: null
}, async () => {
const user = new DiscordUser(client, interaction.user);
const member = interaction.member as GuildMember | null;
if(!member) return;

// Check is already verified
const newUserRole = client.config.newUserRoleID;
if(member.roles.cache.has(newUserRole) && await user.isVerified())
return interaction.reply({ content: "You are already verified!", ephemeral: true });
// Check is already verified
const newUserRole = client.config.newUserRoleID;
if(member.roles.cache.has(newUserRole) && await user.isVerified())
return await interaction.reply({ content: "You are already verified!", ephemeral: true });

// Update the user
await member.roles.add(newUserRole, "Confirmation Role");
await user.updateUserData({ rulesconfirmedon: new Date() });
await startSpan({
name: "Update Verification Status",
op: "event.helper.VertificationHandler",
}, async()=>{
// Update the user
await member.roles.add(newUserRole, "Confirmation Role");
await user.updateUserData({ rulesconfirmedon: new Date() });

// Send the message
await interaction.reply({ content: "Thank you for confirming the rules.", ephemeral: true });
await client.logger.sendLog({
type: "Interaction",
message: `**${interaction.user.tag}** has confirmed the rules.`
// Send the message
await interaction.reply({ content: "Thank you for confirming the rules.", ephemeral: true });
await client.logger.sendLog({
type: "Interaction",
message: `**${interaction.user.tag}** has confirmed the rules.`
});
});
});
}
6 changes: 5 additions & 1 deletion src/events/helper/TwitchCommandHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export async function processCommand(eventData: eventType): Promise<boolean | un
name: `Twitch Command: ${command.name}`,
op: `twitch.cmd.${command.name}`,
parentSpan: null,
}, async () => {
}, async (span) => {
try {
await command.execute({
channel: eventData.channel,
Expand All @@ -51,6 +51,10 @@ export async function processCommand(eventData: eventType): Promise<boolean | un
args
});
} catch(ex) {
span.setStatus({
code: 2,
message: "Command Execution Error"
});
// Feedback events are based on discord ID so there's that...
const eventID = captureException(ex, {tags: {handled: "no"}});
const dClient = eventData.client.discord;
Expand Down
54 changes: 30 additions & 24 deletions src/utils/DiscordInvite.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { createHash } from "crypto";
import { Channel, ChannelType, Guild, GuildInvitableChannelResolvable, InviteCreateOptions, NewsChannel, TextChannel, VoiceChannel } from "discord.js";
import { DiscordClient } from "../core/DiscordClient";
import { startSpan } from "@sentry/node";

interface tempInviteOption extends InviteCreateOptions {
channel?: GuildInvitableChannelResolvable;
Expand Down Expand Up @@ -76,39 +77,44 @@ export class DiscordInvite {
* @returns The invite link or the code
*/
public async getTempInvite(inviteOpt?: tempInviteOption) {
if(!inviteOpt) inviteOpt = {};

// Use the vanity code if possible
if(this.guild.vanityURLCode) return inviteOpt.rawCode ? this.guild.vanityURLCode : this.baseUrl + this.guild.vanityURLCode;

// Use the cached invite key if it exists
if(!inviteOpt.nocache) {
const cache = await this.client.redis.get(this.redisKey);
if(cache) return inviteOpt.rawCode ? cache : this.baseUrl + cache;
}
return await startSpan({
name: "Discord Invite Request",
op: "DiscordInvite.getTempInvite",
onlyIfParent: true,
}, async() =>{
if(!inviteOpt) inviteOpt = {};

// Use the vanity code if possible
if(this.guild.vanityURLCode) return inviteOpt.rawCode ? this.guild.vanityURLCode : this.baseUrl + this.guild.vanityURLCode;

// Use the cached invite key if it exists
if(!inviteOpt.nocache) {
const cache = await this.client.redis.get(this.redisKey);
if(cache) return inviteOpt.rawCode ? cache : this.baseUrl + cache;
}

// Default value for the invite
inviteOpt.maxAge = inviteOpt.maxAge ?? 86400;
inviteOpt.reason = inviteOpt.reason ?? "Temporary Invite";
// Default value for the invite
inviteOpt.maxAge = inviteOpt.maxAge ?? 86400;
inviteOpt.reason = inviteOpt.reason ?? "Temporary Invite";

// Find a valid guild channel to create invite in
inviteOpt.channel = inviteOpt.channel ??
// Find a valid guild channel to create invite in
inviteOpt.channel = inviteOpt.channel ??
this.guild.rulesChannel ??
this.guild.publicUpdatesChannel ??
this.guild.channels.cache.find(ch=>this.isInviteChannel(ch)) as TextChannel | VoiceChannel | NewsChannel | undefined ??
(await this.guild.channels.fetch()).find(ch=>this.isInviteChannel(ch)) as TextChannel | VoiceChannel | NewsChannel | undefined;
if(!inviteOpt.channel) throw new DiscordInviteError("No channel is associated with this server");
if(!inviteOpt.channel) throw new DiscordInviteError("No channel is associated with this server");

// Create invite
const inviteLink = await this.guild.invites.create(inviteOpt.channel, inviteOpt);
if(!inviteOpt.nocache)
return inviteOpt.rawCode ? inviteLink.code : inviteLink.url;
// Create invite
const inviteLink = await this.guild.invites.create(inviteOpt.channel, inviteOpt);
if(!inviteOpt.nocache)
return inviteOpt.rawCode ? inviteLink.code : inviteLink.url;

// Save to cache if allowed
await this.client.redis.set(this.redisKey, inviteLink.code, "EX", inviteOpt.maxAge);
return inviteOpt.rawCode ? inviteLink.code : inviteLink.url;
// Save to cache if allowed
await this.client.redis.set(this.redisKey, inviteLink.code, "EX", inviteOpt.maxAge);
return inviteOpt.rawCode ? inviteLink.code : inviteLink.url;
});
}

}


Expand Down
Loading

0 comments on commit 553d625

Please sign in to comment.