Guild in the presence of discord bot - javascript

I am trying to put the guild in the presence of discord bot, but I can't get it, the version is v12 this is my code. example (Watching 76 servers)
function presence(){
client.user.setPresence({
status: "online",
activity: {
name: "{len(bot.guilds)}",
type: "WATCHING",
}
});
}
Sample image:

I'm assuming you're trying to show the number of guilds the bot is in.
// Discord.js v13
client.user.setPresence({
status: 'online',
activities: [{
name: `${client.guilds.cache.size} servers`,
type: 'WATCHING'
}]
)
// Discord.js v12
client.user.setPresence({
status: 'online',
activity: {
name: `${client.guilds.cache.size} servers`,
type: 'WATCHING'
}
)
Alternatively, you can use setActivity if you are not changing the client's status (as it defaults to online):
client.user.setActivity(`${client.guilds.cache.size} servers`, {type: 'WATCHING'})

Related

Discord.js channel manage channel permissions

I'm having a little issue creating a new channel. I want to create a new channel using the following code:
message.guild.channels.create('channel name', {
type: 'voice',
permissionOverwrites: [
{
id: some_id,
deny: ['VIEW_CHANNEL'],
},
{
id: bot_id,
deny: ['MANAGE_CHANNEL'],
},
],
})
However, I'm getting an error saying the bitfield of manage channel is invalid. I have tried to lookup a list of channel permissions but I couldn't find any.
Error : [Symbol(code)]: 'BITFIELD_INVALID'
Can anyone help me ?
const { ChannelType, PermissionsBitField } = require('discord.js');
guild.channels.create({
name: 'new-channel',
type: ChannelType.GuildText,
permissionOverwrites: [
{
id: interaction.guild.id,
deny: [PermissionsBitField.Flags.ViewChannel],
},
{
id: interaction.user.id,
allow: [PermissionsBitField.Flags.ViewChannel],
},
],
});
Discord.js#v14 has change all BitField check it out this link: https://discordjs.guide/popular-topics/permissions.html#adding-overwrites

discord.js - TypeError [INVALID_TYPE]: Supplied parameter is not a User nor a Role

If I press the button, the following error occurs: TypeError [INVALID_TYPE]: Supplied parameter is not a User nor a Role. However, I can also put a solid id there and this error still occurs.
button.guild.channels.create('cs', {
type: "text",
parent_id: '802172599836213258',
permissionOverwrites: [
{
id: button.guild.roles.everyone,
deny: ['VIEW_CHANNEL', 'SEND_MESSAGES', 'READ_MESSAGE_HISTORY']
},
{
id: button.clicker.id,
allow: ['VIEW_CHANNEL', 'SEND_MESSAGES', 'READ_MESSAGE_HISTORY']
}
]
})
In DiscordJS 13 (maybe previous version as well), you must passed the type parameter for it to work if the role/user is not yet cached.
button.guild.channels.create('cs', {
type: "text",
parent_id: '802172599836213258',
permissionOverwrites: [
{
id: button.guild.roles.everyone,
deny: ['VIEW_CHANNEL', 'SEND_MESSAGES', 'READ_MESSAGE_HISTORY']
},
{
id: button.clicker.id,
type: "member",
allow: ['VIEW_CHANNEL', 'SEND_MESSAGES', 'READ_MESSAGE_HISTORY']
}
]
})
Explanation:
The reason why it does not work without the type is due to this code when creating a channel:
https://github.com/discordjs/discord.js/blob/34ba9d1c4c80eff7e6ac199a40232d07491432cc/packages/discord.js/src/structures/PermissionOverwrites.js#L183
if (typeof overwrite.id === 'string' && overwrite.type in OverwriteType) {
return {
id: overwrite.id,
type: overwrite.type,
allow: PermissionsBitField.resolve(overwrite.allow ?? PermissionsBitField.DefaultBit).toString(),
deny: PermissionsBitField.resolve(overwrite.deny ?? PermissionsBitField.DefaultBit).toString(),
};
}
const userOrRole = guild.roles.resolve(overwrite.id) ?? guild.client.users.resolve(overwrite.id);
if (!userOrRole) throw new TypeError(ErrorCodes.InvalidType, 'parameter', 'User nor a Role');
const type = userOrRole instanceof Role ? OverwriteType.Role : OverwriteType.Member;
And here when using the PermissionOverwriteManager:
https://github.com/discordjs/discord.js/blob/5d8bd030d60ef364de3ef5f9963da8bda5c4efd4/packages/discord.js/src/managers/PermissionOverwriteManager.js#L95
async upsert(userOrRole, options, overwriteOptions = {}, existing) {
let userOrRoleId = this.channel.guild.roles.resolveId(userOrRole) ?? this.client.users.resolveId(userOrRole);
let { type, reason } = overwriteOptions;
if (typeof type !== 'number') {
userOrRole = this.channel.guild.roles.resolve(userOrRole) ?? this.client.users.resolve(userOrRole);
if (!userOrRole) throw new TypeError(ErrorCodes.InvalidType, 'parameter', 'User nor a Role');
type = userOrRole instanceof Role ? OverwriteType.Role : OverwriteType.Member;
}
....
If type is not given, then it will try to call this.client.role.resolve and this.client.users.resolve which will only check the cache.
Either update your cache by fetching the user/role, or precise the type to avoid to have to check the cache.
The only possible problem I can see is the button.guild.roles.everyone, there's no everyone property in button.guild.roles, the everyone id is the guild id, so if you replace button.guild.roles.everyone with button.guild.id, it should fix the problem.
button.guild.channels.create('cs', {
type: "text",
parent_id: '802172599836213258',
permissionOverwrites: [
{
id: button.guild.id,
deny: ['VIEW_CHANNEL', 'SEND_MESSAGES', 'READ_MESSAGE_HISTORY']
},
{
id: button.clicker.id,
allow: ['VIEW_CHANNEL', 'SEND_MESSAGES', 'READ_MESSAGE_HISTORY']
}
]
})

How do I specify options for commands?

I want to specify choices for an option for my command in Discord.js. How do I do that?
The command:
module.exports = {
name: 'gifsearch',
description: 'Returns a gif based on your search term.',
options: [{
name: 'type',
type: 'STRING',
description: 'Whether to search for gifs or stickers.',
choices: //***this is the area where I have a question***
required: true
},
{
name: 'search_term',
type: 'STRING',
description: 'The search term to use when searching for gifs.',
required: true,
}],
async execute(interaction) {
let searchTerm = interaction.options.getString('search_term')
const res = fetch(`https://api.giphy.com/v1/gifs/search?q=${searchTerm}&api_key=${process.env.giphyAPIkey}&limit=1&rating=g`)
.then((res) => res.json())
.then((json) => {
if (json.data.length <= 0) return interaction.reply({ content: `No gifs found!` })
interaction.reply({content: `${json.data[0].url}`})
})
},
};
I have read the discord.js documentation/guide and I know about the .addChoice() method, but it doesn't look like that will be compatible with my bot's current code.
The discord.js api describes this as ApplicationCommandOptionChoices.
So you basically just insert an array of this in your choices.
module.exports = {
...
choices: [
{
name: "name to display",
value: "the actual value"
},
{
name: "another option",
value: "the other value"
}
]
...
};

server-info command not showing human and bots members discord.js

I'm making a server info command and i want it to say how many humans and bots are in the server, here's the part of the embed that should do that:
{ name: 'Human members: ', value: message.guild.members.cache.filter(member => !member.user.bot).size, inline: true},
{ name: 'Bots members: ', value: message.guild.members.cache.filter(member => member.user.bot).size, inline: true},
But this is what i get:
I'm trying the command in a server with like 50 humans and 10 bots, i hope you can help
Try to fetch the members before you check the cache. members.fetch() will fetch members from Discord, even if they're offline. It returns a collection of GuildMember objects so you don't even need the cache property any more:
let members = await message.guild.members.fetch()
// ...
.addFields(
{ name: 'Human members: ', value: members.filter(member => !member.user.bot).size, inline: true},
{ name: 'Bots members: ', value: members.filter(member => member.user.bot).size, inline: true},
)

What is the executor property for the MEMBER_ROLE_UPDATE audit log if the role is managed by an integration?

I'm creating a Discord logging bot to log all the role updates to the members of my server and also who executed those changes. I've observed some of the audit log entries for the audit log action MEMBER_ROLE_UPDATE and I get this:
GuildAuditLogsEntry {
targetType: 'USER',
actionType: 'UPDATE',
action: 'MEMBER_ROLE_UPDATE',
reason: null,
executor: User {
id: '334911278298562561',
bot: false,
username: 'Pritt',
discriminator: '0780',
avatar: 'e5e205996571c0c7c4e69246027fb1f8',
flags: UserFlags { bitfield: 256 },
lastMessageID: null,
lastMessageChannelID: null
},
changes: [ { key: '$add', old: undefined, new: [Array] } ],
id: '732185365166817280',
extra: null,
target: User {
id: 'hidden',
bot: false,
username: 'hidden',
discriminator: '3203',
avatar: 'hidden',
flags: UserFlags { bitfield: 256 },
lastMessageID: null,
lastMessageChannelID: null
}
}
This is fairy simple, its a log I collected for when I updated the role of a user in my server. I can see the executor property is the user object.
My only confusion is when the role in question is managed by an integration (such as the nitro booster role or any of the bot roles). These roles cannot be manually assigned or removed by any members so the executor of the audit log entry cannot be a user. What would this property be then?

Categories

Resources