【问题标题】:Grant a role through Discord Bot in C#?在 C# 中通过 Discord Bot 授予角色?
【发布时间】:2018-07-24 11:31:11
【问题描述】:

我发现无法使用我的机器人将用户的角色/等级设置为“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】:

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

    !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);
    }
    

    编辑 根据您的评论,您似乎希望它通过提及来工作。

    为此,您可以使用 IUser。

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

    【讨论】:

    • 我希望它更像:!geoff @juan,所以它会在命令之后将滚动分配给这个人。
    猜你喜欢
    • 1970-01-01
    • 2021-04-15
    • 2021-10-11
    • 2021-04-07
    • 2020-02-26
    • 2019-04-16
    • 2021-08-09
    • 2016-11-19
    • 1970-01-01
    相关资源
    最近更新 更多