Skip to content

Commit

Permalink
v1.1.10 - autocomplete fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
itzTheMeow committed Sep 16, 2023
1 parent c700745 commit aa157b9
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 16 deletions.
6 changes: 6 additions & 0 deletions core/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
v1.1.10

- Added tag support for autocomplete.
- Added tag support for `expandMentions` in message payload.
- Fixed autocomplete showing for times. (ex. 10:45 autocompletes with ":45")

v1.1.9

- Added `User.discriminator`, `User.tag`, and `User.displayName`.
Expand Down
2 changes: 1 addition & 1 deletion core/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "revkit",
"version": "1.1.9",
"version": "1.1.10",
"description": "An alternative to revolt.js that aims to be familiar to use.",
"main": "dist/cjs/index.js",
"module": "dist/es6/index.js",
Expand Down
2 changes: 1 addition & 1 deletion core/src/objects/User.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export class User extends BaseObject<APIUser> {
}
/** Full tag for this user. (username#discriminator) */
public get tag() {
return this.username + "#" + this.discriminator;
return `${this.username}#${this.discriminator}`;
}
/** This user's display name. (if any) */
public get displayName() {
Expand Down
32 changes: 19 additions & 13 deletions core/src/utils/Autocomplete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ function sortlen<Obj extends Record<string, any>>(items: Obj[], prop: keyof Obj)
* @param channel The channel to use for calculation of channel/user mentions.
* @param text The text to calculate against.
* @param cursorPos The position of the cursor in the text.
* @param unique If `emojis` is set, uses emoji's `uniqueName` instead of ID for autocomplete. If `users` is set, uses user's username for autocomplete. (pairs nicely with Channel.send expandMentions/expandEmojis)
* @param unique If `emojis` is set, uses emoji's `uniqueName` instead of ID for autocomplete. If `users` is set, uses user's tag for autocomplete. (pairs nicely with Channel.send expandMentions/expandEmojis)
* @returns An AutocompleteResult with matched items.
*/
export function parseAutocomplete(
Expand Down Expand Up @@ -86,7 +86,7 @@ export function parseAutocomplete(
newText =
textBeforeCursor.replace(
new RegExp(`\\${i.delimiter}(\\S+)?$`, "i"),
(unique?.users ? "@%" : i.result).replace("%", unique?.users ? item.username : item.id)
(unique?.users ? "@%" : i.result).replace("%", unique?.users ? item.tag : item.id)
) + " ";
}
const totalText = newText + text.slice(cursorPos);
Expand All @@ -102,11 +102,17 @@ export function parseAutocomplete(
.match(new RegExp(`\\${i.delimiter}([^\\s\\${i.delimiter}]+)?$`, "i"))?.[0]
?.substring(i.delimiter.length)
.toLowerCase();
if (typeof matchedText !== "string") return (failed += 1);
const beforeMatch = textBeforeCursor.slice(
0,
textBeforeCursor.length - (matchedText.length + i.delimiter.length)
);
if (
typeof matchedText !== "string" ||
textBeforeCursor
.slice(0, textBeforeCursor.length - (matchedText.length + i.delimiter.length))
.match(new RegExp(`:(([^\\s\\${i.delimiter}]{1,26}))$`))
beforeMatch.match(new RegExp(`:(([^\\s\\${i.delimiter}]{1,26}))$`)) ||
// Matches whitespace or beginning of line
// V Matches 1-2 digits. VVV
// V V "Meant to prevent autocomplete for times (ex. typing 10:45)"
beforeMatch.match(/(\s|^)\d{1,2}$/)
)
return (failed += 1);
switch (i.type) {
Expand Down Expand Up @@ -162,7 +168,7 @@ export function parseAutocomplete(
? channel.server.members.filter(
(m) =>
m.nickname?.toLowerCase().includes(matchedText) ||
m.user?.username.toLowerCase().includes(matchedText)
m.user?.tag.toLowerCase().includes(matchedText)
)
: [
...new Set(
Expand All @@ -174,20 +180,20 @@ export function parseAutocomplete(
(u1, u2) =>
channel.lastMessageBy(u2)?.createdAt - channel.lastMessageBy(u1)?.createdAt
)
.map((user) => ({ name: user.username, user }))
).map((i) => ({ name: i.nickname || i.user?.username || "", user: i.user }))
.map((user) => ({ name: user.tag, user }))
).map((i) => ({ name: i.nickname || i.user?.tag || "", user: i.user }))
: channel.isGroupDM()
? [channel.client.user, ...channel.recipients]
.filter((u) => u.username.toLowerCase().includes(matchedText))
.filter((u) => u.tag.toLowerCase().includes(matchedText))
.map((user) => ({
name: user.username,
name: user.tag,
user,
}))
: channel.isDM()
? [channel.client.user, channel.recipient]
.filter((u) => u.username.toLowerCase().includes(matchedText))
.filter((u) => u.tag.toLowerCase().includes(matchedText))
.map((user) => ({
name: user.username,
name: user.tag,
user,
}))
: [];
Expand Down
2 changes: 1 addition & 1 deletion core/src/utils/Messaging.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export function constructMessagePayload(data: MessagePayload, channel?: Channel)
.forEach(
(m) =>
(opts.content = opts.content.replace(
new RegExp(escapeRegex(`@${m.user.username}`), "g"),
new RegExp(escapeRegex(`@${m.user.tag}`), "g"),
`<@${m.id}>`
))
);
Expand Down

0 comments on commit aa157b9

Please sign in to comment.