我的bot得到了一个403错误,尽管它以前从未发生过,并且代码根本没有改变。
throw new DiscordAPIError(data, res.status, request);
^
DiscordAPIError: Bots cannot use this endpoint
---
{
method: 'put',
path: '/applications/---/guilds/---/commands/---/permissions',
code: 20001,
httpStatus: 403,
requestData: { json: { permissions: [Array] }, files: [] }
}
尝试设置fullPermissions
给我一个405错误而不是
DiscordAPIError: 405: Method Not Allowed
---
{
method: 'put',
path: '/applications/---/guilds/---/commands/permissions',
code: 0,
httpStatus: 405,
requestData: {
json: [
[Object], [Object], [Object],
[Object], [Object], [Object],
[Object], [Object], [Object],
[Object], [Object], [Object],
[Object], [Object], [Object],
[Object], [Object], [Object],
[Object], [Object], [Object],
[Object]
],
files: []
}
}
路径/applications/{applicationId}/guilds/{guildId}/commands/{commandId}/permissions
和/applications/{applicationId}/guilds/{guildId}/commands/permissions
之前工作得很好,为什么他们现在不能工作?
Discord昨天(2022年4月27日)向所有公会发布了Permissions V2。机器人不能再控制权限,只有服务器管理员可以。如果您想限制它,最好将简单的权限/角色/用户检查放在代码中,并删除将设置命令权限的代码。
// only run for specific user
if (interaction.user.id !== "userId") return;
// only run for specific role
if (!interaction.member.roles.cache.has("roleId")) return;
// only run for specific permission
if (!interaction.member.permissions.has("BAN_MEMBERS")) return; // You can use any permission flag of course
管理员可以通过转到Server Settings --> Integerations
来设置它,但是您可以使用具有applications.commands.permissions.update
范围的承载令牌以编程方式编辑命令权限,您可以从这样的URL获得
https://discord.com/api/oauth2/authorize?client_id=CLIENT_ID&redirect_uri=REDIRECT_URI&response_type=token&scope=applications.commands.permissions.update
您将被重定向到您设置redirect_uri
的URL参数。你可以复制access_token
部分
PUT命令权限
const applicationId = "APPLICATION_ID"
const guildId = "GUILD_ID"
const commandId = "COMMAND_ID" // Set this as your application ID to set default permissions
const url = `https://discord.com/api/v9/applications/${applicationId}/guilds/${guildId}/commands/${commandId}/permissions`
// requires the user that the bearer token belongs to have used applications.commands.permissions.update scope and have manage guild/roles permission
const token = "Bearer {token}" // replace {token} with what you copied from access_token
const payload = {
permissions: [
{
id: "ID", // role/user/channel ID
type: 1, // 1 for role, 2 for user, and 3 for channel
permission: false // whether or not that role/user can use the command or you can use the command in the channel
}
]
}
await fetch(url, {
method: "PUT",
body: JSON.stringify(payload),
headers: {
Authorization: `${token}`,
Accept: 'application/json',
'Content-Type': 'application/json'
},
})
获取命令权限
const applicationId = "APPLICATION_ID"
const guildId = "GUILD_ID"
const commandId = "COMMAND_ID" // Set this as your application ID to get default permissions
const url = `https://discord.com/api/v9/applications/${applicationId}/guilds/${guildId}/commands/${commandId}/permissions`
const token = "Bearer {token}"
await fetch(url, {
method: "GET",
headers: {
Authorization: `${token}`,
Accept: 'application/json',
'Content-Type': 'application/json'
},
})
获取所有命令权限
const applicationId = "APPLICATION_ID"
const guildId = "GUILD_ID"
const url = `https://discord.com/api/v9/applications/${applicationId}/guilds/${guildId}/commands/permissions`
const token = "Bearer {token}"
await fetch(url, {
method: "GET",
headers: {
Authorization: `${token}`,
Accept: 'application/json',
'Content-Type': 'application/json'
},
})