admin管理员组文章数量:1332881
I've recently made a Discord bot using discord.js v12 and I haven't been able to set a presence for the bot.
This is the code:
client.user.setPresence({ game: { name: '.help' }, status: 'online' });
And the error is: TypeError: Cannot read property 'setPresence' of null
.
I've recently made a Discord bot using discord.js v12 and I haven't been able to set a presence for the bot.
This is the code:
client.user.setPresence({ game: { name: '.help' }, status: 'online' });
And the error is: TypeError: Cannot read property 'setPresence' of null
.
-
How did you define
client
? – Lioness100 Commented Sep 24, 2020 at 10:29
4 Answers
Reset to default 3If you read the Client class docs here, you can see that the type of the user
property has type ?ClientUser
with ?
out the front - this means that it may not be defined (it's an optional value).
If you visit the ClientUser
documentation, it says it "Represents the logged in client's Discord user."
I'm guessing that if the client has not yet fully logged in, then the user
property will be undefined. Call client.login(token)
first, which is an asynchronous function, and then you can change the presence.
This means, using promises:
client.login(token).then((token) => {
// client.user is now defined
client.user.setPresence({
game: { name: '.help' },
status: 'online',
});
});
You may also want to query that client.user
is defined either way before proceeding to prevent crashing.
I suspect the reason your existing code does not work is because if you do not set the presence in the callback for the login function, you will not yet have finished logging in by the time the rest of the code runs. The following will cause an error:
client.login(token); // bad - this is async
client.user.setPresence(...)
This isn't quite an answer, but it's too big to go in a ment. If you want to set the status of the bot right when it goes online, you can define the presence through the Discord.Client()
constructer itself. One of the Client()
constructer's options is presence
.
// at the beginning of your code:
const client = new Discord.Client({
presence: {
status: 'online',
activity: {
name: '.help',
type: 'PLAYING',
},
},
});
You can use the .setActivity()
function
client.user.setActivity(".help" {type: "PLAYING"}) //the type can be "PLAYING", "WATCHING", "STREAMING", "LISTENING"
Use this in your
client.on('ready' () => {
client.user.setActivity(".help" {type: "PLAYING"})
})
Try setting the presence in the client.on('ready', () => {})
section:
client.on('ready', () => {
console.log(`Logged in as ${client.user.tag}!`);
client.user.setPresence({
status: 'online',
activity: {
name: ".help",
type: "PLAYING"
}
});
});
Quick explanation:
The presence is what you are setting, it is made of multiple variables.
The status
can be online
, idle
, dnd
, or invisible
. (dnd is Do not disturb)
The other variable here is activity
. It is a group of two variables: name
and type
.
The name
is what the bot is doing. This is a string of your choice.
The type
is the other thing that will help it display as. It can be "PLAYING"
, "STREAMING"
, "WATCHING"
, "LISTENING"
, and "CUSTOM_STATUS"
.
本文标签: javascriptDiscord Bot PresenceStack Overflow
版权声明:本文标题:javascript - Discord Bot Presence - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742305154a2449765.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论