Skip to content

Commit 78afb27

Browse files
committed
2 parents 7b30fc4 + 3d793d0 commit 78afb27

File tree

3 files changed

+165
-148
lines changed

3 files changed

+165
-148
lines changed

plugins/discord.js

+144-135
Original file line numberDiff line numberDiff line change
@@ -5,150 +5,159 @@ import { Client, channelMention, ChannelType } from "discord.js";
55
* @type {Object<string, import("../src/ArgumentParser").UsageParser>}
66
*/
77
const DiscordUsages = {
8-
_mentionable: {
9-
type: "text",
10-
async parse(ctx) {
11-
return ctx.arg.replace(/[<@#!&>]/g, "");
12-
},
13-
},
14-
15-
user: {
16-
type: "_mentionable",
17-
async parse(ctx) {
18-
let user = ctx.context.client.users
19-
.fetch(ctx.arg)
20-
.catch(() => null);
21-
22-
if (!user) {
23-
return ctx.fail(`${ctx.opts.bot ? 'Bot' : 'User'} not found!`);
24-
};
25-
26-
if (ctx.opts.bot && !user.bot)
27-
return ctx.fail(`${ctx.style.bold(
28-
user.username
29-
)} isn't a bot!`);
30-
else if (!opts.bot && user.bot)
31-
return ctx.fail(`${ctx.style.bold(user.username)} is a bot!`);
32-
33-
return { parsed: user };
34-
},
35-
},
36-
37-
member: {
38-
type: "user",
39-
async parse(ctx) {
40-
const { guild } = ctx.context.message;
41-
const member = await guild.members
42-
.fetch(ctx.arg.id)
43-
.catch(() => null);
44-
45-
if (!member)
46-
return ctx.fail(`${ctx.style.bold(
47-
ctx.arg.username
48-
)} isn't on ${ctx.style.bold(guild.name)}!`);
49-
50-
return { parsed: member };
51-
}
52-
},
53-
54-
role: {
55-
type: "_mentionable",
56-
async parse(ctx) {
57-
const { guild } = ctx.context.message;
58-
const role = await guild.roles.fetch(ctx.arg).catch(() => null);
59-
60-
if (!role)
61-
return ctx.fail(`Role not found!`);
62-
63-
return { parsed: role };
64-
},
65-
},
66-
67-
channel: {
68-
type: "_mentionable",
69-
async parse(ctx) {
70-
const { guild } = ctx.context.message;
71-
const channel = await guild.channels.fetch(ctx.arg).catch(() => null);
72-
73-
if (!channel)
74-
return ctx.fail(`Channel not found!`);
75-
76-
if(ctx.opts.channelType !== undefined
77-
&& ctx.opts.channelType != channel.type) {
78-
return ctx.fail(`${channelMention(channel.id)} isn't ${ctx.style.bold(ChannelType[ctx.opts.channelType])}`);
79-
}
80-
81-
return { parsed: channel };
82-
},
83-
},
84-
85-
textChannel: {
86-
type: "channel",
87-
channelType: ChannelType.GuildText,
88-
},
89-
90-
voiceChannel: {
91-
type: "channel",
92-
channelType: ChannelType.GuildVoice,
93-
},
8+
_mentionable: {
9+
type: "text",
10+
async parse(ctx) {
11+
return ctx.arg.replace(/[<@#!&>]/g, "");
12+
},
13+
},
14+
15+
user: {
16+
type: "_mentionable",
17+
async parse(ctx) {
18+
let user = ctx.context.client.users
19+
.fetch(ctx.arg)
20+
.catch(() => null);
21+
22+
if (!user) {
23+
return ctx.fail(`${ctx.opts.bot ? "Bot" : "User"} not found!`);
24+
}
25+
26+
if (ctx.opts.bot && !user.bot)
27+
return ctx.fail(
28+
`${ctx.style.bold(user.username)} isn't a bot!`,
29+
);
30+
else if (!opts.bot && user.bot)
31+
return ctx.fail(`${ctx.style.bold(user.username)} is a bot!`);
32+
33+
return { parsed: user };
34+
},
35+
},
36+
37+
member: {
38+
type: "user",
39+
async parse(ctx) {
40+
const { guild } = ctx.context.message;
41+
const member = await guild.members
42+
.fetch(ctx.arg.id)
43+
.catch(() => null);
44+
45+
if (!member)
46+
return ctx.fail(
47+
`${ctx.style.bold(
48+
ctx.arg.username,
49+
)} isn't on ${ctx.style.bold(guild.name)}!`,
50+
);
51+
52+
return { parsed: member };
53+
},
54+
},
55+
56+
role: {
57+
type: "_mentionable",
58+
async parse(ctx) {
59+
const { guild } = ctx.context.message;
60+
const role = await guild.roles.fetch(ctx.arg).catch(() => null);
61+
62+
if (!role) return ctx.fail(`Role not found!`);
63+
64+
return { parsed: role };
65+
},
66+
},
67+
68+
channel: {
69+
type: "_mentionable",
70+
async parse(ctx) {
71+
const { guild } = ctx.context.message;
72+
const channel = await guild.channels
73+
.fetch(ctx.arg)
74+
.catch(() => null);
75+
76+
if (!channel) return ctx.fail(`Channel not found!`);
77+
78+
if (
79+
ctx.opts.channelType !== undefined &&
80+
ctx.opts.channelType != channel.type
81+
) {
82+
return ctx.fail(
83+
`${channelMention(channel.id)} isn't ${ctx.style.bold(
84+
ChannelType[ctx.opts.channelType],
85+
)}`,
86+
);
87+
}
88+
89+
return { parsed: channel };
90+
},
91+
},
92+
93+
textChannel: {
94+
type: "channel",
95+
channelType: ChannelType.GuildText,
96+
},
97+
98+
voiceChannel: {
99+
type: "channel",
100+
channelType: ChannelType.GuildVoice,
101+
},
94102
};
95103

96-
97104
const DiscordChecks = {
98-
requirePermissions(permissions) {},
99-
100-
async requireGuildOwner(client, msg, args) {
101-
return msg.guild.ownerId == msg.author.id ?
102-
{
103-
pass: true,
104-
} :
105-
{
106-
pass: false,
107-
message: "You must be the owner of this guild",
108-
};
109-
},
105+
requirePermissions(permissions) {},
106+
107+
async requireGuildOwner(client, msg, args) {
108+
return msg.guild.ownerId == msg.author.id
109+
? {
110+
pass: true,
111+
}
112+
: {
113+
pass: false,
114+
message: "You must be the owner of this guild",
115+
};
116+
},
110117
};
111118

112119
/**
113120
* @typedef {import("../src/CommandHandler").CommandHandlerOptions} DiscordCommandHandlerOptions
114121
*/
115122

116123
class DiscordCommandHandler extends CommandHandler {
117-
/**
118-
*
119-
* @param {Client} client The discord client
120-
* @param {DiscordCommandHandlerOptions} opts - Options for the command handler
121-
*/
122-
constructor(client, opts = {}) {
123-
super(Object.assign({
124-
argumentParser: {
125-
styling: {
126-
arg: (x) => `\`${x}\``,
127-
bold: (x) => `**${x}**`,
128-
},
129-
},
130-
}, opts));
131-
this.client = client;
132-
this.client.handler = this;
133-
134-
for(let [id, usage] of Object.entries(DiscordUsages))
135-
this.registerUsage(id, usage);
136-
}
137-
138-
buildArguments({ args, ctx }) {
139-
return [ctx.client, ctx.message, args];
140-
}
141-
142-
// handles discord.js messages
143-
handleMessage(msg) {
144-
this.run(msg.content, {
145-
message: msg,
146-
client: this.client,
147-
});
148-
}
124+
/**
125+
*
126+
* @param {Client} client The discord client
127+
* @param {DiscordCommandHandlerOptions} opts - Options for the command handler
128+
*/
129+
constructor(client, opts = {}) {
130+
super(
131+
Object.assign(
132+
{
133+
argumentParser: {
134+
styling: {
135+
arg: (x) => `\`${x}\``,
136+
bold: (x) => `**${x}**`,
137+
},
138+
},
139+
},
140+
opts,
141+
),
142+
);
143+
this.client = client;
144+
this.client.handler = this;
145+
146+
for (let [id, usage] of Object.entries(DiscordUsages))
147+
this.registerUsage(id, usage);
148+
}
149+
150+
buildArguments({ args, ctx }) {
151+
return [ctx.client, ctx.message, args];
152+
}
153+
154+
// handles discord.js messages
155+
handleMessage(msg) {
156+
this.run(msg.content, {
157+
message: msg,
158+
client: this.client,
159+
});
160+
}
149161
}
150162

151-
export {
152-
DiscordCommandHandler,
153-
DiscordUsages,
154-
};
163+
export { DiscordCommandHandler, DiscordUsages };

src/ArgumentParser.js

+15-7
Original file line numberDiff line numberDiff line change
@@ -63,15 +63,19 @@ const NativeUsages = Object.entries({
6363
async parse(ctx) {
6464
if (ctx.opts.max !== undefined && ctx.arg.length > ctx.opts.max) {
6565
return fail(
66-
`${ctx.style.arg(ctx.name)} cannot be longer than ${ctx.style.arg(
66+
`${ctx.style.arg(
67+
ctx.name,
68+
)} cannot be longer than ${ctx.style.arg(
6769
opts.max,
6870
)} characters!`,
6971
);
7072
}
7173

7274
if (ctx.opts.min !== undefined && ctx.arg.length <= ctx.opts.min) {
7375
return fail(
74-
`${ctx.style.arg(ctx.name)} cannot be shorter than ${ctx.style.arg(
76+
`${ctx.style.arg(
77+
ctx.name,
78+
)} cannot be shorter than ${ctx.style.arg(
7579
ctx.opts.min,
7680
)} characters!`,
7781
);
@@ -91,20 +95,24 @@ const NativeUsages = Object.entries({
9195
}
9296

9397
if (ctx.opts.isInt && arg % 1 !== 0) {
94-
return fail(`${ctx.style.arg(ctx.name)} must be a whole number!`);
98+
return fail(
99+
`${ctx.style.arg(ctx.name)} must be a whole number!`,
100+
);
95101
}
96102

97103
if (ctx.opts.max !== undefined && arg > ctx.opts.max) {
98104
return fail(
99-
`${ctx.style.arg(ctx.name)} cannot be greater than ${ctx.style.arg(
100-
ctx.opts.max,
101-
)}!`,
105+
`${ctx.style.arg(
106+
ctx.name,
107+
)} cannot be greater than ${ctx.style.arg(ctx.opts.max)}!`,
102108
);
103109
}
104110

105111
if (ctx.opts.min !== undefined && arg > ctx.opts.min) {
106112
return fail(
107-
`${ctx.style.arg(ctx.name)} cannot be smaller than ${ctx.style.arg(
113+
`${ctx.style.arg(
114+
ctx.name,
115+
)} cannot be smaller than ${ctx.style.arg(
108116
ctx.opts.min,
109117
)} characters!`,
110118
);

src/CommandHandler.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,10 @@ import { ArgumentParser } from "./ArgumentParser.js";
3232
*/
3333

3434
const noLogger = {
35-
log: () => { },
36-
info: () => { },
37-
debug: () => { },
38-
error: () => { },
35+
log: () => {},
36+
info: () => {},
37+
debug: () => {},
38+
error: () => {},
3939
};
4040

4141
/**
@@ -158,9 +158,9 @@ class CommandHandler extends EventEmitter {
158158
* @param {Object} context
159159
* @param {function():string} context.getFullString - gives you a nicely rendered string
160160
*/
161-
invalidUsageMessage({ ctx, input, name, command, getFullString }) { }
161+
invalidUsageMessage({ ctx, input, name, command, getFullString }) {}
162162

163-
failedChecksMessage({ ctx, input, name, command, checks }) { }
163+
failedChecksMessage({ ctx, input, name, command, checks }) {}
164164

165165
async run(input, ctx) {
166166
if (!input.startsWith(this.prefix)) return;

0 commit comments

Comments
 (0)