如何使用 Javascript 创建多个 discord 机器人状态
How to create multiple discord bot statuses with Javascript
我想添加一些内容,以便在播放来自 API 的块后,我希望它播放另一种状态。例如,如果它播放了几分钟的积木,然后它变成播放 "hello world" 之类的东西,然后它在几分钟后变回播放积木。以下是我目前拥有的代码:
function updateBlock() {
let response3 = axios.get(`https://google/api/getblockcount`)
.then((response3) => {
return Promise.resolve(response3);
}).catch((error) => {
console.log("Can not connect to API");
console.log(error);
return Promise.resolve({
failed: true
})
});
response3.then((response3)=> {
if (response3.failed) {
console.log("API Response Failed");
return response3;
}
let b = response3.data;
try {
Client.user.setActivity("B: " + b, { type: 'WATCHING' })
.then()
.catch(console.error);
} catch(err) {
console.log("This is an API error catch");
console.log(err);
}
});
}
Client.on("ready", () => {
console.log(botName + " online!");
updateBlock();
setInterval(() => {
updateBlock();
}, 10000);
});
假设您正在使用 discord.js 库,这是如何显示数组 textList
中的随机项目,其中也是从 API 接收到的块高度,每 1 分钟更改一次。
client.on('ready', () => {
setInterval(async ()=>{
let {data: blocks} = await axios.get('https://chain.ragnaproject.io/api/getblockcount').catch(console.log)
let textList = ['Hello World','Lorem Ipsum','Discord Bots', 'Blocks: ' + blocks]
var text = textList[Math.floor(Math.random() * textList.length)];
client.user.setActivity(text , { type: 'WATCHING' })
},60000) // milliseconds
});
你可能要用Client.user.setActivity()
: the first argument is the name of the game you want to display, the second is the options
object. options.type
defines the string that comes before the name of your game: "Playing", "Watching", "Listening to" and "Streaming". The ActivityType
s分别是:
'PLAYING'
'WATCHING'
'LISTENING'
'STREAMING'
如果您将 'STREAMING'
设置为类型,您还可以将 options.url
设置为您直播的 URL。
这是一个示例实现:
client.user.setActivity("your sample text", {
type: 'WATCHING'
});
// OR
client.user.setActivity("Overwatch", {
type: 'STREAMING',
url: "https://example.com"
});
我想添加一些内容,以便在播放来自 API 的块后,我希望它播放另一种状态。例如,如果它播放了几分钟的积木,然后它变成播放 "hello world" 之类的东西,然后它在几分钟后变回播放积木。以下是我目前拥有的代码:
function updateBlock() {
let response3 = axios.get(`https://google/api/getblockcount`)
.then((response3) => {
return Promise.resolve(response3);
}).catch((error) => {
console.log("Can not connect to API");
console.log(error);
return Promise.resolve({
failed: true
})
});
response3.then((response3)=> {
if (response3.failed) {
console.log("API Response Failed");
return response3;
}
let b = response3.data;
try {
Client.user.setActivity("B: " + b, { type: 'WATCHING' })
.then()
.catch(console.error);
} catch(err) {
console.log("This is an API error catch");
console.log(err);
}
});
}
Client.on("ready", () => {
console.log(botName + " online!");
updateBlock();
setInterval(() => {
updateBlock();
}, 10000);
});
假设您正在使用 discord.js 库,这是如何显示数组 textList
中的随机项目,其中也是从 API 接收到的块高度,每 1 分钟更改一次。
client.on('ready', () => {
setInterval(async ()=>{
let {data: blocks} = await axios.get('https://chain.ragnaproject.io/api/getblockcount').catch(console.log)
let textList = ['Hello World','Lorem Ipsum','Discord Bots', 'Blocks: ' + blocks]
var text = textList[Math.floor(Math.random() * textList.length)];
client.user.setActivity(text , { type: 'WATCHING' })
},60000) // milliseconds
});
你可能要用Client.user.setActivity()
: the first argument is the name of the game you want to display, the second is the options
object. options.type
defines the string that comes before the name of your game: "Playing", "Watching", "Listening to" and "Streaming". The ActivityType
s分别是:
'PLAYING'
'WATCHING'
'LISTENING'
'STREAMING'
如果您将 'STREAMING'
设置为类型,您还可以将 options.url
设置为您直播的 URL。
这是一个示例实现:
client.user.setActivity("your sample text", {
type: 'WATCHING'
});
// OR
client.user.setActivity("Overwatch", {
type: 'STREAMING',
url: "https://example.com"
});