From eff01b65de3f119e5abaa3ae28ef2ddbbc90e3f6 Mon Sep 17 00:00:00 2001 From: xHyroM Date: Thu, 20 Oct 2022 12:39:44 +0200 Subject: [PATCH] feat: add removeExclamationFromNicknames --- src/index.ts | 6 ++++++ src/utils/discord.ts | 39 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+) create mode 100644 src/utils/discord.ts diff --git a/src/index.ts b/src/index.ts index d686e2c..af6ed5d 100644 --- a/src/index.ts +++ b/src/index.ts @@ -13,9 +13,15 @@ import registerCommands from './utils/registerCommands'; import { Option, OptionOptions } from './structures/Option'; import { AutocompleteContext } from './structures/contexts/AutocompleteContext'; import { deleteIssueOrPR, fetchIssues, fetchPullRequests, setIssue, setPullRequest } from './utils/githubUtils'; +import { removeExclamationFromNicknames } from './utils/discord'; await fetchIssues(); await fetchPullRequests(); +(async() => { + Logger.info('Removing exclamation marks from nicknames...'); + await removeExclamationFromNicknames(); + Logger.info('Removing is done!'); +})(); await loadCommands(); try { await registerCommands(config.client.token, config.client.id); diff --git a/src/utils/discord.ts b/src/utils/discord.ts new file mode 100644 index 0000000..42ac34b --- /dev/null +++ b/src/utils/discord.ts @@ -0,0 +1,39 @@ +export const getDiscordGuildMembers = async() => { + let oldId; + const result: any[] = []; + + while (true) { + const members: any[] = await (await fetch( + `https://discord.com/api/v8/guilds/${process.env.DISCORD_GUILD_ID}/members?limit=1000${oldId ? `&after=${oldId}` : ''}`, + { + headers: { + Authorization: `Bot ${process.env.DISCORD_BOT_TOKEN}`, + }, + } + )).json(); + + if (!members.length) break; + + result.push(...members.map(m => ({ id: m.id, nickname: m.nick }))); + oldId = members[members.length - 1].id; + } + + return result; +} + +export const removeExclamationFromNicknames = async() => { + for (const member of await getDiscordGuildMembers()) { + if (!member.nickname?.startsWith?.('!')) continue; + + await fetch(`https://discord.com/api/v8/guilds/${process.env.DISCORD_GUILD_ID}/members/${member.id}`, { + method: 'PATCH', + headers: { + Authorization: `Bot ${process.env.DISCORD_BOT_TOKEN}`, + 'Content-Type': 'application/json', + }, + body: JSON.stringify({ + nick: member.nickname.slice(1), + }), + }); + } +} \ No newline at end of file