[英]Refresh Value NodeJS (DiscordJS) GameDig
I'm making a Discord bot that allows to give indications of a game server.我正在制作一个 Discord 机器人,它可以指示游戏服务器。 I use the GameDig library, but the values don't refresh.
我使用 GameDig 库,但值不会刷新。 I have to restart the app (
bot.js
) to see the changes.我必须重新启动应用程序 (
bot.js
) 才能看到更改。 I'd like it to refresh every x
time automatically...我希望它每
x
次自动刷新一次...
I tried with setInterval(() => {... }, 6000);
我试过
setInterval(() => {... }, 6000);
. .
const Gamedig = require('gamedig');
Gamedig.query({
type: 'garrysmod',
host: 'xx.xx.xx.xx',
port: '27015'
}).then((state) => {
console.log(state);
let nom = state.name
let carte = state.map
let joueursmax = state.maxplayers
let joueurs = state.players.length
let latence = state.ping
client.on('ready', () => {
client.user.setStatus('online')
setInterval(() => {
client.user.setActivity(joueurs+'/'+joueursmax+' Joueurs');
}, 6000);
Also, I can't use variables from the game state
in my message handler ( nom
, carte
, etc.):此外,我不能在我的消息处理程序(
nom
、 carte
等)中使用游戏state
中的变量:
client.on('message', message => {
if (!message.content.startsWith(prefix) || message.author.bot) return;
const args = message.content.slice(prefix.length).trim().split(/ +/);
const command = args.shift().toLowerCase();
// Commandes State
if (command === 'state') {
message.channel.send('__**Nom**__: '+nom);
message.channel.send('__**Map**__: '+carte);
message.channel.send('__**Joueurs**__: '+joueurs+'/'+joueursmax);
message.channel.send('__**Latence**__: '+latence+' ms');
}
// other commands...
if (command === 'say') {
message.delete().catch()
message.channel.send(args.join(" ")).catch(console.error);
}
if (command === 'ping') {
message.channel.send(` BotServer01: Latency is ${Date.now() - message.createdTimestamp}ms. API Latency is ${Math.round(client.ws.ping)}ms`);
}
});
You are querying your game server only once.您只查询一次游戏服务器。 If you want to refresh your value and send it to discord, then you have to change the order.
如果您想刷新您的值并将其发送到 discord,那么您必须更改顺序。
// connected to your discord client
client.on('ready', () => {
client.user.setStatus('online')
// you want to refresh your data every 6 seconds
setInterval(() => {
// query your game server
Gamedig.query({
type: 'garrysmod',
host: 'xx.xx.xx.xx',
port: '27015'
}).then((state) => {
const joueursmax = state.maxplayers
const joueurs = state.players.length
// send new data to your discord server
client.user.setActivity(joueurs+'/'+joueursmax+' Joueurs');
});
}, 6000);
});
On a side note, you should use const
over let
when you can .附带说明一下,您应该尽可能使用
const
而不是let
。
You can declare state
on the top so you can access it in both client.on('message')
and client.on('ready')
.您可以在顶部声明
state
以便您可以在client.on('message')
和client.on('ready')
中访问它。 You can update this state
variable from your interval every time you receive a response from your game server.每次收到来自游戏服务器的响应时,您都可以根据时间间隔更新此
state
变量。
You can set up this interval when the bot is ready, but make sure that you fetch the state every x
seconds if you want to update it.您可以在机器人准备就绪时设置此间隔,但如果您想更新它,请确保每
x
秒获取一次 state。 In your current code, when you fetch outside the setInterval
, it only fetches once, and uses the same state every time.在您当前的代码中,当您在
setInterval
之外获取时,它只获取一次,并且每次都使用相同的 state。 So, you need to move it into the setInterval
's callback.因此,您需要将其移至
setInterval
的回调中。
I've used the destructuring assignment syntax to get and rename state variables.我使用解构赋值语法来获取和重命名 state 变量。 I've also used async/await to handle promises, maybe it's a bit easier to read:
我还使用 async/await 来处理 Promise,也许它更容易阅读:
// declare state outside of client.on, so you can access it in both "ready"
// and "message"
let state = null;
client.on('message', (message) => {
if (!message.content.startsWith(prefix) || message.author.bot) return;
const args = message.content.slice(prefix.length).trim().split(/ +/);
const command = args.shift().toLowerCase();
if (command === 'state') {
// if state is null just send an error message
if (!state) {
return message.channel.send('Oops, game state is not yet available');
}
// destructure state
const {
name: nom,
map: carte,
maxplayers: joueursmax,
ping: latence,
players: { length: joueurs },
} = state;
message.channel.send('__**Nom**__: ' + nom);
message.channel.send('__**Map**__: ' + carte);
message.channel.send('__**Joueurs**__: ' + joueurs + '/' + joueursmax);
message.channel.send('__**Latence**__: ' + latence + ' ms');
}
// other commands...
});
client.on('ready', () => {
client.user.setStatus('online');
setInterval(async () => {
try {
// update the state declared outside these functions
// no const, let, var keywords needed
state = await Gamedig.query({
type: 'garrysmod',
host: 'xx.xx.xx.xx',
port: '27015',
});
const {
maxplayers: joueursmax,
players: { length: joueurs },
} = state;
client.user.setActivity(`${joueurs}/${joueursmax} Joueurs`);
} catch (e) {
// you need to handle the error somehow!
console.log(e);
}
}, 6000);
});
If you don't want to use async/await and destructuring, this one should work:如果你不想使用 async/await 和解构,这个应该可以工作:
let state = null;
client.on('message', (message) => {
if (!message.content.startsWith(prefix) || message.author.bot) return;
const args = message.content.slice(prefix.length).trim().split(/ +/);
const command = args.shift().toLowerCase();
if (command === 'state') {
// if state is null, just send an error message
if (!state) {
return message.channel.send('Oops, game state is not yet available');
}
const nom = state.name;
const carte = state.map;
const joueursmax = state.maxplayers;
const joueurs = state.players.length;
const latence = state.ping;
message.channel.send('__**Nom**__: ' + nom);
message.channel.send('__**Map**__: ' + carte);
message.channel.send('__**Joueurs**__: ' + joueurs + '/' + joueursmax);
message.channel.send('__**Latence**__: ' + latence + ' ms');
}
// other commands...
});
client.on('ready', () => {
client.user.setStatus('online');
setInterval(() => {
Gamedig.query({
type: 'garrysmod',
host: 'xx.xx.xx.xx',
port: '27015',
})
.then((updatedState) => {
state = updatedState;
const joueursmax = state.maxplayers;
const joueurs = state.players.length;
client.user.setActivity(`${joueurs}/${joueursmax} Joueurs`);
})
.catch((e) => {
console.log(e);
});
}, 6000);
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.