feat: add removeExclamationFromNicknames

This commit is contained in:
xHyroM 2022-10-20 12:39:44 +02:00
parent 07fd5bc8a6
commit eff01b65de
2 changed files with 45 additions and 0 deletions

View file

@ -13,9 +13,15 @@ import registerCommands from './utils/registerCommands';
import { Option, OptionOptions } from './structures/Option'; import { Option, OptionOptions } from './structures/Option';
import { AutocompleteContext } from './structures/contexts/AutocompleteContext'; import { AutocompleteContext } from './structures/contexts/AutocompleteContext';
import { deleteIssueOrPR, fetchIssues, fetchPullRequests, setIssue, setPullRequest } from './utils/githubUtils'; import { deleteIssueOrPR, fetchIssues, fetchPullRequests, setIssue, setPullRequest } from './utils/githubUtils';
import { removeExclamationFromNicknames } from './utils/discord';
await fetchIssues(); await fetchIssues();
await fetchPullRequests(); await fetchPullRequests();
(async() => {
Logger.info('Removing exclamation marks from nicknames...');
await removeExclamationFromNicknames();
Logger.info('Removing is done!');
})();
await loadCommands(); await loadCommands();
try { try {
await registerCommands(config.client.token, config.client.id); await registerCommands(config.client.token, config.client.id);

39
src/utils/discord.ts Normal file
View file

@ -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),
}),
});
}
}