通过C#中的Discord Bot授予角色?

问题描述 投票:-1回答:1

我发现无法使用我的机器人将用户的角色/等级设置为“PLEB”。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Discord;
using Discord.Commands;


namespace Spoxbot.Modules
{
public class Mod : ModuleBase<SocketCommandContext>
{
    [Command("geoff")]
    public async Task geoff([Remainder]string user)
    {
        var role = Context.Guild.Roles.FirstOrDefault(x => x.Name == "PLEB");
        await (user as IGuildUser).AddRoleAsync(role);
    }

}
}

我想要发生的是,无论何时用户键入!geoff,它都会为用户提供“PLEB”

此机器人具有管理员权限/权限。

c# bots discord
1个回答
1
投票

目前,您的命令旨在为用户提供必须在命令中包含角色的用户。

!geoff insertuseridhere

正如您的问题所示,您希望每个键入!geoff的用户都能获得排名。

要实现这一点,您可以使用命令中的上下文

[Command("geoff")]
public async Task geoff()
{
    var role = Context.Guild.Roles.FirstOrDefault(x => x.Name == "PLEB");
    await (context.User as IGuildUser).AddRoleAsync(role);
}

编辑根据您的评论,您似乎希望它能够提及。

为此,您可以使用用户。

[Command("geoff")]
public async Task geoff(IUser user)
{
    var role = Context.Guild.Roles.FirstOrDefault(x => x.Name == "PLEB");
    await (user as IGuildUser).AddRoleAsync(role);
}
© www.soinside.com 2019 - 2024. All rights reserved.