问题描述
按照音乐机器人教程中的官方 discord4j 命令指南,我现在有一个名为“Command”的接口,其中包含 $Uri = "http://127.0.0.1:8989" # for example
# Shows processes that have registered URLs with HTTP.sys
$QueueText = netsh http show servicestate view=requestq verbose=yes | Out-String
# Break into text chunks; discard the header
$Queues = $QueueText -split '(?<=\n)(?=Request queue name)' | Select-Object -Skip 1
# Find the chunk for the request queue listening on your URI
$Queue = @($Queues) -match [regex]::Escape($Uri -replace '/$')
if ($Queue.Count -eq 1)
{
# Will be null if Could not pick out exactly one PID
$ProcessId = [string]$Queue -replace '(?s).*Process IDs:\s+' -replace '(?s)\s.*' -as [int]
if ($ProcessId)
{
Write-Verbose "Identified process $ProcessId as the HTTP listener. Killing..."
Stop-Process -Id $ProcessId -Confirm
}
}
,然后在包含 main() 的类中,我有一个哈希图来包含命令>
void execute(MessageCreateEvent event);
public static final Map<String,Command> commands = new HashMap<>();
然后在 MessageCreateEvent 中执行命令,我有
static {
commands.put("ping",event -> event.getMessage()
.getChannel().block()
.createMessage("Pong!").block());
}
那行得通!如果我发送 ;ping,我会得到“Pong!”,但我还计划让我的机器人能够踢、静音和禁止,这应该需要特定的权限,但我不知道如何向这个系统添加权限,因为这这是我的第一个实际项目,所以我的经验很少。所以我问,我怎么能这样做?
解决方法
向机器人授予权限的最佳方式是通过他的邀请链接指定他需要哪些权限。一种简单的方法是使用 permissions calculator。
链接的站点可以生成一个邀请链接(您需要使用您的机器人 ID 完成),该链接将在加入服务器时请求必要的权限。这将专门为机器人创建一个角色,以授予对指定权限的访问权限。
如果您已经在服务器上邀请了您的机器人并且想要更改他的权限,您可以通过手动修改授予机器人角色的权限来实现。