forked from Dragory/Knub
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpluginUtils.ts
More file actions
79 lines (68 loc) · 2.18 KB
/
pluginUtils.ts
File metadata and controls
79 lines (68 loc) · 2.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import type { APIInteractionGuildMember, Guild, GuildMember, PartialGuildMember } from "discord.js";
import type { PermissionLevels } from "../config/configTypes.ts";
import type { AnyContext, GlobalContext, GuildContext, GuildPluginMap } from "../types.ts";
import type { KeyOfMap } from "../utils.ts";
import type {
AnyGlobalPluginBlueprint,
AnyGuildPluginBlueprint,
AnyPluginBlueprint,
BasePluginBlueprint,
} from "./PluginBlueprint.ts";
export function getMemberRoles(member: GuildMember | PartialGuildMember | APIInteractionGuildMember): string[] {
return Array.isArray(member.roles) ? member.roles : Array.from(member.roles.cache.values()).map((r) => r.id);
}
export function getMemberLevel(
levels: PermissionLevels,
member: GuildMember | PartialGuildMember | APIInteractionGuildMember,
guild: Guild,
): number {
const memberId = "id" in member ? member.id : member.user.id;
if (guild.ownerId === memberId) {
return 99999;
}
const roles = getMemberRoles(member);
for (const [id, level] of Object.entries<number>(levels)) {
if (memberId === id || roles.includes(id)) {
return level;
}
}
return 0;
}
export function isGuildContext(ctx: AnyContext): ctx is GuildContext {
return (ctx as any).guildId != null;
}
export function isGlobalContext(ctx: AnyContext): ctx is GuildContext {
return !isGuildContext(ctx);
}
export function isGuildBlueprintByContext(
_ctx: GuildContext,
_blueprint: AnyPluginBlueprint,
): _blueprint is AnyGuildPluginBlueprint {
return true;
}
export function isGlobalBlueprintByContext(
_ctx: GlobalContext,
_blueprint: AnyPluginBlueprint,
): _blueprint is AnyGlobalPluginBlueprint {
return true;
}
export type PluginPublicInterface<T extends BasePluginBlueprint<any, any>> = NonNullable<T["public"]> extends (
...args: any[]
) => infer R
? R
: null;
/**
* By default, return an empty config for all guilds and the global config
*/
export function defaultGetConfig() {
return {};
}
/**
* By default, load all available guild plugins
*/
export function defaultGetEnabledGuildPlugins(
_ctx: AnyContext,
guildPlugins: GuildPluginMap,
): Array<KeyOfMap<GuildPluginMap>> {
return Array.from(guildPlugins.keys());
}