Hello, I am getting this error:
Error: There was an error sending your trade offer. Please try again later. (26)
at exports.makeAnError (/home/hamaad/projects/cs-go/backend/node_modules/steam-tradeoffer-manager/lib/helpers.js:17:12)
at SteamCommunity.<anonymous> (/home/hamaad/projects/cs-go/backend/node_modules/steam-tradeoffer-manager/lib/classes/TradeOffer.js:349:12)
at Request._callback (/home/hamaad/projects/cs-go/backend/node_modules/steamcommunity/components/http.js:67:15)
at self.callback (/home/hamaad/projects/cs-go/backend/node_modules/request/request.js:185:22)
at Request.emit (node:events:514:28)
at Request.<anonymous> (/home/hamaad/projects/cs-go/backend/node_modules/request/request.js:1154:10)
at Request.emit (node:events:514:28)
at Gunzip.<anonymous> (/home/hamaad/projects/cs-go/backend/node_modules/request/request.js:1076:12)
at Object.onceWrapper (node:events:628:28)
at Gunzip.emit (node:events:514:28) {
eresult: 26
}
I know that this error occurs when there is items in the inventory don't exist, but I cant find the problem in my code:
const express = require('express');
const session = require('express-session');
const cookieParser = require('cookie-parser');
const passport = require('passport');
const SteamUser = require('steam-user');
const SteamStrategy = require('passport-steam').Strategy;
const SteamCommunity = require('steamcommunity');
const SteamInventory = require('get-steam-inventory');
const TradeOfferManager = require('steam-tradeoffer-manager');
const crypto = require('crypto');
const cors = require('cors');
const app = express();
const PORT = 8080;
const STEAM_API_KEY = '<API KEY>';
let community = new SteamCommunity();
let manager = new TradeOfferManager({
community: community,
language: 'en',
});
let client = new SteamUser();
client.logOn({
accountName: '<USERNAME>',
password: '<PASSWORD>',
});
manager.on('sessionExpired', function (err) {
client.webLogOn();
});
client.on('webSession', (sessionID, cookies) => {
manager.setCookies(cookies);
community.setCookies(cookies);
});
const httpServer = require('http').createServer(app);
const sessionSecret = crypto.randomBytes(32).toString('hex');
app.use(
session({ secret: sessionSecret, resave: true, saveUninitialized: true })
);
app.use((req, res, next) => {
res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader('Access-Control-Allow-Credentials', 'true');
res.setHeader(
'Access-Control-Allow-Methods',
'GET, POST, PUT, DELETE, OPTIONS'
);
res.setHeader('Access-Control-Allow-Headers', 'Content-Type, Authorization');
next();
});
app.use(
cors({
origin: ['http://localhost:3000', 'http://localhost:3001'],
methods: 'GET,POST,PATCH,PUT,DELETE',
credentials: true,
})
);
app.use(cookieParser());
app.use(passport.initialize());
app.use(passport.session());
const io = require('socket.io')(httpServer, {
cors: {
origin: ['http://localhost:3000', 'http://localhost:3001'],
methods: 'GET,POST,PATCH,PUT,DELETE',
credentials: true,
},
});
try {
io.on('connection', (socket) => {
io.to(socket.id).emit('connected');
});
} catch (error) {
console.log(error);
}
passport.use(
new SteamStrategy(
{
returnURL: 'http://localhost:8080/auth/steam/return',
realm: 'http://localhost:8080/',
apiKey: STEAM_API_KEY,
},
function (identifier, profile, done) {
process.nextTick(function () {
profile.identifier = identifier;
return done(null, profile);
});
}
)
);
passport.serializeUser(function (user, done) {
done(null, user);
});
passport.deserializeUser(function (obj, done) {
done(null, obj);
});
app.get('/auth/steam', passport.authenticate('steam'));
app.get(
'/auth/steam/return',
passport.authenticate('steam', { failureRedirect: '/' }),
(req, res) => {
res.redirect('http://localhost:3000/');
}
);
function isAuthenticated(req, res, next) {
console.log(req.isAuthenticated(), 'auth');
if (req.isAuthenticated()) {
return next();
}
res.redirect('/');
}
app.get('/user', isAuthenticated, (req, res) => {
try {
res.json({ user: req?.user?._json });
} catch (error) {
res.json({ message: error.message });
}
});
app.get('/inventory', isAuthenticated, async (req, res) => {
const steamId = req?.user?._json?.steamid;
SteamInventory.getinventory(730, steamId, '2')
.then((data) => {
res.json({ invetory: data });
})
.catch((err) => res.send(err));
});
let coinflipRooms = [];
io.on('connection', (socket) => {
const sendRoomsInterval = 5000;
function sendRooms() {
socket.emit('getCoinFlipRooms', coinflipRooms);
}
const sendRoomsTimer = setInterval(sendRooms, sendRoomsInterval);
socket.on(
'createCoinflipRoom',
({ roomId, player, invertoryItem, roomOwner, coin, tradeUrl }) => {
const owner = { player, invertoryItem, roomOwner, coin, tradeUrl };
const room = { roomId, players: [owner] };
coinflipRooms.push(room);
socket.emit('coinflipRoomCreated', room); // Notify all clients about the new room
}
);
socket.on(
'joinCoinflipRoom',
({ roomId, player, invertoryItem, roomOwner, coin, tradeUrl }) => {
if (coinflipRooms.length === 0) {
io.emit(`coinFlipRoomError${player.steamId}`, 'Room not found');
} else {
const joinedPlayer = { player, invertoryItem, roomOwner, coin, tradeUrl };
coinflipRooms.forEach((room) => {
if (room.roomId === roomId) {
room.players.push(joinedPlayer);
}
});
socket.emit('coinflipPlayerJoined', coinflipRooms); // Notify all clients in the room about the new player
const selectWinnerInterval = 3000;
async function selectRandomPlayer(room) {
const randomIndex = Math.floor(Math.random() * room?.players.length);
const randomPlayer = room?.players[randomIndex]; // Winner
const loserPlayer = room?.players.find(
(player) => player !== randomPlayer
);
socket.emit('coinflipWinner', randomPlayer);
const removedCurrectRoom = coinflipRooms.filter(
(obj) => obj?.roomId !== room?.roomId
);
coinflipRooms = removedCurrectRoom;
// Transfer items to the winner
const offer = manager.createOffer(randomPlayer?.player?.tradeUrl);
console.log('Adding: ');
console.log(loserPlayer?.invertoryItem[0].asset);
offer.addTheirItem(loserPlayer?.invertoryItem[0].asset);
offer.setMessage(
`You won the coinflip! Your items: ${loserPlayer?.invertoryItem}`
);
offer.send(async function (err, status) {
if (err) {
console.log(err);
return;
}
if (status == 'pending') {
console.log(`Offer #${offer.id} sent, but requires confirmation`);
community.acceptConfirmationForObject(
await getIdentitySecret(loserPlayer?.player?.steamid),
offer.id,
function (err) {
if (err) {
console.log(err);
} else {
console.log('Offer confirmed');
}
}
);
} else {
console.log(`Offer #${offer.id} sent successfully`);
}
});
}
const autoWinnerTimer = setInterval(
() =>
selectRandomPlayer(coinflipRooms.find((room) => room.roomId === roomId)),
selectWinnerInterval
);
}
}
);
socket.on('disconnect', () => {
console.log('user disconnected');
clearInterval(autoRoomTimer);
clearInterval(autoWinnerTimer);
});
process.on('SIGINT', () => {
clearInterval(autoRoomTimer);
clearInterval(autoWinnerTimer);
process.exit();
});
});
httpServer.listen(PORT, () => {
console.log(`Server is running on http://localhost:${PORT}`);
});