Skip to content

Commit de8ed03

Browse files
committed
discord.js update
1 parent 6a48bd1 commit de8ed03

File tree

10 files changed

+385
-235
lines changed

10 files changed

+385
-235
lines changed

package-lock.json

Lines changed: 240 additions & 158 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
"cron-validator": "^1.3.1",
1919
"croner": "^4.3.7",
2020
"discord-api-types": "^0.33.0",
21-
"discord.js": "^14.0.1",
21+
"discord.js": "^14.8.0",
2222
"emoji-tree": "^1.1.2",
2323
"fastify-static": "^4.7.0",
2424
"gitmoji-cli": "^4.11.0",
@@ -93,7 +93,7 @@
9393
},
9494
"overrides": {
9595
"@buildtheearth/bot-utils": {
96-
"discord.js": "^14.0.1"
96+
"discord.js": "^14.8.0"
9797
}
9898
},
9999
"repository": "BuildTheEarth/main-bot",

src/commands/reactionrole.command.ts

Lines changed: 43 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ import Command from "../struct/Command.js"
33

44
import CommandMessage from "../struct/CommandMessage.js"
55
import Args from "../struct/Args.js"
6-
import emojiTree from 'emoji-tree'
6+
import emojiTree from "emoji-tree"
7+
import getEmoji from "../util/getEmoji.util.js"
8+
import { noop } from "@buildtheearth/bot-utils"
79

810
export default new Command({
911
name: "reactionroles",
@@ -122,6 +124,18 @@ export default new Command({
122124
name: "add",
123125
description: "Add a required role",
124126
args: [
127+
{
128+
name: "emoji",
129+
description: "The emoji for the reaction role",
130+
optionType: "STRING",
131+
required: true
132+
},
133+
{
134+
name: "message_link",
135+
description: "The message link for the reaction role",
136+
optionType: "STRING",
137+
required: true
138+
},
125139
{
126140
name: "require_role",
127141
description: "The role to require",
@@ -133,7 +147,7 @@ export default new Command({
133147
{
134148
name: "delete",
135149
description: "Delete a required role",
136-
args: [
150+
args: [
137151
{
138152
name: "emoji",
139153
description: "The emoji for the reaction role",
@@ -158,29 +172,43 @@ export default new Command({
158172
}
159173
],
160174
async run(this: Command, client: Client, message: CommandMessage, args: Args) {
161-
const subcommandGroup = args.consumeSubcommandGroupIf(['blacklist', 'require'])
175+
const subcommandGroup = args.consumeSubcommandGroupIf(["blacklist", "require"])
162176
if (subcommandGroup == null) {
163177
const subcommand = args.consumeSubcommand()
164178
if (subcommand == "list") {
165179
return message.sendError("no")
166180
} else {
167181
const emoji = args.consume("emoji")
168-
const messageLink = args.consume("message_link")
169-
let realEmoji : string | undefined = undefined
170-
const emojis = emojiTree(emoji).filter((ele) => ele.type == "emoji").map((ele) => ele.text)
171-
console.log(emojis)
172-
if (emojis.length >= 1) {
173-
realEmoji = emojis[0]
174-
} else {
175-
const matches = emoji.match(/<a?:.+?:\d{16,20}>/gu)
176-
if (matches && matches.length >= 1) {
177-
realEmoji = matches[0].match(/(\d+)/)?.[0]
178-
}
179-
}
182+
const realEmoji = getEmoji(emoji)
180183
if (!realEmoji) {
181184
return message.sendErrorMessage("emojiNotFound")
182185
}
183186
console.log(realEmoji)
187+
188+
189+
const urlRegex =
190+
/(?<=(https:\/\/)(canary\.discord\.com\/channels\/|discord\.com\/channels\/|ptb\.discord\.com\/channels\/))([0-9]{17,})(\/)([0-9]{17,})(\/)([0-9]{17,})/
191+
const messageUrl = args.consume("message_link")
192+
if (!messageUrl) return message.sendErrorMessage("provideMsgUrl")
193+
if (!urlRegex.test(messageUrl)) return message.sendErrorMessage("provideMsgUrl")
194+
const messagePropsTemp = urlRegex.exec(messageUrl)
195+
if (!messagePropsTemp) return message.sendErrorMessage("provideMsgUrl")
196+
const messageProps = {
197+
guildId: messagePropsTemp[3],
198+
channelId: messagePropsTemp[5],
199+
messageId: messagePropsTemp[7]
200+
}
201+
202+
const guild = await client.guilds.fetch(messageProps.guildId).catch(noop)
203+
204+
if (!guild) {
205+
return message.sendErrorMessage("provideMsgUrl")
206+
}
207+
208+
const channel = await guild.channels.fetch(messageProps.channelId).catch(noop)
209+
210+
if (!channel) return message.sendErrorMessage("provideMsgUrl")
211+
184212
return message.sendSuccess(realEmoji)
185213
}
186214
}

src/entities/ReactionRole.entity.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import typeorm from "typeorm"
44
import Client from "../struct/Client.js"
55
import SnowflakeColumn from "./decorators/SnowflakeColumn.decorator.js"
66

7-
87
@typeorm.Entity({ name: "reaction_roles" })
98
export default class ReactionRole extends typeorm.BaseEntity {
109
@typeorm.PrimaryGeneratedColumn()
@@ -37,7 +36,7 @@ export default class ReactionRole extends typeorm.BaseEntity {
3736
}
3837

3938
public static async getChannel(channelId: string): Promise<ReactionRole[]> {
40-
return ReactionRole.find({channelId: channelId})
39+
return ReactionRole.find({ channelId: channelId })
4140
}
4241

4342
public contingent(): boolean {

src/events/messageReactionAdd.event.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ export default async function messageReactionAdd(
1111
): Promise<void> {
1212
if (reaction.partial) await reaction.fetch().catch(noop)
1313

14-
1514
const guild = reaction.message.guild
1615
if (guild) {
1716
const member: Discord.GuildMember | null = await guild.members
@@ -20,7 +19,14 @@ export default async function messageReactionAdd(
2019

2120
console.log(reaction.emoji.name)
2221

23-
if (reaction.emoji.name && member) ReactionRole.react(this, reaction.emoji.name, reaction.message.channel.id, reaction.message.id, member)
22+
if (reaction.emoji.name && member)
23+
ReactionRole.react(
24+
this,
25+
reaction.emoji.name,
26+
reaction.message.channel.id,
27+
reaction.message.id,
28+
member
29+
)
2430

2531
const channel = reaction.message.channel as Discord.TextChannel
2632

src/struct/Client.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ export default class Client extends Discord.Client {
7171
synchronize: true, //process.env.NODE_ENV !== "production",
7272
//TODO: Fix this after db updates
7373
logging: "all", //process.env.NODE_ENV !== "production" ? "all" : false
74-
charset: 'utf8mb4'
74+
charset: "utf8mb4"
7575
}
7676

7777
if (!["mariadb", "mysql", "sqlite"].includes(db.type)) {

src/struct/client/WebEvents.ts

Lines changed: 43 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,43 @@
1-
import { Client } from "discord.js";
2-
import url from 'url'
3-
import pathModule from 'path'
4-
import fs from 'fs'
5-
import { loadSyncJSON5 } from "@buildtheearth/bot-utils";
6-
7-
export default class WebEvents {
8-
client: Client
9-
10-
private objs: Map<string, any>
11-
12-
13-
constructor(client: Client) {
14-
this.client = client
15-
this.objs = new Map()
16-
}
17-
18-
public async load(): Promise<void> {
19-
const dir = pathModule.join(
20-
pathModule.dirname(url.fileURLToPath(import.meta.url)) +
21-
"/../../../config/extensions/web/replies"
22-
)
23-
const files = await fs.promises.readdir(
24-
url.pathToFileURL(dir)
25-
)
26-
for (const file of files) {
27-
this.objs.set(file.replace(/.json$/, ""), loadSyncJSON5(pathModule.join(dir, file)))
28-
}
29-
30-
}
31-
32-
public hasMessage(message: string): boolean {
33-
return this.objs.has(message)
34-
}
35-
36-
public fill(message: string, args: Record<string, any>): any {
37-
let toRet = JSON.stringify(this.objs.get(message))
38-
for (const key of Object.keys(args)) {
39-
toRet = toRet.replace("${" + key + "}", args[key].toString())
40-
}
41-
42-
return JSON.parse(toRet)
43-
}
44-
}
1+
import { Client } from "discord.js"
2+
import url from "url"
3+
import pathModule from "path"
4+
import fs from "fs"
5+
import { loadSyncJSON5 } from "@buildtheearth/bot-utils"
6+
7+
export default class WebEvents {
8+
client: Client
9+
10+
private objs: Map<string, any>
11+
12+
constructor(client: Client) {
13+
this.client = client
14+
this.objs = new Map()
15+
}
16+
17+
public async load(): Promise<void> {
18+
const dir = pathModule.join(
19+
pathModule.dirname(url.fileURLToPath(import.meta.url)) +
20+
"/../../../config/extensions/web/replies"
21+
)
22+
const files = await fs.promises.readdir(url.pathToFileURL(dir))
23+
for (const file of files) {
24+
this.objs.set(
25+
file.replace(/.json$/, ""),
26+
loadSyncJSON5(pathModule.join(dir, file))
27+
)
28+
}
29+
}
30+
31+
public hasMessage(message: string): boolean {
32+
return this.objs.has(message)
33+
}
34+
35+
public fill(message: string, args: Record<string, any>): any {
36+
let toRet = JSON.stringify(this.objs.get(message))
37+
for (const key of Object.keys(args)) {
38+
toRet = toRet.replace("${" + key + "}", args[key].toString())
39+
}
40+
41+
return JSON.parse(toRet)
42+
}
43+
}

src/struct/web/methods/api/websiteMessage.controller.ts

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,10 @@ export default class WebsiteMessage {
2020
if (!message) {
2121
return res
2222
.status(400)
23-
.send({ error: "MISSING_PARAMETER", message: "Missing parameter: message" })
23+
.send({
24+
error: "MISSING_PARAMETER",
25+
message: "Missing parameter: message"
26+
})
2427
}
2528

2629
if (body === null) {
@@ -32,22 +35,34 @@ export default class WebsiteMessage {
3235
if (!body?.params) {
3336
return res
3437
.status(400)
35-
.send({ error: "MISSING_PARAMETER", message: "Missing parameter: body.params" })
38+
.send({
39+
error: "MISSING_PARAMETER",
40+
message: "Missing parameter: body.params"
41+
})
3642
}
3743

3844
if (!body?.ids) {
3945
return res
4046
.status(400)
41-
.send({ error: "MISSING_PARAMETER", message: "Missing parameter: body.ids" })
47+
.send({
48+
error: "MISSING_PARAMETER",
49+
message: "Missing parameter: body.ids"
50+
})
4251
}
4352

4453
if (!_.isArrayLikeObject(body.ids)) {
4554
return res
4655
.status(400)
47-
.send({ error: "WRONG_DATATYPE", message: "Wrong datatype: body.ids must be an array" })
56+
.send({
57+
error: "WRONG_DATATYPE",
58+
message: "Wrong datatype: body.ids must be an array"
59+
})
4860
}
4961

50-
if (!client.webEvents.hasMessage(message)) return res.status(404).send({error: "NOT_FOUND", message: "Not found: requested message"})
62+
if (!client.webEvents.hasMessage(message))
63+
return res
64+
.status(404)
65+
.send({ error: "NOT_FOUND", message: "Not found: requested message" })
5166
console.log(body.params)
5267

5368
const finalMessage = client.webEvents.fill(message, body.params)
@@ -61,11 +76,10 @@ export default class WebsiteMessage {
6176
const res = await user.send(finalMessage).catch(noop)
6277
if (!res) failure.push(id)
6378
else success.push(id)
64-
}
65-
else failure.push(id)
79+
} else failure.push(id)
6680
} else failure.push(id)
6781
}
6882

69-
return res.status(200).send({success, failure, sentMessage: finalMessage})
83+
return res.status(200).send({ success, failure, sentMessage: finalMessage })
7084
}
7185
}

src/typings/emoji-tree.d.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1-
declare module 'emoji-tree' {
2-
export default function emojiTree(emojiText: string): {text: string, type: 'text' | 'emoji'}[]
3-
}
1+
declare module "emoji-tree" {
2+
export default function emojiTree(
3+
emojiText: string
4+
): { text: string; type: "text" | "emoji" }[]
5+
}

src/util/getEmoji.util.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import emojiTree from "emoji-tree"
2+
3+
export default function getEmoji(emoji: string): string | undefined {
4+
let realEmoji: string | undefined = undefined
5+
const emojis = emojiTree(emoji)
6+
.filter(ele => ele.type == "emoji")
7+
.map(ele => ele.text)
8+
9+
if (emojis.length >= 1) {
10+
realEmoji = emojis[0]
11+
} else {
12+
const matches = emoji.match(/<a?:.+?:\d{16,20}>/gu)
13+
if (matches && matches.length >= 1) {
14+
realEmoji = matches[0].match(/(\d+)/)?.[0]
15+
}
16+
}
17+
18+
return realEmoji
19+
20+
}

0 commit comments

Comments
 (0)