用户点击反应时,我无法发布角色

时间:2020-08-27 10:27:57

标签: c# discord.net

如您所见,如果您删除了发出代码的位置,则当用户单击反应时,它将被评分并以用户名的形式作为响应出现。因为我在程序中编写了此方法并添加了旧版ModuleBase,所以它必须是有限的,所以我必须返回null,因此我总是在所需类的参数中仅将dobavlyu这个问题像素化,但是现在我不能添加超过3个类因为字段ReactionAdded需要3个参数,所以请告诉我该方法如何在内部获取SocketGuild,SocketGuildUser和Yes角色变量,因为ModuleBase

 [Command("react")]
        public async Task HandleReaction()
        {
            RestUserMessage message = await Context.Channel.SendMessageAsync("react");
            Program.MessageId = message.Id;
        }

internal static ulong MessageId { get; set; }
  public  ITextChannel textChannel;

    public SocketGuildUser user;    

private async Task OnReactionAdd(Cacheable<IUserMessage, ulong> cache, ISocketMessageChannel channel, SocketReaction reaction)
        {
          
            if (reaction.MessageId == Program.MessageId)
            {
                  if (reaction.Emote.Name == "?")
            {
                

                ulong roleid = 747992707351183541;
                var role = textChannel.Guild.GetRole(roleid); //System.NullReferenceException: "Object reference not set to an instance of an object."

                await user.AddRoleAsync(role);
                await channel.SendMessageAsync(reaction.User.Value.Username);
            }
            }
        }

1 个答案:

答案 0 :(得分:0)

private async Task OnReactionAdd(Cacheable<IUserMessage, ulong> cache, ISocketMessageChannel channel, SocketReaction reaction)
{  
    if (reaction.MessageId == Program.MessageId)
    {
        if (reaction.Emote.Name == "?")
        {
            ulong roleid = 747992707351183541;
            //here can cast the ISocketMessageChannel to an ITextChannel
            if (channel is SocketTextChannel textChannel) 
            {
                var role = textChannel.Guild.GetRole(roleid); 
                var user = textChannel.Guild.GetUser(reaction.UserId);
                
                await user.AddRoleAsync(role);
                await textChannel.SendMessageAsync(user.Username);
            }   
        }
    }    
}