简体   繁体   English

Discord.js 检查机器人是否能够将嵌入发送到特定频道

[英]Discord.js check if bot is capable of sending embeds into a specific channel

I'm trying to figure out how to check if my bot has permission to send messages and include embeds into a specific channel.我正在尝试弄清楚如何检查我的机器人是否有权发送消息并将嵌入内容包含到特定频道中。

I have found the following code that returns a boolean if a specific member has a permission in a specific channel.如果特定成员在特定频道中具有权限,我发现以下代码会返回 boolean。 but I'm unsure how to perform the same action for the bot itself.但我不确定如何为机器人本身执行相同的操作。

message.member.permissionIn('channel_id').hasPermission('SEND_MESSAGES');

You need to check if the GuildMember has permission.您需要检查 GuildMember 是否具有权限。

[Guild].me.permissionsIn('channel_id').has('SEND_MESSAGES')

None of the other answers explain how the get the GuildMember object, so I'll try explaining it.其他答案都没有解释如何获得GuildMember object,因此我将尝试对其进行解释。 All you need to do is get the GuildMember from your client .您需要做的就是从您的client处获取GuildMember To do this, use client.guilds.cache.get(guild_id).members.me to your the bot's GuildMember , then from there you can use the same code.为此, client.guilds.cache.get(guild_id).members.me用于您的机器人的GuildMember ,然后您可以从那里使用相同的代码。 Try this:尝试这个:

client.guilds.cache.get(guild_id).members.me.permissionsIn(channel_id).has('SEND_MESSAGES')

where guild_id is the server ID and channel_id is the channel ID.其中guild_id是服务器 ID, channel_id是频道 ID。

Alternatively, if you already have the Message object, then you can just the the Guild from there.或者,如果您已经收到Message object,那么您可以从那里直接进入Guild Like this:像这样:

message.guild.members.me.permissionsIn(channel_id).has('SEND_MESSAGES')

In Discord.JS v14, you can do the following:在 Discord.JS v14 中,您可以执行以下操作:

const { PermissionFlagsBits } = require("discord.js");

// Check for permissions
if (!channel.permissionsFor(interaction.guild.members.me).has(PermissionFlagsBits.SendMessages)) {
    // No permissions!
}

In which channel is your channel object你在哪个channel object

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM