-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
149 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,3 +11,5 @@ server_only 'yes' | |
server_scripts { | ||
'dist/server.js', | ||
} | ||
|
||
provide 'zdiscord' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { GatewayDispatchEvents } from '@discordjs/core'; | ||
import { guildId } from '../../../utils/env.js'; | ||
import { GatewayEvent } from '../../types.js'; | ||
import { members } from '../exports.js'; | ||
|
||
export const guildMemberAdd = { | ||
name: GatewayDispatchEvents.GuildMemberAdd, | ||
type: 'on', | ||
async execute({ data: member }) { | ||
if (!guildId || member.guild_id !== guildId) return; | ||
|
||
members.set(member.user.id, { | ||
nick: member.nick, | ||
globalName: member.user.global_name, | ||
username: member.user.username, | ||
roles: member.roles, | ||
}); | ||
}, | ||
} satisfies GatewayEvent<GatewayDispatchEvents.GuildMemberAdd>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { GatewayDispatchEvents } from '@discordjs/core'; | ||
import { guildId } from '../../../utils/env.js'; | ||
import { GatewayEvent } from '../../types.js'; | ||
import { members } from '../exports.js'; | ||
|
||
export const guildMemberRemove = { | ||
name: GatewayDispatchEvents.GuildMemberRemove, | ||
type: 'on', | ||
async execute({ data: member }) { | ||
if (!guildId || member.guild_id !== guildId) return; | ||
|
||
members.delete(member.user.id); | ||
}, | ||
} satisfies GatewayEvent<GatewayDispatchEvents.GuildMemberRemove>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { GatewayDispatchEvents } from '@discordjs/core'; | ||
import { guildId } from '../../../utils/env.js'; | ||
import { GatewayEvent } from '../../types.js'; | ||
import { members } from '../exports.js'; | ||
|
||
export const guildMemberUpdate = { | ||
name: GatewayDispatchEvents.GuildMemberUpdate, | ||
type: 'on', | ||
async execute({ data }) { | ||
if (!guildId || data.guild_id !== guildId) return; | ||
|
||
const member = members.get(data.user.id)!; | ||
member.nick = data.nick; | ||
member.globalName = data.user.global_name; | ||
member.username = data.user.username; | ||
member.roles = data.roles; | ||
|
||
console.log(data.nick); | ||
}, | ||
} satisfies GatewayEvent<GatewayDispatchEvents.GuildMemberUpdate>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import { Collection } from '@discordjs/collection'; | ||
import { Snowflake } from '@discordjs/core'; | ||
import { zdiscordBridge } from '../../utils/env.js'; | ||
|
||
function zdiscordExports(name: string, cb: Function) { | ||
if (!zdiscordBridge) return; | ||
|
||
on(`__cfx_export_zdiscord_${name}`, (setCB: (cb: Function) => void) => | ||
setCB(cb), | ||
); | ||
} | ||
|
||
export interface ZDiscordMemberData { | ||
nick: string | null | undefined; | ||
globalName: string | null; | ||
username: string; | ||
roles: string[]; | ||
} | ||
|
||
export const members = new Collection<Snowflake, ZDiscordMemberData>(); | ||
|
||
function getMember(userId: string): ZDiscordMemberData | false { | ||
return members.get(userId) ?? false; | ||
} | ||
|
||
function getMemberFromSource(playerId: number): ZDiscordMemberData | false { | ||
const userId = GetPlayerIdentifierByType(`${playerId}`, 'discord'); | ||
if (!userId) return false; | ||
|
||
return getMember(userId); | ||
} | ||
|
||
function parseMember(memberId: string | number): ZDiscordMemberData | false { | ||
if (!memberId) return false; | ||
|
||
return typeof memberId === 'number' | ||
? getMemberFromSource(memberId) | ||
: getMember(memberId); | ||
} | ||
|
||
zdiscordExports( | ||
'isRolePresent', | ||
(memberId: string | number, role: string | string[]): boolean => { | ||
if (!memberId || !role) return false; | ||
|
||
const member = parseMember(memberId); | ||
if (!member) return false; | ||
|
||
return typeof role === 'object' | ||
? member.roles.filter((r) => role.includes(r)).length > 0 | ||
: member.roles.includes(role); | ||
}, | ||
); | ||
|
||
zdiscordExports('getMemberRoles', (memberId: string | number): string[] => { | ||
if (!memberId) return []; | ||
|
||
const member = parseMember(memberId); | ||
if (!member) return []; | ||
|
||
return member.roles; | ||
}); | ||
|
||
zdiscordExports('getName', (memberId: string | number): string | false => { | ||
const member = parseMember(memberId); | ||
if (!member) return false; | ||
|
||
return member.nick ?? member.globalName ?? member.username; | ||
}); | ||
|
||
zdiscordExports('getDiscordId', (playerId: number): string | false => { | ||
return GetPlayerIdentifierByType(`${playerId}`, 'discord') || false; | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { Component } from '../types.js'; | ||
import { guildMemberAdd } from './events/guild-member-add.js'; | ||
import { guildMemberRemove } from './events/guild-member-remove.js'; | ||
import { guildMemberUpdate } from './events/guild-member-update.js'; | ||
|
||
export default { | ||
gatewayEvents: [guildMemberAdd, guildMemberRemove, guildMemberUpdate], | ||
} satisfies Component; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters