【问题标题】:Give user a role DSharpPlus给用户一个角色 DSharpPlus
【发布时间】:2019-04-27 15:11:06
【问题描述】:

我需要替换以下代码中的IGuildUser。 (我在 DSharpPlus 中编码:https://github.com/DSharpPlus/DSharpPlus):

    [Command("i_accept"), Description("Accept the rules and gain full access to the server.")]
    [Hidden]
    public async Task accept(CommandContext ctx)
    {
        var user = ctx.User;
        var role = ctx.Guild.Roles.FirstOrDefault(x => x.Name == "Approved");
        await (user as IGuildUser).AddRoleAsync(role);
        await ctx.RespondAsync("You are now approved!");
    }

【问题讨论】:

  • 你的问题有点简洁。你能更详细地解释一下你遇到的问题吗?

标签: c# discord


【解决方案1】:

您向公会内的成员添加角色,这是通过首先获取DiscordMember 实例然后添加角色来完成的。

var member = ctx.Member;
await ctx.Guild.GrantRoleAsync(member, role);

请注意,这仅适用于公会,并且会导致 DM 出现问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-08-13
    • 1970-01-01
    • 1970-01-01
    • 2015-08-01
    • 2012-04-04
    • 2023-02-14
    • 2019-03-31
    相关资源
    最近更新 更多