Skip to content

Commit

Permalink
migrate: replace request 2 axios
Browse files Browse the repository at this point in the history
  • Loading branch information
Insiro committed Jul 25, 2022
1 parent c3efbe5 commit f3f9345
Show file tree
Hide file tree
Showing 12 changed files with 110 additions and 73 deletions.
35 changes: 35 additions & 0 deletions .pnp.cjs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file not shown.
Binary file not shown.
5 changes: 1 addition & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"dependencies": {
"@discordjs/builders": "^0.15.0",
"@discordjs/rest": "^0.5.0",
"axios": "^0.27.2",
"cheerio": "^1.0.0-rc.12",
"class-validator": "^0.13.2",
"cross-env": "^7.0.3",
Expand All @@ -29,8 +30,6 @@
"node-schedule": "^2.1.0",
"pg": "^8.7.3",
"reflect-metadata": "^0.1.13",
"request": "^2.88.2",
"request-promise-native": "^1.0.9",
"typeorm": "^0.3.7",
"ws": "^8.8.1",
"xml2js": "^0.4.23"
Expand All @@ -41,8 +40,6 @@
"@types/node": "^18.6.1",
"@types/node-schedule": "^2.1.0",
"@types/pg": "^8.6.5",
"@types/request": "^2.48.8",
"@types/request-promise-native": "^1.0.18",
"@types/ws": "^8.5.3",
"@types/xml2js": "^0.4.11",
"@typescript-eslint/eslint-plugin": "^5.30.7",
Expand Down
41 changes: 18 additions & 23 deletions src/newsUpdater.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import * as request from 'request-promise-native';
import * as cheerio from 'cheerio';
import { parseString } from 'xml2js';
import axios from 'axios';
import { parseStringPromise } from 'xml2js';

import { NewsDate } from './entity/NewsDate';
import { BotServer } from './entity/BotServer';
Expand All @@ -10,31 +9,27 @@ import AppDataSource from './data-sources';
const newsRepository = AppDataSource.getRepository(NewsDate);

async function getData(url: string): Promise<Array<any>> {
const re = await request.get(url);
let datas = '';
parseString(
cheerio.load(re, { xmlMode: true }).xml(),
{ mergeAttrs: true },
(err, result) => (datas = JSON.stringify(result))
);
return JSON.parse(datas).rss.channel[0].item;
}
const re = await axios.get(url);
const result= await parseStringPromise(re.data, {
mergeAttrs: true,
});

return result.rss.channel[0].item;
}
async function sendEmbed(client: Client, embed: MessageEmbed): Promise<void> {
const serverList = await BotServer.find();
serverList.forEach((server) => {
if (server.newsChannel !== null && server.newsChannel !== undefined) {
(
client.channels.cache.get(server.newsChannel) as
| TextChannel
| undefined
)?.send({ embeds:[embed] });
if (!server.newsChannel) {
const channel = client.channels.cache.get(
server.newsChannel
) as TextChannel;
if (channel) channel.send({ embeds: [embed] });
}
});
}

function pushEmbedList(
parsedData: any,
parsedData: any[],
compareDate: Date | null,
embedList: Array<MessageEmbed>
): void {
Expand Down Expand Up @@ -81,10 +76,10 @@ export async function worker(client: Client): Promise<void> {
pushEmbedList(update, null, embedList);
pushEmbedList(notice, null, embedList);
}
news.event = new Date(event[0].pubDate);
news.magazine = new Date(magazine[0].pubDate);
news.update = new Date(update[0].pubDate);
news.notic = new Date(notice[0].pubDate);
news.event = new Date(event[0].pubDate[0]);
news.magazine = new Date(magazine[0].pubDate[0]);
news.update = new Date(update[0].pubDate[0]);
news.notic = new Date(notice[0].pubDate[0]);
embedList.forEach((embed) => {
sendEmbed(client, embed);
});
Expand Down
6 changes: 3 additions & 3 deletions src/sender/Clan.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import axios from 'axios';
import { CommandInteraction, MessageEmbed } from 'discord.js';
import {
SlashCommandBuilder,
SlashCommandStringOption,
SlashCommandSubcommandBuilder,
} from '@discordjs/builders';
import * as request from 'request-promise-native';
import * as cheerio from 'cheerio';

import { BotServer } from '../entity/BotServer';
Expand Down Expand Up @@ -58,8 +58,8 @@ const playingMember = async (guidID: string): Promise<string> => {
const clanUri = await getClanLink(guidID, false);
if (clanUri === null || clanUri === undefined)
return 'not set Clan Link yet';
const re = await request.get(clanUri);
const html = cheerio.load(re);
const re = await axios.get(clanUri);
const html = cheerio.load(re.data);
const memberList = html('.member_list > p')
.text()
.split(' ')
Expand Down
29 changes: 14 additions & 15 deletions src/sender/Match.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import axios from 'axios';
import {
SlashCommandBuilder,
SlashCommandStringOption,
} from '@discordjs/builders';
import { CommandInteraction } from 'discord.js';
import { CyphersApiKey } from '../config';
import { ParseError, CyApiLink } from '../utils/values';
import * as request from 'request-promise-native';

const getPlayerStr = (userJson: any): string => {
const playerInfo = userJson.playInfo;
Expand Down Expand Up @@ -39,24 +39,23 @@ const getPlayerStr = (userJson: any): string => {
return playerString;
};

export const getMatchInfo = async (interaction:CommandInteraction): Promise<string> => {
const matchKey = interaction.options.getString("matching_id")
const options = {
uri: CyApiLink + '/matches/' + matchKey,
qs: {
apikey: CyphersApiKey,
},
};
export const getMatchInfo = async (
interaction: CommandInteraction
): Promise<string> => {
const matchKey = interaction.options.getString('matching_id');
try {
const json = JSON.parse(await request.get(options));
const outString: string = '시간 : ' + json.date;
const response = await axios.get(CyApiLink + '/matches/' + matchKey, {
params: { apikey: CyphersApiKey },
});
const result = response.data;
const outString: string = '시간 : ' + result.date;
let win;
if (json.teams[0].result === 'win') win = json.teams[0].players;
else win = json.teams[1].players;
if (result.teams[0].result === 'win') win = result.teams[0].players;
else win = result.teams[1].players;
let winstr = '';
let losestr = '';
for (const player in json.players) {
const user = json.players[player];
for (const player in result.players) {
const user = result.players[player];
let iswin = false;
for (const winner in win) {
if (win[winner] === user.playerId) {
Expand Down
15 changes: 5 additions & 10 deletions src/sender/Ranking.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import axios from 'axios';
import { CommandInteraction } from 'discord.js';
import * as request from 'request-promise-native';
import { CyphersApiKey } from '../config';
import { CyApiLink } from '../utils/values';
import {
Expand All @@ -12,15 +12,10 @@ export const getRanking = async (
): Promise<string> => {
let offset = interaction.options.getInteger('offset', false);
offset = !offset ? 0 : offset - 1;
console.log(offset)
const Options = {
uri: CyApiLink + 'ranking/ratingpoint',
qs: {
apikey: CyphersApiKey,
offset: offset,
},
};
const list = JSON.parse(await request.get(Options))['rows'];
const result = await axios.get(CyApiLink + 'ranking/ratingpoint', {
params: { apikey: CyphersApiKey, offset: offset },
});
const list = result.data['rows'];
let outString = '```markdown\n';
for (const i in list) {
const item = list[i];
Expand Down
27 changes: 12 additions & 15 deletions src/sender/Record.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as request from 'request-promise-native';
import axios from 'axios';
import {
SlashCommandBuilder,
SlashCommandStringOption,
Expand All @@ -9,15 +9,14 @@ import { CyApiLink, ParseError, parseErrorMsg } from '../utils/values';
const apiLink: string = CyApiLink + 'players/';

const getPlayerID = async (playerName: string): Promise<string> => {
const options = {
uri: apiLink,
qs: {
apikey: CyphersApiKey,
nickname: playerName,
},
};
try {
return JSON.parse(await request.get(options)).rows[0].playerId;
const result = await axios.get(apiLink, {
params: {
apikey: CyphersApiKey,
nickname: playerName,
},
});
return result.data.rows[0].playerId;
} catch (error) {
return ParseError(error as Error);
}
Expand Down Expand Up @@ -77,15 +76,13 @@ export const getRecords = async (
const playerID = await getPlayerID(user_name);
if (playerID === parseErrorMsg) return parseErrorMsg;
try {
const options = {
uri: apiLink + playerID + '/matches',
qs: {
const result = await axios.get(apiLink + playerID + '/matches', {
params: {
gameTypeId: gameTypeId,
apikey: CyphersApiKey,
},
};
const result: string = await request.get(options);
const json = JSON.parse(result);
});
const json = result.data;
return (
'```' +
json['nickname'] +
Expand Down
2 changes: 1 addition & 1 deletion src/sender/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,5 +71,5 @@ export const commands = [
match_command,
help_command,
how_command,
setup_command
setup_command,
];
2 changes: 0 additions & 2 deletions updated dependancies.txt

This file was deleted.

21 changes: 21 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1666,6 +1666,16 @@ __metadata:
languageName: node
linkType: hard

"axios@npm:^0.27.2":
version: 0.27.2
resolution: "axios@npm:0.27.2"
dependencies:
follow-redirects: ^1.14.9
form-data: ^4.0.0
checksum: 38cb7540465fe8c4102850c4368053c21683af85c5fdf0ea619f9628abbcb59415d1e22ebc8a6390d2bbc9b58a9806c874f139767389c862ec9b772235f06854
languageName: node
linkType: hard

"babel-jest@npm:^28.1.3":
version: 28.1.3
resolution: "babel-jest@npm:28.1.3"
Expand Down Expand Up @@ -2256,6 +2266,7 @@ __metadata:
"@types/xml2js": ^0.4.11
"@typescript-eslint/eslint-plugin": ^5.30.7
"@typescript-eslint/parser": ^5.30.7
axios: ^0.27.2
cheerio: ^1.0.0-rc.12
class-validator: ^0.13.2
cross-env: ^7.0.3
Expand Down Expand Up @@ -2945,6 +2956,16 @@ __metadata:
languageName: node
linkType: hard

"follow-redirects@npm:^1.14.9":
version: 1.15.1
resolution: "follow-redirects@npm:1.15.1"
peerDependenciesMeta:
debug:
optional: true
checksum: 6aa4e3e3cdfa3b9314801a1cd192ba756a53479d9d8cca65bf4db3a3e8834e62139245cd2f9566147c8dfe2efff1700d3e6aefd103de4004a7b99985e71dd533
languageName: node
linkType: hard

"forever-agent@npm:~0.6.1":
version: 0.6.1
resolution: "forever-agent@npm:0.6.1"
Expand Down

0 comments on commit f3f9345

Please sign in to comment.