Skip to content

Commit

Permalink
#patch fixing dispatch announcement, added /superstore, added type …
Browse files Browse the repository at this point in the history
…defs for chats api
  • Loading branch information
jgaribsin committed Apr 20, 2024
1 parent 408bc0e commit d852394
Show file tree
Hide file tree
Showing 15 changed files with 469 additions and 99 deletions.
5 changes: 5 additions & 0 deletions .idea/.gitignore

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

57 changes: 57 additions & 0 deletions .idea/codeStyles/Project.xml

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

5 changes: 5 additions & 0 deletions .idea/codeStyles/codeStyleConfig.xml

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

12 changes: 12 additions & 0 deletions .idea/hellcom.iml

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

6 changes: 6 additions & 0 deletions .idea/inspectionProfiles/Project_Default.xml

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

8 changes: 8 additions & 0 deletions .idea/modules.xml

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

7 changes: 7 additions & 0 deletions .idea/prettier.xml

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

6 changes: 6 additions & 0 deletions .idea/vcs.xml

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

24 changes: 22 additions & 2 deletions src/api-wrapper/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@ import {writeFileSync} from 'fs';
import {getAllPlanets} from './planets';
import axios, {AxiosRequestConfig} from 'axios';
import {config} from '../config';
import {logger} from '../handlers';

const API_URL = 'https://api.live.prod.thehelldiversgame.com/api';
const CHATS_URL = 'https://api.diveharder.com/v1/all';
const {IDENTIFIER} = config;

export const seasons = {
Expand Down Expand Up @@ -92,10 +94,10 @@ export let data: ApiData = {
const axiosOpts: AxiosRequestConfig = {
headers: {
'Accept-Language': 'en-us',
'User-Agent': 'HellComBot/1.0',
},
};

let getDataCounter = 0;
export async function getData() {
const season = seasons.current;
// https://api.live.prod.thehelldiversgame.com/api/WarSeason/801/Status
Expand Down Expand Up @@ -125,6 +127,19 @@ export async function getData() {
).data;
const planetStats = statsApi as PlanetStats;

let chatsAPI;
try {
// Unofficial: api wrapper for the authed chats endpoint
chatsAPI = await (
await axios.get(CHATS_URL, {...axiosOpts, timeout: 10_000})
).data;
} catch (err) {
logger.error('Failed to fetch chats data.', {
type: 'API',
...(err as Error),
});
}

// let planetStats: PlanetStats = data.PlanetStats;
// if (getDataCounter % 2 === 0) {
// const planetStatsApi = await (
Expand Down Expand Up @@ -250,8 +265,13 @@ export async function getData() {
// this is the starting point in unix for whatever time thing they use
UTCOffset: Math.floor(status.timeUtc - status.time * 1000), // use this value to add to the time to get the UTC time in seconds
};
if (
chatsAPI &&
chatsAPI['store_rotation'] &&
chatsAPI['store_rotation'].items
)
data.SuperStore = chatsAPI['store_rotation'];

getDataCounter++;
writeFileSync('data.json', JSON.stringify(data, null, 2));
return data;
}
Expand Down
20 changes: 20 additions & 0 deletions src/api-wrapper/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,25 @@ export type NewsFeedItem = {
message: string;
};

export type StoreItem = {
name: string;
description: string;
type: 'Light' | 'Medium' | 'Heavy';
slot: 'Head' | 'Body' | 'Cloak';
armor_rating: number;
speed: number;
stamina_regen: number;
passive: {
name: string;
description: string;
};
};

export type StoreRotation = {
expire_time: Date;
items: StoreItem[];
};

export type SteamNewsItem = {
title: string;
url: string;
Expand Down Expand Up @@ -309,6 +328,7 @@ export type ApiData = {
ActivePlanets: MergedPlanetData[];
PlanetAttacks: {source: string; target: string}[];
Events: GlobalEvent[];
SuperStore?: StoreRotation;
Players: {
[key in Faction]: number;
};
Expand Down
6 changes: 5 additions & 1 deletion src/commands/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@ import dispatches from './dispatches';
import events from './events';
import history from './history';
import map from './map';
import steam from './steam';
import planet from './planet';
import subscribe from './subscribe';
import superstore from './superstore';
import wiki from './wiki';
import {Category, WikiData} from '../handlers';

Expand All @@ -27,7 +29,9 @@ const commandList: Command[] = [
history,
community,
map,
wiki,
// steam,
// wiki,
superstore,
];
const notEphemeral: string[] = [];
const ephemeralCmds = commandList
Expand Down
151 changes: 151 additions & 0 deletions src/commands/steam.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
import {
ActionRowBuilder,
ButtonBuilder,
ButtonStyle,
CommandInteraction,
EmbedBuilder,
SlashCommandBuilder,
} from 'discord.js';
import {Command} from '../interfaces';
import {FOOTER_MESSAGE} from './_components';
import axios from 'axios';
import {SteamNewsFeed} from '../api-wrapper';
import dayjs from 'dayjs';

const command: Command = {
data: new SlashCommandBuilder()
.setName('steam')
.setDescription('View the latest Helldivers 2 / bot patch notes')
.addSubcommand(subcommand =>
subcommand
.setName('news')
.setDescription('View the latest Helldivers 2 non-patch news')
)
.addSubcommand(subcommand =>
subcommand
.setName('patchnotes')
.setDescription('View the latest Helldivers 2 patch notes')
),
run: async interaction => {
const subcommand = interaction.options.data[0].name;

await subcmds[subcommand](interaction);
},
};

const subcmds: {[key: string]: (job: CommandInteraction) => Promise<void>} = {
patchnotes,
news,
};

async function patchnotes(interaction: CommandInteraction) {
const axiosOpts = {
headers: {
'User-Agent': 'HelldiversBot/1.0',
'Accept-Language': 'en-US',
},
};
const apiData = (await (
await axios.get('http://api.diveharder.com/raw/updates', {
...axiosOpts,
params: {
maxEntries: 512,
},
})
).data) as SteamNewsFeed;
const steamPatchNotes = apiData.filter(news =>
news.title.toLowerCase().includes('patch')
);

const messages = steamPatchNotes
.slice(0, 3)
.reverse()
.map(p => {
const embed = new EmbedBuilder()
.setTitle(p.title)
.setURL(p.url)
.setTimestamp(dayjs(p.date).unix() * 1000)
.setFooter({text: FOOTER_MESSAGE});

if (p.contents.length > 4000)
embed.setDescription(
p.contents.slice(0, 4000) +
'`...`\n\n## Click the button below to read more!'
);
else embed.setDescription(p.contents);

return {
embeds: [embed],
components: [
new ActionRowBuilder<ButtonBuilder>().addComponents([
new ButtonBuilder()
.setLabel('Steam Post')
.setStyle(ButtonStyle.Link)
.setURL(p.url),
]),
],
};
});

messages.forEach(async (m, i) => {
if (i === 0) await interaction.editReply({...m});
else await interaction.followUp({...m, ephemeral: true});
});
}

async function news(interaction: CommandInteraction) {
const axiosOpts = {
headers: {
'User-Agent': 'HelldiversBot/1.0',
'Accept-Language': 'en-US',
},
};
const apiData = (await (
await axios.get('https://api.diveharder.com/raw/updates', {
...axiosOpts,
params: {
maxEntries: 512,
},
})
).data) as SteamNewsFeed;
const steamNews = apiData.filter(
news => !news.title.toLowerCase().includes('patch')
);

const messages = steamNews
.slice(0, 3)
.reverse()
.map(p => {
const embed = new EmbedBuilder()
.setTitle(p.title)
.setURL(p.url)
.setTimestamp(dayjs(p.date).unix() * 1000)
.setFooter({text: FOOTER_MESSAGE});

if (p.contents.length > 4000)
embed.setDescription(
p.contents.slice(0, 4000) +
'`...`\n\n## Click the button below to read more!'
);
else embed.setDescription(p.contents);

return {
embeds: [embed],
components: [
new ActionRowBuilder<ButtonBuilder>().addComponents([
new ButtonBuilder()
.setLabel('Steam Post')
.setStyle(ButtonStyle.Link)
.setURL(p.url),
]),
],
};
});

messages.forEach(async (m, i) => {
if (i === 0) await interaction.editReply({...m});
else await interaction.followUp({...m, ephemeral: true});
});
}

export default command;
Loading

0 comments on commit d852394

Please sign in to comment.