Skip to content

Commit

Permalink
dep: use axios and downgrade to node 16
Browse files Browse the repository at this point in the history
  • Loading branch information
tomli380576 committed Sep 15, 2022
1 parent d5d0d59 commit c89830a
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 62 deletions.
60 changes: 30 additions & 30 deletions package-lock.json

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

5 changes: 2 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
"@types/google-spreadsheet": "^3.1.5",
"@types/mocha": "^9.0.0",
"@types/node-fetch": "^2.6.1",
"@types/y18n": "^5.0.0",
"@typescript-eslint/eslint-plugin": "^4.29.3",
"@typescript-eslint/parser": "^4.29.3",
"chai": "^4.3.4",
Expand All @@ -33,6 +32,7 @@
"@discordjs/rest": "^0.1.0-canary.0",
"ascii-table": "^0.0.9",
"ascii-table3": "^0.7.7",
"axios": "^0.27.2",
"discord-api": "^0.0.1",
"discord-api-types": "^0.22.0",
"discord.js": "^13.7.0",
Expand All @@ -41,10 +41,9 @@
"google-spreadsheet": "^3.1.15",
"@google-cloud/local-auth": "^2.1.0",
"googleapis": "^100.0.0",
"node-fetch": "^2.6.6",
"y18n": "^5.0.8"
},
"engines": {
"node": "^18.0.0"
"node": "^16.17.0"
}
}
7 changes: 4 additions & 3 deletions src/command-handling/button-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,13 @@ class ButtonCommandDispatcher {
await interaction.editReply(SimpleEmbed(
successMsg,
EmbedColor.Success),
).catch(() => console.error(logEditFailure)))
.catch(async (err: UserViewableError) =>
).catch(logEditFailure)
).catch(async (err: UserViewableError) =>
// Central error handling, reply to user with the error
await interaction.editReply(
ErrorEmbed(err)
).catch(() => console.error(logEditFailure)));
).catch(logEditFailure)
);
} else {
await interaction.editReply(ErrorEmbed(
new CommandNotImplementedError('This command does not exist.'))
Expand Down
7 changes: 4 additions & 3 deletions src/command-handling/command-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,12 +93,13 @@ class CentralCommandDispatcher {
SimpleEmbed(
successMsg,
EmbedColor.Success)
).catch(logEditFailure))
.catch(async (err: UserViewableError) =>
).catch(logEditFailure)
).catch(async (err: UserViewableError) =>
// Central error handling, reply to user with the error
await interaction.editReply(
ErrorEmbed(err)
).catch(logEditFailure));
).catch(logEditFailure)
);
} else {
await interaction.editReply(ErrorEmbed(
new CommandNotImplementedError('This command does not exist.')
Expand Down
27 changes: 16 additions & 11 deletions src/extensions/session-calendar/calendar-command-extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,11 @@ class CalendarInteractionExtension extends BaseInteractionExtension {
});
const commandMethod = this.commandMethodMap.get(interaction.commandName);
if (commandMethod === undefined) {
await interaction.editReply(ErrorEmbed(
new CommandNotImplementedError('This external command does not exist.')
)).catch(logEditFailure);
await interaction.editReply(
ErrorEmbed(new CommandNotImplementedError(
'This external command does not exist.'
))
).catch(logEditFailure);
return;
}
console.log(
Expand All @@ -114,10 +116,12 @@ class CalendarInteractionExtension extends BaseInteractionExtension {
successMsg,
EmbedColor.Success)
).catch(logEditFailure)
.catch(async (err: UserViewableError) =>
await interaction.editReply(
ErrorEmbed(err)
).catch(logEditFailure)));
)
.catch(async (err: UserViewableError) =>
await interaction.editReply(
ErrorEmbed(err)
).catch(logEditFailure)
);
}

/**
Expand Down Expand Up @@ -157,11 +161,12 @@ class CalendarInteractionExtension extends BaseInteractionExtension {
SimpleEmbed(
successMsg,
EmbedColor.Success)
).catch(logEditFailure))
.catch(async (err: UserViewableError) =>
).catch(logEditFailure)
).catch(async (err: UserViewableError) =>
await interaction.editReply(
ErrorEmbed(err)
).catch(logEditFailure));
).catch(logEditFailure)
);
}

/**
Expand Down Expand Up @@ -195,7 +200,7 @@ class CalendarInteractionExtension extends BaseInteractionExtension {
` ${newCalendarName.length > 0
? ` '${newCalendarName}'. `
: ", but it doesn't have a name. "}` +
`The calendar embed will refresh soon. ` +
`The calendar embeds will refresh soon. ` +
`Or you can manually refresh it using the refresh button.`
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import {
class CalendarQueueExtension extends BaseQueueExtension {

private upcomingHours: UpComingSessionViewModel[] = [];
private display?: Readonly<QueueDisplayV2>;
private display?: Readonly<QueueDisplayV2>;

private constructor(
private readonly renderIndex: number,
Expand Down
18 changes: 7 additions & 11 deletions src/extensions/session-calendar/shared-calendar-functions.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { calendar_v3 } from "googleapis";
import { serverIdCalendarStateMap } from "./calendar-states";
import axios from 'axios';

import calendarConfig from '../extension-credentials/calendar-config.json';

// ViewModel for 1 tutor's upcoming session
Expand Down Expand Up @@ -41,14 +43,14 @@ async function getUpComingTutoringEvents(
timeMin: new Date(),
timeMax: nextWeek
});
const response = await fetch(calendarUrl);
const response = await axios.get(calendarUrl);
if (response.status !== 200) {
return Promise.reject(new CalendarConnectionError(
'Failed to connect to Google Calendar. ' +
'The calendar might be deleted or set to private.'
));
}
const responseJSON = await response.json();
const responseJSON = await response.data;
const events = (responseJSON as calendar_v3.Schema$Events).items;
if (!events || events.length === 0) {
return [];
Expand Down Expand Up @@ -81,17 +83,17 @@ async function checkCalendarConnection(
): Promise<string> {
const nextWeek = new Date();
nextWeek.setDate(nextWeek.getDate() + 7);
const url = buildCalendarURL({
const calendarUrl = buildCalendarURL({
calendarId: newCalendarId,
timeMin: new Date(),
timeMax: nextWeek,
apiKey: calendarConfig.YABOB_GOOGLE_API_KEY
});
const response = await fetch(url);
const response = await axios.get(calendarUrl);
if (response.status !== 200) {
return Promise.reject('Calendar request failed.');
}
const responseJSON = await response.json();
const responseJSON = await response.data;
return (responseJSON as calendar_v3.Schema$Events).summary ?? '';
}

Expand All @@ -116,27 +118,21 @@ function composeViewModel(
if (words.length !== 2) {
return undefined;
}

const punctuations = /[,]/g;
const tutorName = words[0]?.trim();
const eventQueues = words[1]?.trim().split(', ')
.map(eventQueue => eventQueue
?.replace(punctuations, '')
.trim());

// eventQueues will be:
// ["ECS 20", "ECS 36A", "ECS 36B", "ECS 122A", "ECS 122B"]

if (eventQueues?.length === 0 || tutorName === undefined) {
return undefined;
}

const targetQueue = eventQueues?.find(eventQueue => queueName === eventQueue);

if (targetQueue === undefined) {
return undefined;
}

return {
start: start,
end: end,
Expand Down

0 comments on commit c89830a

Please sign in to comment.