Skip to content

Commit

Permalink
🔀 Merge pull request #1746 from Hhanuska/master
Browse files Browse the repository at this point in the history
feat: Steam WebAPI key in env / use accessToken if not set
  • Loading branch information
idinium96 authored May 2, 2024
2 parents d4d19a2 + 4de3c2b commit e7a88e7
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 18 deletions.
11 changes: 7 additions & 4 deletions src/classes/Bot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ export default class Bot {
this.manager = new TradeOfferManager({
steam: this.client,
community: this.community,
// useAccessToken: false, // https://github.com/DoctorMcKay/node-steam-tradeoffer-manager/wiki/Access-Tokens
useAccessToken: !this.options.steamApiKey, // https://github.com/DoctorMcKay/node-steam-tradeoffer-manager/wiki/Access-Tokens
language: 'en',
pollInterval: -1,
cancelTime: 15 * 60 * 1000,
Expand Down Expand Up @@ -960,7 +960,7 @@ export default class Bot {
});
},
(callback): void => {
log.info('Getting Steam API key...');
log.info('Setting cookies...');
void this.setCookies(cookies).asCallback(callback);
},
(callback): void => {
Expand All @@ -984,8 +984,7 @@ export default class Bot {
},
(callback): void => {
this.schemaManager = new SchemaManager({
apiKey: this.manager.apiKey,
updateTime: 24 * 60 * 60 * 1000,
updateTime: 1 * 60 * 60 * 1000,
lite: true
});

Expand Down Expand Up @@ -1247,6 +1246,10 @@ export default class Bot {
this.listingManager.setUserID(this.userID);
}

if (this.options.steamApiKey) {
this.manager.apiKey = this.options.steamApiKey;
}

return new Promise((resolve, reject) => {
this.manager.setCookies(cookies, err => {
if (err) {
Expand Down
24 changes: 17 additions & 7 deletions src/classes/Friends.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,26 @@ export default class Friends {

get getMaxFriends(): Promise<number> {
return new Promise((resolve, reject) => {
const params: {
steamid: string;
key?: string;
access_token?: string;
} = {
steamid: (this.bot.client.steamID === null
? this.bot.handler.getBotInfo.steamID
: this.bot.client.steamID
).getSteamID64()
};

if (this.bot.manager.apiKey) {
params.key = this.bot.manager.apiKey;
} else {
params.access_token = this.bot.manager.accessToken;
}
void axios({
url: 'https://api.steampowered.com/IPlayerService/GetBadges/v1/',
method: 'GET',
params: {
key: this.bot.manager.apiKey,
steamid: (this.bot.client.steamID === null
? this.bot.handler.getBotInfo.steamID
: this.bot.client.steamID
).getSteamID64()
}
params
})
.then(response => {
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-member-access
Expand Down
2 changes: 2 additions & 0 deletions src/classes/Options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2178,6 +2178,7 @@ export default interface Options extends JsonOptions {
steamPassword?: string;
steamSharedSecret?: string;
steamIdentitySecret?: string;
steamApiKey?: string;

bptfAccessToken?: string;
bptfApiKey?: string;
Expand Down Expand Up @@ -2494,6 +2495,7 @@ export function loadOptions(options?: Options): Options {
steamPassword: getOption('steamPassword', '', String, incomingOptions),
steamSharedSecret: getOption('steamSharedSecret', '', String, incomingOptions),
steamIdentitySecret: getOption('steamIdentitySecret', '', String, incomingOptions),
steamApiKey: getOption('steamApiKey', '', String, incomingOptions),

bptfAccessToken: getOption('bptfAccessToken', '', String, incomingOptions),
bptfApiKey: getOption('bptfApiKey', '', String, incomingOptions),
Expand Down
17 changes: 13 additions & 4 deletions src/classes/TF2Inventory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,13 +99,22 @@ export default class TF2Inventory {

private fetch(): Promise<void> {
return new Promise((resolve, reject) => {
const params: {
steamid: string;
key?: string;
access_token?: string;
} = { steamid: this.getSteamID.toString() };

if (this.manager.apiKey) {
params.key = this.manager.apiKey;
} else {
params.access_token = this.manager.accessToken;
}

void axios({
url: 'https://api.steampowered.com/IEconItems_440/GetPlayerItems/v0001/',
method: 'GET',
params: {
key: this.manager.apiKey,
steamid: this.getSteamID.toString()
}
params
})
.then(response => {
const body = response.data as GetPlayerItems;
Expand Down
2 changes: 2 additions & 0 deletions src/types/modules/@tf2autobot/tradeoffer-manager/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ declare module '@tf2autobot/tradeoffer-manager' {

apiKey: string | null;

accessToken: string | null;

pollInterval: number;

getUserInventoryContents(
Expand Down
5 changes: 2 additions & 3 deletions template.ecosystem.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"STEAM_PASSWORD": "",
"STEAM_SHARED_SECRET": "",
"STEAM_IDENTITY_SECRET": "",
"STEAM_API_KEY": "",

"BPTF_ACCESS_TOKEN": "",
"BPTF_API_KEY": "",
Expand All @@ -28,9 +29,7 @@
"STEAMSUPPLY_API_KEY": "",
"STEAMAPIS_API_KEY": "",

"ADMINS": [
{ "steam": "<your steamid 64>", "discord": null }
],
"ADMINS": [{ "steam": "<your steamid 64>", "discord": null }],
"KEEP": ["<steamid of person to keep in friendslist>"],
"ITEM_STATS_WHITELIST": [],
"GROUPS": ["103582791469033930"],
Expand Down
1 change: 1 addition & 0 deletions template.env
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ STEAM_ACCOUNT_NAME=""
STEAM_PASSWORD=""
STEAM_SHARED_SECRET=""
STEAM_IDENTITY_SECRET=""
STEAM_API_KEY=""

BPTF_ACCESS_TOKEN=""
BPTF_API_KEY=""
Expand Down

0 comments on commit e7a88e7

Please sign in to comment.