This repository has been archived by the owner on Jun 12, 2022. It is now read-only.
/
Context.ts
59 lines (49 loc) · 2.02 KB
/
Context.ts
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
import {Constants, Member, Message, MessageContent, MessageFile} from 'eris';
import parseArgs from './parseArgs';
export default class Context extends Message {
args: string[];
cmd: string;
suffix: string;
me?: Member;
constructor(data, client) {
super(data, client);
const {args, cmd, suffix} = parseArgs(this.content);
this.args = args;
this.cmd = cmd;
this.suffix = suffix;
this.me = this.guild ? this.guild.members.get(client.user.id) : null;
}
get guild() {
return this.channel.guild;
}
async send(content: MessageContent, file?: MessageFile, destination: ContextDestinations = ContextDestinations.Channel): Promise<Message> {
if (destination === 'channel') return this.channel.createMessage(content, file);
else if (destination === 'author') return this.author.getDMChannel().then(dm => dm.createMessage(content, file));
else throw new Error(`Unknown destination: ${where}`);
}
hasPermission(permission: string, target: PermissionTargets = PermissionTargets.Self): boolean {
if (!Object.keys(Constants.Permissions).includes(permission)) {
if (this.client.extensions.logger) this.client.extensions.logger.dispatch('warn', `Unknown permission "${permission}"`);
return true;
}
switch (target) {
case PermissionTargets.Self:
return this.channel.permissionsOf(this._client.user.id).has(permission);
case PermissionTargets.Author:
return this.channel.permissionsOf(this.author.id).has(permission);
case PermissionTargets.Both:
return this.hasPermission(permission) && this.hasPermission(permission, PermissionTargets.Author);
default:
throw new Error(`Unknown target: ${target}`);
}
}
}
export enum ContextDestinations {
Channel = 'channel',
Author = 'author'
}
export enum PermissionTargets {
Self = 'self',
Author = 'author',
Both = 'both'
}